1 ! Copyright (C) 2006 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
29 SPECIALIZED-ARRAY: uchar
32 TUPLE: space-invaders < cpu port1 port2i port2o port3o port4lo port4hi port5o bitmap sounds looping? ;
33 CONSTANT: game-width 224
34 CONSTANT: game-height 256
36 : make-opengl-bitmap ( -- array )
37 game-height game-width 3 * * uchar <c-array> ;
39 : bitmap-index ( point -- index )
41 first2 game-width 3 * * swap 3 * + ;
43 : set-bitmap-pixel ( color point array -- )
44 #! 'color' is a {r g b}. Point is {x y}.
45 [ bitmap-index ] dip ! color index array
46 [ [ first ] 2dip set-nth ] 3keep
47 [ [ second ] 2dip [ 1 + ] dip set-nth ] 3keep
48 [ third ] 2dip [ 2 + ] dip set-nth ;
50 : get-bitmap-pixel ( point array -- color )
51 #! Point is a {x y}. color is a {r g b}
54 [ [ 1 + ] dip nth ] 2keep
55 [ 2 + ] dip nth 3array ;
57 CONSTANT: SOUND-SHOT 0
59 CONSTANT: SOUND-BASE-HIT 2
60 CONSTANT: SOUND-INVADER-HIT 3
61 CONSTANT: SOUND-WALK1 4
62 CONSTANT: SOUND-WALK2 5
63 CONSTANT: SOUND-WALK3 6
64 CONSTANT: SOUND-WALK4 7
65 CONSTANT: SOUND-UFO-HIT 8
67 : init-sound ( index cpu filename -- )
68 normalize-path swapd [ sounds>> nth AL_BUFFER ] dip
69 create-buffer-from-wav set-source-param ;
71 : init-sounds ( cpu -- )
73 [ 9 gen-sources swap (>>sounds) ] keep
74 [ SOUND-SHOT "resource:extra/space-invaders/resources/Shot.wav" init-sound ] keep
75 [ SOUND-UFO "resource:extra/space-invaders/resources/Ufo.wav" init-sound ] keep
76 [ sounds>> SOUND-UFO swap nth AL_LOOPING AL_TRUE set-source-param ] keep
77 [ SOUND-BASE-HIT "resource:extra/space-invaders/resources/BaseHit.wav" init-sound ] keep
78 [ SOUND-INVADER-HIT "resource:extra/space-invaders/resources/InvHit.Wav" init-sound ] keep
79 [ SOUND-WALK1 "resource:extra/space-invaders/resources/Walk1.wav" init-sound ] keep
80 [ SOUND-WALK2 "resource:extra/space-invaders/resources/Walk2.wav" init-sound ] keep
81 [ SOUND-WALK3 "resource:extra/space-invaders/resources/Walk3.wav" init-sound ] keep
82 [ SOUND-WALK4 "resource:extra/space-invaders/resources/Walk4.wav" init-sound ] keep
83 [ SOUND-UFO-HIT "resource:extra/space-invaders/resources/UfoHit.wav" init-sound ] keep
86 : cpu-init ( cpu -- cpu )
87 make-opengl-bitmap over (>>bitmap)
91 : <space-invaders> ( -- cpu )
92 space-invaders new cpu-init ;
94 : play-invaders-sound ( cpu sound -- )
95 swap sounds>> nth source-play ;
97 : stop-invaders-sound ( cpu sound -- )
98 swap sounds>> nth source-stop ;
100 : read-port1 ( cpu -- byte )
101 #! Port 1 maps the keys for space invaders
103 #! Bit 1 = two players button
104 #! Bit 2 = one player button
105 #! Bit 4 = player one fire
106 #! Bit 5 = player one left
107 #! Bit 6 = player one right
108 [ port1>> dup HEX: FE bitand ] keep
111 : read-port2 ( cpu -- byte )
112 #! Port 2 maps player 2 controls and dip switches
113 #! Bit 0,1 = number of ships
114 #! Bit 2 = mode (1=easy, 0=hard)
115 #! Bit 4 = player two fire
116 #! Bit 5 = player two left
117 #! Bit 6 = player two right
118 #! Bit 7 = show or hide coin info
119 [ port2i>> HEX: 8F bitand ] keep
120 port1>> HEX: 70 bitand bitor ;
122 : read-port3 ( cpu -- byte )
123 #! Used to compute a special formula
124 [ port4hi>> 8 shift ] keep
125 [ port4lo>> bitor ] keep
126 port2o>> shift -8 shift HEX: FF bitand ;
128 M: space-invaders read-port ( port cpu -- byte )
129 #! Read a byte from the hardware port. 'port' should
130 #! be an 8-bit value.
138 : write-port2 ( value cpu -- )
139 #! Setting this value affects the value read from port 3
142 : bit-newly-set? ( old-value new-value bit -- bool )
143 tuck bit? [ bit? not ] dip and ;
145 : port3-newly-set? ( new-value cpu bit -- bool )
146 [ port3o>> swap ] dip bit-newly-set? ;
148 : port5-newly-set? ( new-value cpu bit -- bool )
149 [ port5o>> swap ] dip bit-newly-set? ;
151 : write-port3 ( value cpu -- )
152 #! Connected to the sound hardware
153 #! Bit 0 = spaceship sound (looped)
155 #! Bit 2 = Your ship hit
156 #! Bit 3 = Invader hit
157 #! Bit 4 = Extended play sound
158 over 0 bit? over looping?>> not and [
159 dup SOUND-UFO play-invaders-sound
162 over 0 bit? not over looping?>> and [
163 dup SOUND-UFO stop-invaders-sound
166 2dup 0 port3-newly-set? [ dup SOUND-UFO play-invaders-sound ] when
167 2dup 1 port3-newly-set? [ dup SOUND-SHOT play-invaders-sound ] when
168 2dup 2 port3-newly-set? [ dup SOUND-BASE-HIT play-invaders-sound ] when
169 2dup 3 port3-newly-set? [ dup SOUND-INVADER-HIT play-invaders-sound ] when
172 : write-port4 ( value cpu -- )
173 #! Affects the value returned by reading port 3
178 : write-port5 ( value cpu -- )
180 #! Bit 0 = invaders sound 1
181 #! Bit 1 = invaders sound 2
182 #! Bit 2 = invaders sound 3
183 #! Bit 3 = invaders sound 4
184 #! Bit 4 = spaceship hit
185 #! Bit 5 = amplifier enabled/disabled
186 2dup 0 port5-newly-set? [ dup SOUND-WALK1 play-invaders-sound ] when
187 2dup 1 port5-newly-set? [ dup SOUND-WALK2 play-invaders-sound ] when
188 2dup 2 port5-newly-set? [ dup SOUND-WALK3 play-invaders-sound ] when
189 2dup 3 port5-newly-set? [ dup SOUND-WALK4 play-invaders-sound ] when
190 2dup 4 port5-newly-set? [ dup SOUND-UFO-HIT play-invaders-sound ] when
193 M: space-invaders write-port ( value port cpu -- )
194 #! Write a byte to the hardware port, where 'port' is
197 { 2 [ write-port2 ] }
198 { 3 [ write-port3 ] }
199 { 4 [ write-port4 ] }
200 { 5 [ write-port5 ] }
204 M: space-invaders reset ( cpu -- )
215 : gui-step ( cpu -- )
216 [ read-instruction ] keep ! n cpu
217 over get-cycles over inc-cycles
218 [ swap instructions nth call( cpu -- ) ] keep
219 [ pc>> HEX: FFFF bitand ] keep
222 : gui-frame/2 ( cpu -- )
225 over 16667 < [ ! cycles cpu
228 [ [ 16667 - ] dip (>>cycles) ] keep
229 dup last-interrupt>> HEX: 10 = [
230 HEX: 08 over (>>last-interrupt) HEX: 08 swap interrupt
232 HEX: 10 over (>>last-interrupt) HEX: 10 swap interrupt
236 : gui-frame ( cpu -- )
237 dup gui-frame/2 gui-frame/2 ;
239 : coin-down ( cpu -- )
240 [ port1>> 1 bitor ] keep (>>port1) ;
243 [ port1>> 255 1 - bitand ] keep (>>port1) ;
245 : player1-down ( cpu -- )
246 [ port1>> 4 bitor ] keep (>>port1) ;
248 : player1-up ( cpu -- )
249 [ port1>> 255 4 - bitand ] keep (>>port1) ;
251 : player2-down ( cpu -- )
252 [ port1>> 2 bitor ] keep (>>port1) ;
254 : player2-up ( cpu -- )
255 [ port1>> 255 2 - bitand ] keep (>>port1) ;
257 : fire-down ( cpu -- )
258 [ port1>> HEX: 10 bitor ] keep (>>port1) ;
261 [ port1>> 255 HEX: 10 - bitand ] keep (>>port1) ;
263 : left-down ( cpu -- )
264 [ port1>> HEX: 20 bitor ] keep (>>port1) ;
267 [ port1>> 255 HEX: 20 - bitand ] keep (>>port1) ;
269 : right-down ( cpu -- )
270 [ port1>> HEX: 40 bitor ] keep (>>port1) ;
272 : right-up ( cpu -- )
273 [ port1>> 255 HEX: 40 - bitand ] keep (>>port1) ;
276 TUPLE: invaders-gadget < gadget cpu quit? windowed? ;
279 { T{ key-down f f "ESC" } [ t over (>>quit?) dup windowed?>> [ close-window ] [ drop ] if ] }
280 { T{ key-down f f "BACKSPACE" } [ cpu>> coin-down ] }
281 { T{ key-up f f "BACKSPACE" } [ cpu>> coin-up ] }
282 { T{ key-down f f "1" } [ cpu>> player1-down ] }
283 { T{ key-up f f "1" } [ cpu>> player1-up ] }
284 { T{ key-down f f "2" } [ cpu>> player2-down ] }
285 { T{ key-up f f "2" } [ cpu>> player2-up ] }
286 { T{ key-down f f "UP" } [ cpu>> fire-down ] }
287 { T{ key-up f f "UP" } [ cpu>> fire-up ] }
288 { T{ key-down f f "LEFT" } [ cpu>> left-down ] }
289 { T{ key-up f f "LEFT" } [ cpu>> left-up ] }
290 { T{ key-down f f "RIGHT" } [ cpu>> right-down ] }
291 { T{ key-up f f "RIGHT" } [ cpu>> right-up ] }
294 : <invaders-gadget> ( cpu -- gadget )
299 M: invaders-gadget pref-dim* drop { 224 256 } ;
301 M: invaders-gadget draw-gadget* ( gadget -- )
304 [ 224 256 GL_RGB GL_UNSIGNED_BYTE ] dip
305 cpu>> bitmap>> glDrawPixels ;
307 CONSTANT: black { 0 0 0 }
308 CONSTANT: white { 255 255 255 }
309 CONSTANT: green { 0 255 0 }
310 CONSTANT: red { 255 0 0 }
312 : addr>xy ( addr -- point )
313 #! Convert video RAM address to base X Y value. point is a {x y}.
315 dup HEX: 1f bitand 8 * 255 swap - ! n y
316 swap -5 shift swap 2array ;
318 : plot-bitmap-pixel ( bitmap point color -- )
319 #! point is a {x y}. color is a {r g b}.
320 spin set-bitmap-pixel ;
322 : within ( n a b -- bool )
324 rot tuck swap <= [ swap >= ] dip and ;
326 : get-point-color ( point -- color )
327 #! Return the color to use for the given x/y position.
330 { [ dup 184 238 within pick 0 223 within and ] [ 2drop green ] }
331 { [ dup 240 247 within pick 16 133 within and ] [ 2drop green ] }
332 { [ dup 247 215 - 247 184 - within pick 0 223 within and ] [ 2drop red ] }
336 : plot-bitmap-bits ( bitmap point byte bit -- )
339 dup swapd -1 * shift 1 bitand 0 =
341 [ black ] [ dup get-point-color ] if
344 : do-bitmap-update ( bitmap value addr -- )
346 [ 0 plot-bitmap-bits ] 3keep
347 [ 1 plot-bitmap-bits ] 3keep
348 [ 2 plot-bitmap-bits ] 3keep
349 [ 3 plot-bitmap-bits ] 3keep
350 [ 4 plot-bitmap-bits ] 3keep
351 [ 5 plot-bitmap-bits ] 3keep
352 [ 6 plot-bitmap-bits ] 3keep
355 M: space-invaders update-video ( value addr cpu -- )
357 bitmap>> -rot do-bitmap-update
362 : sync-frame ( millis -- millis )
363 #! Sleep until the time for the next frame arrives.
364 1000 60 / >fixnum + system:millis - dup 0 >
365 [ milliseconds threads:sleep ] [ drop threads:yield ] if system:millis ;
367 : invaders-process ( millis gadget -- )
368 #! Run a space invaders gadget inside a
369 #! concurrent process. Messages can be sent to
370 #! signal key presses, etc.
375 [ cpu>> gui-frame ] keep
380 M: invaders-gadget graft* ( gadget -- )
381 dup cpu>> init-sounds
383 [ system:millis swap invaders-process ] curry
384 "Space invaders" threads:spawn drop ;
386 M: invaders-gadget ungraft* ( gadget -- )
389 : (run) ( title cpu rom-info -- )
390 over load-rom* <invaders-gadget> t >>windowed? swap open-window ;
393 { HEX: 0000 "invaders/invaders.h" }
394 { HEX: 0800 "invaders/invaders.g" }
395 { HEX: 1000 "invaders/invaders.f" }
396 { HEX: 1800 "invaders/invaders.e" }
399 : run-invaders ( -- )
401 "Space Invaders" <space-invaders> rom-info (run)