game-loop: change "tick-length" to more descriptive "tick-interval-micros"
							parent
							
								
									9131e16f7b
								
							
						
					
					
						commit
						bd62fb22aa
					
				| 
						 | 
				
			
			@ -4,7 +4,7 @@ sequences ;
 | 
			
		|||
IN: game.loop
 | 
			
		||||
 | 
			
		||||
TUPLE: game-loop
 | 
			
		||||
    { tick-length integer read-only }
 | 
			
		||||
    { tick-interval-micros integer read-only }
 | 
			
		||||
    delegate
 | 
			
		||||
    { last-tick integer }
 | 
			
		||||
    thread 
 | 
			
		||||
| 
						 | 
				
			
			@ -24,7 +24,7 @@ SYMBOL: game-loop
 | 
			
		|||
    last-tick>> system-micros swap - ;
 | 
			
		||||
 | 
			
		||||
: tick-slice ( loop -- slice )
 | 
			
		||||
    [ since-last-tick ] [ tick-length>> ] bi /f 1.0 min ;
 | 
			
		||||
    [ since-last-tick ] [ tick-interval-micros>> ] bi /f 1.0 min ;
 | 
			
		||||
 | 
			
		||||
CONSTANT: MAX-FRAMES-TO-SKIP 5
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -49,12 +49,12 @@ TUPLE: game-loop-error game-loop error ;
 | 
			
		|||
 | 
			
		||||
: increment-tick ( loop -- )
 | 
			
		||||
    [ 1 + ] change-tick-number
 | 
			
		||||
    dup tick-length>> [ + ] curry change-last-tick
 | 
			
		||||
    dup tick-interval-micros>> [ + ] curry change-last-tick
 | 
			
		||||
    drop ;
 | 
			
		||||
 | 
			
		||||
: ?tick ( loop count -- )
 | 
			
		||||
    [ system-micros >>last-tick drop ] [
 | 
			
		||||
        over [ since-last-tick ] [ tick-length>> ] bi >=
 | 
			
		||||
        over [ since-last-tick ] [ tick-interval-micros>> ] bi >=
 | 
			
		||||
        [ [ drop increment-tick ] [ drop tick ] [ 1 - ?tick ] 2tri ]
 | 
			
		||||
        [ 2drop ] if
 | 
			
		||||
    ] if-zero ;
 | 
			
		||||
| 
						 | 
				
			
			@ -97,7 +97,7 @@ PRIVATE>
 | 
			
		|||
    f >>thread
 | 
			
		||||
    drop ;
 | 
			
		||||
 | 
			
		||||
: <game-loop> ( tick-length delegate -- loop )
 | 
			
		||||
: <game-loop> ( tick-interval-micros delegate -- loop )
 | 
			
		||||
    system-micros f f 0 0 system-micros 0 0
 | 
			
		||||
    game-loop boa ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,14 +6,14 @@ TUPLE: game-world < world
 | 
			
		|||
    game-loop
 | 
			
		||||
    { tick-slice float initial: 0.0 } ;
 | 
			
		||||
 | 
			
		||||
GENERIC: tick-length ( world -- micros )
 | 
			
		||||
GENERIC: tick-interval-micros ( world -- micros )
 | 
			
		||||
 | 
			
		||||
M: game-world draw*
 | 
			
		||||
    swap >>tick-slice relayout-1 yield ;
 | 
			
		||||
 | 
			
		||||
M: game-world begin-world
 | 
			
		||||
    open-game-input 
 | 
			
		||||
    dup [ tick-length ] [ ] bi <game-loop> [ >>game-loop ] keep start-loop
 | 
			
		||||
    dup [ tick-interval-micros ] [ ] bi <game-loop> [ >>game-loop ] keep start-loop
 | 
			
		||||
    drop ;
 | 
			
		||||
 | 
			
		||||
M: game-world end-world
 | 
			
		||||
| 
						 | 
				
			
			@ -21,3 +21,6 @@ M: game-world end-world
 | 
			
		|||
    close-game-input
 | 
			
		||||
    drop ;
 | 
			
		||||
 | 
			
		||||
TUPLE: game-attributes < world-attributes
 | 
			
		||||
    { tick-interval-micros fixnum read-only } ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -294,7 +294,7 @@ AFTER: bunny-world resize-world
 | 
			
		|||
    [ sobel>> framebuffer>> ] [ dim>> ] bi resize-framebuffer ;
 | 
			
		||||
 | 
			
		||||
M: bunny-world pref-dim* drop { 1024 768 } ;
 | 
			
		||||
M: bunny-world tick-length drop 1000000 60 /i ;
 | 
			
		||||
M: bunny-world tick-interval-micros drop 1000000 60 /i ;
 | 
			
		||||
M: bunny-world wasd-movement-speed drop 1/160. ;
 | 
			
		||||
M: bunny-world wasd-near-plane drop 1/32. ;
 | 
			
		||||
M: bunny-world wasd-far-plane drop 256.0 ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -93,7 +93,7 @@ M: raytrace-world draw-world*
 | 
			
		|||
    } <render-set> render ;
 | 
			
		||||
 | 
			
		||||
M: raytrace-world pref-dim* drop { 1024 768 } ;
 | 
			
		||||
M: raytrace-world tick-length drop 1000000 60 /i ;
 | 
			
		||||
M: raytrace-world tick-interval-micros drop 1000000 60 /i ;
 | 
			
		||||
M: raytrace-world wasd-movement-speed drop 1/4. ;
 | 
			
		||||
 | 
			
		||||
: raytrace-window ( -- )
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -55,7 +55,7 @@ TUPLE: terrain-world < game-world
 | 
			
		|||
        float-4{ 0.0 0.0 0.0 1.0 } >>velocity
 | 
			
		||||
        VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
 | 
			
		||||
 | 
			
		||||
M: terrain-world tick-length
 | 
			
		||||
M: terrain-world tick-interval-micros
 | 
			
		||||
    drop 1000000 60 /i ;
 | 
			
		||||
 | 
			
		||||
: frustum ( dim -- -x x -y y near far )
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue