From 42b7951d5a5104b16b1dd547fc98130b6232ca04 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 24 Apr 2014 21:46:17 -0400 Subject: Sleep when the game loop runs too fast. * 2d/game.scm (frame-sleep): New procedure. (game-loop): Use frame-sleep. --- 2d/game.scm | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/2d/game.scm b/2d/game.scm index 7f24193..14543e8 100644 --- a/2d/game.scm +++ b/2d/game.scm @@ -76,8 +76,16 @@ is the unused accumulator time." lag)) (define (alpha lag) + "Calculate interpolation factor in the range [0, 1] for the +leftover frame time LAG." (/ lag (tick-interval))) +(define (frame-sleep time) + "Sleep for the remainder of the frame that started at TIME." + (let ((t (- (+ time (tick-interval)) + (SDL:get-ticks)))) + (usleep (max 0 (* t 1000))))) + (define (game-loop previous-time lag) "Update game state, and render. PREVIOUS-TIME is the time in milliseconds of the last iteration of the game loop." @@ -86,6 +94,7 @@ milliseconds of the last iteration of the game loop." (process-events) (let ((lag (update (+ lag dt)))) (draw dt (alpha lag)) + (frame-sleep current-time) (game-loop current-time lag)))) (define (quit-game) -- cgit v1.2.3