factor/extra/game/loop/loop.factor

119 lines
2.9 KiB
Factor
Raw Normal View History

2010-01-17 02:07:11 -05:00
! (c)2009 Joe Groff bsd license
2010-06-10 17:39:13 -04:00
USING: accessors timers alien.c-types calendar classes.struct
continuations destructors fry kernel math math.order memory
namespaces sequences specialized-vectors system
tools.memory ui ui.gadgets.worlds vm vocabs.loader arrays
2010-06-16 17:09:51 -04:00
benchmark.struct locals ;
IN: game.loop
2009-04-28 18:23:08 -04:00
TUPLE: game-loop
2010-05-21 21:42:12 -04:00
{ tick-interval-nanos integer read-only }
tick-delegate
draw-delegate
2009-04-28 18:23:08 -04:00
{ running? boolean }
{ tick# integer }
{ frame# integer }
2010-06-16 17:09:51 -04:00
tick-timer
draw-timer
benchmark-data ;
STRUCT: game-loop-benchmark
{ benchmark-data-pair benchmark-data-pair }
{ tick# ulonglong }
{ frame# ulonglong } ;
SPECIALIZED-VECTOR: game-loop-benchmark
: <game-loop-benchmark> ( benchmark-data-pair tick frame -- obj )
\ game-loop-benchmark <struct>
swap >>frame#
swap >>tick#
swap >>benchmark-data-pair ; inline
2009-04-28 18:23:08 -04:00
GENERIC: tick* ( delegate -- )
GENERIC: draw* ( tick-slice delegate -- )
DEFER: stop-loop
TUPLE: game-loop-error game-loop error ;
: ?ui-error ( error -- )
ui-running? [ ui-error ] [ rethrow ] if ;
: game-loop-error ( game-loop error -- )
[ drop stop-loop ] [ \ game-loop-error boa ?ui-error ] 2bi ;
2010-05-21 21:42:12 -04:00
: fps ( fps -- nanos )
2010-06-16 17:09:51 -04:00
[ 1,000,000,000 ] dip /i ; inline
2009-04-28 18:23:08 -04:00
<PRIVATE
: record-benchmarking ( benchark-data-pair loop -- )
[ tick#>> ]
[ frame#>> <game-loop-benchmark> ]
[ benchmark-data>> ] tri push ;
2010-06-16 17:09:51 -04:00
: last-tick-percent-offset ( loop -- float )
[ draw-timer>> iteration-start-nanos>> nano-count swap - ]
[ tick-interval-nanos>> ] bi /f 1.0 min ;
2009-04-28 18:23:08 -04:00
: redraw ( loop -- )
[ 1 + ] change-frame#
[
2010-06-16 17:09:51 -04:00
[ last-tick-percent-offset ] [ draw-delegate>> ] bi
[ draw* ] with-benchmarking
] keep record-benchmarking ;
2009-04-28 18:23:08 -04:00
: tick ( loop -- )
[
[ tick-delegate>> tick* ] with-benchmarking
] keep record-benchmarking ;
2009-04-28 18:23:08 -04:00
: increment-tick ( loop -- )
[ 1 + ] change-tick#
2009-04-28 18:23:08 -04:00
drop ;
PRIVATE>
2010-06-16 17:09:51 -04:00
:: when-running ( loop quot -- )
[
loop
dup running?>> quot [ drop ] if
] [
loop game-loop-error
] recover ; inline
: tick-iteration ( loop -- )
[ [ tick ] [ increment-tick ] bi ] when-running ;
: frame-iteration ( loop -- )
[ redraw ] when-running ;
2010-05-20 13:28:02 -04:00
2009-04-28 18:23:08 -04:00
: start-loop ( loop -- )
t >>running?
2010-06-16 17:09:51 -04:00
dup
[ '[ _ tick-iteration ] f ]
[ tick-interval-nanos>> nanoseconds ] bi <timer> >>tick-timer
dup '[ _ frame-iteration ] f 1 milliseconds <timer> >>draw-timer
[ tick-timer>> ] [ draw-timer>> ] bi [ start-timer ] bi@ ;
2009-04-28 18:23:08 -04:00
: stop-loop ( loop -- )
f >>running?
2010-06-16 17:09:51 -04:00
[ tick-timer>> ] [ draw-timer>> ] bi [ stop-timer ] bi@ ;
2009-04-28 18:23:08 -04:00
2010-05-21 21:42:12 -04:00
: <game-loop*> ( tick-interval-nanos tick-delegate draw-delegate -- loop )
2010-06-16 17:09:51 -04:00
f 0 0 f f
game-loop-benchmark-vector{ } clone
2009-04-28 18:23:08 -04:00
game-loop boa ;
2010-05-21 21:42:12 -04:00
: <game-loop> ( tick-interval-nanos delegate -- loop )
dup <game-loop*> ; inline
2009-04-28 18:23:08 -04:00
M: game-loop dispose
stop-loop ;
{ "game.loop" "prettyprint" } "game.loop.prettyprint" require-when