view clojure/com/aurellem/run/image.clj @ 494:79606f173658

improbed color display kernel.
author Robert McIntyre <rlm@mit.edu>
date Thu, 07 Jun 2012 23:11:18 -0500
parents 783a09c84a28
children 1d81ddd4fa41
line wrap: on
line source
1 (ns com.aurellem.run.image
2 (:use (com.aurellem.gb saves gb-driver util constants
3 items vbm characters money
4 rlm-assembly))
5 (:use (com.aurellem.run util music title save-corruption
6 bootstrap-0 bootstrap-1))
7 (:require clojure.string)
8 (:import [com.aurellem.gb.gb_driver SaveState])
9 (:import java.io.File))
11 ;; want to display an image onto the screen.
12 ;; probably will be the six ponies, possibly with scrolling.
14 ;; probably don't need hi-color mode since the images shuld be
15 ;; simple.
17 ;; use background tiles? they provide greater color depth than
18 ;; sprites, and can still be scrolled, so why not?
21 ;; First of all, RGB colors in an image are not the same as those in a
22 ;; GameBoy, so I need to convert them. Fortunately, this code is
23 ;; already written for me in this C-code from the public domain
24 ;; hi-color converter by Glen Cook, Jeff Frohwein, and Rob Jones.
26 ;; the code snipped itself is by Brett Bibby and is translated here
27 ;; from C into clojure.
30 ;; This section of code is used to convert an RGB (pc) triplet into
31 ;; a RGB (gameboy) triplet. This section of code was kindly donated
32 ;; by Brett Bibby (GameBrains).
34 ;; BYTE intensity[32] = {
35 ;; 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x5e, 0x6c, 0x7a, 0x88, 0x94,
36 ;; 0xa0, 0xae, 0xb7, 0xbf, 0xc6, 0xce, 0xd3, 0xd9, 0xdf, 0xe3, 0xe7,
37 ;; 0xeb, 0xef, 0xf3, 0xf6, 0xf9, 0xfb, 0xfd, 0xfe, 0xff, 0xff };
39 ;; unsigned char influence[3][3] =
40 ;; {
41 ;; {16,4,4},
42 ;; {8,16,8},
43 ;; {0,8,16}
44 ;; };
46 ;; RGBQUAD translate(BYTE rgb[3])
47 ;; {
48 ;; RGBQUAD color;
49 ;; BYTE tmp[3];
50 ;; BYTE m[3][3];
51 ;; BYTE i,j;
53 ;; for (i=0;i<3;i++)
54 ;; for (j=0;j<3;j++)
55 ;; m[i][j] = (intensity[rgb[i]>>3]*influence[i][j]) >> 5;
57 ;; for (i=0;i<3;i++)
58 ;; {
59 ;; if (m[0][i]>m[1][i])
60 ;; {
61 ;; j=m[0][i];
62 ;; m[0][i]=m[1][i];
63 ;; m[1][i]=j;
64 ;; }
66 ;; if (m[1][i]>m[2][i])
67 ;; {
68 ;; j=m[1][i];
69 ;; m[1][i]=m[2][i];
70 ;; m[2][i]=j;
71 ;; }
73 ;; if (m[0][i]>m[1][i])
74 ;; {
75 ;; j=m[0][i];
76 ;; m[0][i]=m[1][i];
77 ;; m[1][i]=j;
78 ;; }
80 ;; tmp[i]=(((m[0][i]+m[1][i]*2+m[2][i]*4)*5) >> 4)+32;
81 ;; }
83 ;; color.rgbRed = tmp[0];
84 ;; color.rgbGreen = tmp[1];
85 ;; color.rgbBlue = tmp[2];
87 ;; return color;
88 ;; }
93 (def image-program-target 0xB000)
96 (def display-width 160)
97 (def display-height 144)
101 ;{:r :g :b }
103 (def character-data 0x8000)
104 (def character-data-end 0x97FF)
109 (def BG-data-1 0x9800)
111 (def BG-data-2 0x9C00)
113 (def OAM 0xFE00)
117 (def video-bank-select-register 0xFF4F)
119 (defn gb-rgb->bits [[r g b]]
120 (assert (<= 0 r 31))
121 (assert (<= 0 g 31))
122 (assert (<= 0 b 31))
123 [(bit-and
124 0xFF
125 (+
126 r
127 (bit-shift-left g 5)))
128 (+
129 (bit-shift-right g 3)
130 (bit-shift-left b 2))])
133 (def bg-palette-select 0xFF68)
134 (def bg-palette-data 0xFF69)
136 (def obj-palette-select 0xFF6A)
137 (def obj-palette-data 0xFF6B)
139 (def max-palettes 8)
141 (defn write-data [target data]
142 (flatten
143 [0x3E ;; load literal to A
144 data
145 0xEA ;; load A into target
146 (reverse (disect-bytes-2 target))]))
148 (defn begin-sequential-palette-write
149 [palette-num palette-select-address]
150 (assert (<= 0 palette-num max-palettes))
151 (assert
152 (or (= palette-select-address bg-palette-select)
153 (= palette-select-address obj-palette-select)))
154 (let [palette-write-data
155 (Integer/parseInt
156 (str "1" ;; auto increment
157 "0" ;; not used
158 (format
159 "%03d"
160 (Integer/parseInt
161 (Integer/toBinaryString palette-num) 10))
162 "00" ;; color num
163 "0" ;; H/L
164 ) 2)]
165 (write-data palette-select-address palette-write-data)))
167 (defn set-palettes [palette-select palette-data palettes]
168 (assert (<= (count palettes)) max-palettes)
169 (flatten
170 [(begin-sequential-palette-write 0 palette-select)
171 (map (partial write-data palette-data)
172 (flatten (map gb-rgb->bits palettes)))]))
174 (defn display-one-color
175 "Displayes a single color onto the gameboy screen. input rgb in
176 gameboy rgb."
177 [[r g b]]
178 ;; construct a kernel that displays a single color
179 (let
180 [palettes (repeat 8 [r g b])
181 kernel-address 0xC000
182 kernel
183 [0xF3 ;; disable interrupts
184 (clear-music-registers)
185 (frame-metronome)
186 (set-palettes obj-palette-select obj-palette-data palettes)
187 (set-palettes bg-palette-select bg-palette-data palettes)
188 (infinite-loop)]]
189 (-> (set-memory-range (second (music-base))
190 kernel-address (flatten kernel))
191 (PC! kernel-address))))
196 (defn write-palette-color [palette-num r g b]
197 (let [[byte-1 byte-2] (gb-rgb->bits r g b)]
200 ))