From 75f7831673f533ab33e162c869be5ddd48fbf91b Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 9 Jan 2014 07:42:39 -0500 Subject: Move FPS to a module and add accumulator parameter. * 2d/game.scm (accumulator): New parameter. (draw-hook): Change to arity 2. (current-fps, accumulate-fps!, game-fps): Delete. (game-loop, update-and-render): Drop accumulator formal parameter. * 2d/fps.scm: New module. --- 2d/game.scm | 87 +++++++++++++++++++++---------------------------------------- 1 file changed, 30 insertions(+), 57 deletions(-) diff --git a/2d/game.scm b/2d/game.scm index b6be399..00e5129 100644 --- a/2d/game.scm +++ b/2d/game.scm @@ -43,8 +43,7 @@ resume-game game-running? game-paused? - register-event-handler - current-fps)) + register-event-handler)) ;;; ;;; Game Loop @@ -57,43 +56,44 @@ (define game-loop-status (make-parameter 'stopped)) (define ticks-per-second 60) (define tick-interval (make-parameter 0)) -(define draw-hook (make-hook)) +(define draw-hook (make-hook 2)) +(define accumulator (make-parameter 0)) (define (run-game-loop) "Start the game loop." (parameterize ((game-loop-status 'running) - (tick-interval (floor (/ 1000 ticks-per-second)))) + (tick-interval (floor (/ 1000 ticks-per-second))) + (accumulator 0)) (resume-game) (spawn-server) - (game-loop (SDL:get-ticks) 0))) + (game-loop (SDL:get-ticks)))) (define (draw dt alpha) "Render a frame." (set-gl-matrix-mode (matrix-mode modelview)) (gl-load-identity) (gl-clear (clear-buffer-mask color-buffer depth-buffer)) - (run-hook draw-hook) - (SDL:gl-swap-buffers) - (accumulate-fps! dt)) + (run-hook draw-hook dt alpha) + (SDL:gl-swap-buffers)) -(define (update accumulator) +(define (update) "Call the update callback. The update callback will be called as many times as `tick-interval` can divide ACCUMULATOR. The return value is the unused accumulator time." - (if (>= accumulator (tick-interval)) - (begin - (read-input) - (tick-agenda!) - (update (- accumulator (tick-interval)))) - accumulator)) - -(define (update-and-render dt accumulator) - (let ((remainder (update accumulator))) - (run-repl) - (draw dt (/ remainder (tick-interval))) - remainder)) - -(define (tick dt accumulator) + (while (>= (accumulator) (tick-interval)) + (read-input) + (tick-agenda!) + (accumulator (- (accumulator) (tick-interval))))) + +(define (alpha) + (/ (accumulator) (tick-interval))) + +(define (update-and-render dt) + (update) + (run-repl) + (draw dt (alpha))) + +(define (tick dt) "Advance the game by one frame." (if (game-paused?) (begin @@ -102,7 +102,7 @@ is the unused accumulator time." accumulator) (catch #t (lambda () - (update-and-render dt accumulator)) + (update-and-render dt)) (lambda (key . args) (pause-game) accumulator) @@ -110,15 +110,15 @@ is the unused accumulator time." (display-backtrace (make-stack #t) (current-output-port)))))) -(define (game-loop last-time accumulator) +(define (game-loop last-time) "Update game state, and render. LAST-TIME is the time in -milliseconds of the last iteration of the loop. ACCUMULATOR is the -time in milliseconds that has passed since the last game update." +milliseconds of the last iteration of the loop." (when (game-running?) (let* ((current-time (SDL:get-ticks)) - (dt (- current-time last-time)) - (accumulator (+ accumulator dt))) - (game-loop current-time (tick dt accumulator))))) + (dt (- current-time last-time))) + (accumulator (+ (accumulator) dt)) + (tick dt) + (game-loop current-time)))) ;;; ;;; State management @@ -166,33 +166,6 @@ time in milliseconds that has passed since the last game update." (when handle ((car handle) e)))) -;;; -;;; Frames Per Second -;;; - -(define game-fps 0) - -(define accumulate-fps! - (let* ((elapsed-time 0) - (fps 0)) - (lambda (dt) - "Increment the frames-per-second counter. Resets to 0 every -second." - (let ((new-time (+ elapsed-time dt)) - (new-fps (1+ fps))) - (if (>= new-time 1000) - (begin - (set! game-fps new-fps) - (set! fps 0) - (set! elapsed-time 0)) - (begin - (set! fps new-fps) - (set! elapsed-time new-time))))))) - -(define (current-fps) - "Return the current FPS value." - game-fps) - ;;; ;;; REPL ;;; -- cgit v1.2.3