diff options
author | David Thompson <dthompson@member.fsf.org> | 2014-04-24 21:46:17 -0400 |
---|---|---|
committer | David Thompson <dthompson@member.fsf.org> | 2014-04-24 21:46:17 -0400 |
commit | 42b7951d5a5104b16b1dd547fc98130b6232ca04 (patch) | |
tree | ee76c13f0537d534d8f59a4a5523ccbc3073beae | |
parent | 257b4c2498640dc31c3d807e04c1637cf5ba4967 (diff) |
Sleep when the game loop runs too fast.
* 2d/game.scm (frame-sleep): New procedure.
(game-loop): Use frame-sleep.
-rw-r--r-- | 2d/game.scm | 9 |
1 files changed, 9 insertions, 0 deletions
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) |