1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors audio.engine combinators concurrency.promises
4 destructors game.input game.loop kernel math parser sequences
5 threads ui ui.gadgets ui.gadgets.worlds vocabs.parser
9 TUPLE: game-world < world
12 { tick-interval-nanos integer }
13 { use-game-input? boolean }
14 { use-audio-engine? boolean }
15 { audio-engine-device initial: f }
16 { audio-engine-voice-count initial: 16 }
17 { tick-slice float initial: 0.0 } ;
19 GENERIC: begin-game-world ( world -- )
20 M: object begin-game-world drop ;
22 GENERIC: end-game-world ( world -- )
23 M: object end-game-world drop ;
25 GENERIC: tick-game-world ( world -- )
26 M: object tick-game-world drop ;
30 [ audio-engine>> [ update-audio ] when* ] bi ;
33 swap >>tick-slice relayout-1 yield ;
37 : open-game-audio-engine ( game-world -- audio-engine )
39 [ audio-engine-device>> ]
40 [ audio-engine-voice-count>> ]
41 } cleave <audio-engine>
42 [ start-audio* ] keep ; inline
46 M: game-world begin-world
47 dup use-game-input?>> [ open-game-input ] when
48 dup use-audio-engine?>> [ dup open-game-audio-engine >>audio-engine ] when
49 dup [ tick-interval-nanos>> ] [ ] bi <game-loop>
50 [ >>game-loop begin-game-world ] keep start-loop ;
52 M: game-world end-world
53 dup game-loop>> [ stop-loop ] when*
55 [ audio-engine>> [ dispose ] when* ]
56 [ use-game-input?>> [ close-game-input ] when ] tri ;
58 TUPLE: game-attributes < world-attributes
59 { tick-interval-nanos integer }
60 { use-game-input? boolean initial: f }
61 { use-audio-engine? boolean initial: f }
62 { audio-engine-device initial: f }
63 { audio-engine-voice-count initial: 16 } ;
65 M: game-world apply-world-attributes
67 [ tick-interval-nanos>> >>tick-interval-nanos ]
68 [ use-game-input?>> >>use-game-input? ]
69 [ use-audio-engine?>> >>use-audio-engine? ]
70 [ audio-engine-device>> >>audio-engine-device ]
71 [ audio-engine-voice-count>> >>audio-engine-voice-count ]
75 : start-game ( attributes -- game-world )
78 : wait-game ( attributes -- game-world )
79 f swap open-window* dup promise>> ?promise drop ;
81 : define-attributes-word ( word tuple -- )
82 [ name>> "-attributes" append create-word-in ] dip define-constant ;
86 game-attributes parse-window-attributes
87 2dup define-attributes-word
89 [ define-window ] [ 2drop current-vocab main<< ] 3bi ;