1 ! (c)2009 Joe Groff bsd license
2 USING: accessors audio.engine combinators concurrency.promises
3 destructors game.input game.loop kernel math parser sequences
4 threads ui ui.gadgets ui.gadgets.worlds vocabs.parser words.constant ;
7 TUPLE: game-world < world
10 { tick-interval-nanos integer }
11 { use-game-input? boolean }
12 { use-audio-engine? boolean }
13 { audio-engine-device initial: f }
14 { audio-engine-voice-count initial: 16 }
15 { tick-slice float initial: 0.0 } ;
17 GENERIC: begin-game-world ( world -- )
18 M: object begin-game-world drop ;
20 GENERIC: end-game-world ( world -- )
21 M: object end-game-world drop ;
23 GENERIC: tick-game-world ( world -- )
24 M: object tick-game-world drop ;
28 [ audio-engine>> [ update-audio ] when* ] bi ;
31 swap >>tick-slice relayout-1 yield ;
35 : open-game-audio-engine ( game-world -- audio-engine )
37 [ audio-engine-device>> ]
38 [ audio-engine-voice-count>> ]
39 } cleave <audio-engine>
40 [ start-audio* ] keep ; inline
44 M: game-world begin-world
45 dup use-game-input?>> [ open-game-input ] when
46 dup use-audio-engine?>> [ dup open-game-audio-engine >>audio-engine ] when
47 dup [ tick-interval-nanos>> ] [ ] bi <game-loop>
48 [ >>game-loop begin-game-world ] keep start-loop ;
50 M: game-world end-world
51 dup game-loop>> [ stop-loop ] when*
53 [ audio-engine>> [ dispose ] when* ]
54 [ use-game-input?>> [ close-game-input ] when ] tri ;
56 TUPLE: game-attributes < world-attributes
57 { tick-interval-nanos integer }
58 { use-game-input? boolean initial: f }
59 { use-audio-engine? boolean initial: f }
60 { audio-engine-device initial: f }
61 { audio-engine-voice-count initial: 16 } ;
63 M: game-world apply-world-attributes
65 [ tick-interval-nanos>> >>tick-interval-nanos ]
66 [ use-game-input?>> >>use-game-input? ]
67 [ use-audio-engine?>> >>use-audio-engine? ]
68 [ audio-engine-device>> >>audio-engine-device ]
69 [ audio-engine-voice-count>> >>audio-engine-voice-count ]
73 : start-game ( attributes -- game-world )
76 : wait-game ( attributes -- game-world )
77 f swap open-window* dup promise>> ?promise drop ;
79 : define-attributes-word ( word tuple -- )
80 [ name>> "-attributes" append create-word-in ] dip define-constant ;
84 game-attributes parse-window-attributes
85 2dup define-attributes-word
87 [ define-window ] [ 2drop current-vocab main<< ] 3bi ;