]> gitweb.factorcode.org Git - factor.git/blob - extra/game/worlds/worlds.factor
Merge branch 'master' of git://github.com/william42/factor
[factor.git] / extra / game / worlds / worlds.factor
1 ! (c)2009 Joe Groff bsd license
2 USING: accessors combinators fry game.input game.loop generic kernel math
3 parser sequences ui ui.gadgets ui.gadgets.worlds ui.gestures threads
4 words audio.engine destructors ;
5 IN: game.worlds
6
7 TUPLE: game-world < world
8     game-loop
9     audio-engine
10     { tick-interval-micros fixnum }
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-micros>> ] [ ] bi <game-loop>
48     [ >>game-loop begin-game-world ] keep start-loop ;
49
50 M: game-world end-world
51     [ [ stop-loop ] when* f ] change-game-loop
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-micros fixnum }
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-micros>> >>tick-interval-micros ]
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 SYNTAX: GAME:
74     CREATE
75     game-attributes parse-main-window-attributes
76     parse-definition
77     define-main-window ;