Catch and wrap game-loop errors and send them through the UI when available. much nicer than having to check the console
							parent
							
								
									d951052ab1
								
							
						
					
					
						commit
						469d7af27a
					
				| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
USING: accessors calendar destructors kernel math math.order namespaces
 | 
					USING: accessors calendar continuations destructors kernel math
 | 
				
			||||||
system threads ;
 | 
					math.order namespaces system threads ui ui.gadgets.worlds ;
 | 
				
			||||||
IN: game-loop
 | 
					IN: game-loop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TUPLE: game-loop
 | 
					TUPLE: game-loop
 | 
				
			||||||
| 
						 | 
					@ -27,6 +27,16 @@ SYMBOL: game-loop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CONSTANT: MAX-FRAMES-TO-SKIP 5
 | 
					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
 | 
					<PRIVATE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: redraw ( loop -- )
 | 
					: redraw ( loop -- )
 | 
				
			||||||
| 
						 | 
					@ -54,7 +64,9 @@ CONSTANT: MAX-FRAMES-TO-SKIP 5
 | 
				
			||||||
    [ drop ] if ;
 | 
					    [ drop ] if ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: run-loop ( loop -- )
 | 
					: 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 )
 | 
					: benchmark-millis ( loop -- millis )
 | 
				
			||||||
    millis swap benchmark-time>> - ;
 | 
					    millis swap benchmark-time>> - ;
 | 
				
			||||||
| 
						 | 
					@ -91,3 +103,6 @@ PRIVATE>
 | 
				
			||||||
M: game-loop dispose
 | 
					M: game-loop dispose
 | 
				
			||||||
    stop-loop ;
 | 
					    stop-loop ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					USING: vocabs vocabs.loader ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					"prettyprint" vocab [ "game-loop.prettyprint" require ] when
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,9 @@
 | 
				
			||||||
 | 
					! (c)2009 Joe Groff bsd license
 | 
				
			||||||
 | 
					USING: accessors debugger game-loop io ;
 | 
				
			||||||
 | 
					IN: game-loop.prettyprint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					M: game-loop-error error.
 | 
				
			||||||
 | 
					    "An error occurred inside a game loop." print
 | 
				
			||||||
 | 
					    "The game loop has been stopped to prevent runaway errors." print
 | 
				
			||||||
 | 
					    "The error was:" print nl
 | 
				
			||||||
 | 
					    error>> error. ;
 | 
				
			||||||
		Loading…
	
		Reference in New Issue