]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/game-loop/game-loop.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / extra / game-loop / game-loop.factor
index 8abbe6ba25ef5e702554e86ce3056be4a96717f2..5f78c6770cadcfbadc2dc70b4584377fb18eff52 100644 (file)
@@ -1,5 +1,6 @@
-USING: accessors calendar destructors kernel math math.order namespaces
-system threads ;
+USING: accessors calendar continuations destructors kernel math
+math.order namespaces system threads ui ui.gadgets.worlds
+sequences ;
 IN: game-loop
 
 TUPLE: game-loop
@@ -27,26 +28,36 @@ SYMBOL: game-loop
 
 CONSTANT: MAX-FRAMES-TO-SKIP 5
 
+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 ;
+
 <PRIVATE
 
 : redraw ( loop -- )
-    [ 1+ ] change-frame-number
+    [ 1 + ] change-frame-number
     [ tick-slice ] [ delegate>> ] bi draw* ;
 
 : tick ( loop -- )
     delegate>> tick* ;
 
 : increment-tick ( loop -- )
-    [ 1+ ] change-tick-number
+    [ 1 + ] change-tick-number
     dup tick-length>> [ + ] curry change-last-tick
     drop ;
 
 : ?tick ( loop count -- )
-    dup zero? [ drop millis >>last-tick drop ] [
+    [ millis >>last-tick drop ] [
         over [ since-last-tick ] [ tick-length>> ] bi >=
-        [ [ drop increment-tick ] [ drop tick ] [ 1- ?tick ] 2tri ]
+        [ [ drop increment-tick ] [ drop tick ] [ 1 - ?tick ] 2tri ]
         [ 2drop ] if
-    ] if ;
+    ] if-zero ;
 
 : (run-loop) ( loop -- )
     dup running?>>
@@ -54,7 +65,9 @@ CONSTANT: MAX-FRAMES-TO-SKIP 5
     [ drop ] if ;
 
 : run-loop ( loop -- )
-    dup game-loop [ (run-loop) ] with-variable ;
+    dup game-loop
+    [ [ (run-loop) ] [ game-loop-error ] recover ]
+    with-variable ;
 
 : benchmark-millis ( loop -- millis )
     millis swap benchmark-time>> - ;
@@ -91,3 +104,6 @@ PRIVATE>
 M: game-loop dispose
     stop-loop ;
 
+USING: vocabs vocabs.loader ;
+
+"prettyprint" vocab [ "game-loop.prettyprint" require ] when