]> gitweb.factorcode.org Git - factor.git/blob - extra/game/loop/loop.factor
121176089b14614d8c990ca3c184c057976e1565
[factor.git] / extra / game / loop / loop.factor
1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors calendar continuations destructors fry kernel
4 locals math math.order system timers ui ui.gadgets.worlds
5 vocabs.loader ;
6 IN: game.loop
7
8 TUPLE: game-loop
9     { tick-interval-nanos integer read-only }
10     tick-delegate
11     draw-delegate
12     { running? boolean }
13     { tick# integer }
14     { frame# integer }
15     tick-timer
16     draw-timer
17     benchmark-data ;
18
19 GENERIC: tick* ( delegate -- )
20 GENERIC: draw* ( tick-slice delegate -- )
21
22 DEFER: stop-loop
23
24 TUPLE: game-loop-error-state error game-loop ;
25
26 : ?ui-error ( error -- )
27     ui-running? [ ui-error ] [ rethrow ] if ;
28
29 : game-loop-error ( error game-loop -- )
30     [ nip stop-loop ] [ \ game-loop-error-state boa ?ui-error ] 2bi ;
31
32 : fps ( fps -- nanos )
33     [ 1,000,000,000 ] dip /i ; inline
34
35 <PRIVATE
36
37 : last-tick-percent-offset ( loop -- float )
38     [ draw-timer>> next-nanos>> nano-count - ]
39     [ tick-interval-nanos>> ] bi /f 1.0 swap -
40     0.0 1.0 clamp ;
41
42 GENERIC#: record-benchmarking 1 ( loop quot -- )
43
44 M: object record-benchmarking
45     call( loop -- ) ;
46
47 : redraw ( loop -- )
48     [ 1 + ] change-frame#
49     [
50         [ last-tick-percent-offset ] [ draw-delegate>> ] bi
51         draw*
52     ] record-benchmarking ;
53
54 : tick ( loop -- )
55     [ tick-delegate>> tick* ] record-benchmarking ;
56
57 : increment-tick ( loop -- )
58     [ 1 + ] change-tick#
59     drop ;
60
61 PRIVATE>
62
63 :: when-running ( loop quot -- )
64     [
65         loop
66         dup running?>> quot [ drop ] if
67     ] [
68         loop game-loop-error
69     ] recover ; inline
70
71 : tick-iteration ( loop -- )
72     [ [ tick ] [ increment-tick ] bi ] when-running ;
73
74 : frame-iteration ( loop -- )
75     [ redraw ] when-running ;
76
77 : start-loop ( loop -- )
78     t >>running?
79
80     dup
81     [ '[ _ tick-iteration ] f ]
82     [ tick-interval-nanos>> nanoseconds ] bi <timer> >>tick-timer
83
84     dup '[ _ frame-iteration ] f 1 milliseconds <timer> >>draw-timer
85
86     [ tick-timer>> ] [ draw-timer>> ] bi [ start-timer ] bi@ ;
87
88 : stop-loop ( loop -- )
89     f >>running?
90     [ tick-timer>> ] [ draw-timer>> ] bi [ stop-timer ] bi@ ;
91
92 : <game-loop*> ( tick-interval-nanos tick-delegate draw-delegate -- loop )
93     f 0 0 f f f game-loop boa ;
94
95 : <game-loop> ( tick-interval-nanos delegate -- loop )
96     dup <game-loop*> ; inline
97
98 M: game-loop dispose
99     stop-loop ;
100
101 { "game.loop" "prettyprint" } "game.loop.prettyprint" require-when
102 { "game.loop" "tools.memory" } "game.loop.benchmark" require-when