summaryrefslogtreecommitdiff
path: root/2d
diff options
context:
space:
mode:
authorDavid Thompson <dave@izanagi>2013-06-23 15:14:48 -0400
committerDavid Thompson <dave@izanagi>2013-06-23 15:14:48 -0400
commita6c41ce1fe0ade2edaf37f7cbd12d4c307b1cd55 (patch)
treef630c6528da8a51976c0b7886e1a9d9337fb560d /2d
parent810fbc96fcb2aad1113ba42cb43174b5fa50a9e0 (diff)
Improve update loop logic.
Diffstat (limited to '2d')
-rw-r--r--2d/game-loop.scm29
1 files changed, 21 insertions, 8 deletions
diff --git a/2d/game-loop.scm b/2d/game-loop.scm
index 4b097ce..f2faf13 100644
--- a/2d/game-loop.scm
+++ b/2d/game-loop.scm
@@ -31,6 +31,7 @@
run-game-loop))
(define target-fps 60)
+(define frame-interval (/ 1000 target-fps))
;;;
;;; Callbacks
@@ -100,16 +101,28 @@
(set! last-time time)
(set! fps 0))))))
+(define (update accumulator)
+ "Call the update callback. The update callback will be called as
+many times as frame-interval can divide accumulator. The return value
+is the unused accumulator time."
+ (if (>= accumulator frame-interval)
+ (begin
+ (update-callback)
+ (update (- accumulator frame-interval)))
+ accumulator))
+
(define update-and-render
- (let ((last-update 0)
- (update-interval (/ 1000 target-fps)))
+ (let ((remainder 0)
+ (last-time 0))
(lambda ()
- "Calls update and draw callback when enough time has passed since
-the last tick."
- (let ((time (SDL:get-ticks)))
- (when (>= time (+ last-update update-interval))
- (set! last-update time)
- (update-callback)
+ "Calls update and draw callback when enough time has passed
+since the last tick."
+ (let* ((time (SDL:get-ticks))
+ (elapsed (- time last-time))
+ (accumulator (+ remainder elapsed)))
+ (when (>= accumulator frame-interval)
+ (set! last-time time)
+ (set! remainder (update accumulator))
(accumulate-fps)
(render))))))