]> gitweb.factorcode.org Git - factor.git/blob - extra/game/worlds/worlds.factor
46b3f2c5c23658f8bf68c4ab7c67b99a96c9bc2e
[factor.git] / extra / game / worlds / worlds.factor
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 ;
5 IN: game.worlds
6
7 TUPLE: game-world < world
8     game-loop
9     audio-engine
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 } ;
16
17 GENERIC: begin-game-world ( world -- )
18 M: object begin-game-world drop ;
19
20 GENERIC: end-game-world ( world -- )
21 M: object end-game-world drop ;
22
23 GENERIC: tick-game-world ( world -- )
24 M: object tick-game-world drop ;
25
26 M: game-world tick*
27     [ tick-game-world ]
28     [ audio-engine>> [ update-audio ] when* ] bi ;
29
30 M: game-world draw*
31     swap >>tick-slice relayout-1 yield ;
32
33 <PRIVATE
34
35 : open-game-audio-engine ( game-world -- audio-engine )
36     {
37         [ audio-engine-device>> ]
38         [ audio-engine-voice-count>> ]
39     } cleave <audio-engine>
40     [ start-audio* ] keep ; inline
41
42 PRIVATE>
43
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 ;
49
50 M: game-world end-world
51     dup game-loop>> [ stop-loop ] when*
52     [ end-game-world ]
53     [ audio-engine>> [ dispose ] when* ]
54     [ use-game-input?>> [ close-game-input ] when ] tri ;
55
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 } ;
62
63 M: game-world apply-world-attributes
64     {
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 ]
70         [ call-next-method ]
71     } cleave ;
72
73 : start-game ( attributes -- game-world )
74     f swap open-window* ;
75
76 : wait-game ( attributes -- game-world )
77     f swap open-window* dup promise>> ?promise drop ;
78
79 : define-attributes-word ( word tuple -- )
80     [ name>> "-attributes" append create-word-in ] dip define-constant ;
81
82 SYNTAX: GAME:
83     scan-new-word
84     game-attributes parse-window-attributes
85     2dup define-attributes-word
86     parse-definition
87     [ define-window ] [ 2drop current-vocab main<< ] 3bi ;