summaryrefslogtreecommitdiff
path: root/2d
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-04-27 20:31:17 -0400
committerDavid Thompson <dthompson2@worcester.edu>2014-04-27 20:31:17 -0400
commitff4799b1c8800d35d4a06230b0782ca6af7708d0 (patch)
tree93701509b9acc4be97f3c5d814103854546923fd /2d
parent00f185843b3e6a4a1d58b48bcaebae291b83179d (diff)
Add max-ticks-per-frame variable.
* 2d/game.scm (max-ticks-per-frame): New variable (update): Limit ticks to the value of max-ticks-per-frame.
Diffstat (limited to '2d')
-rw-r--r--2d/game.scm20
1 files changed, 15 insertions, 5 deletions
diff --git a/2d/game.scm b/2d/game.scm
index d8eeb9b..ac33881 100644
--- a/2d/game.scm
+++ b/2d/game.scm
@@ -30,6 +30,7 @@
#:use-module (2d signal)
#:use-module (2d window)
#:export (tick-interval
+ max-ticks-per-frame
game-agenda
draw-hook
start-game-loop
@@ -41,6 +42,11 @@
;; Update 60 times per second by default.
(define tick-interval (floor (/ 1000 60)))
+;; The maximum number of times the game loop will update game state in
+;; a single frame. When this upper bound is reached due to poor
+;; performance, the game will start to slow down instead of becoming
+;; completely unresponsive and possibly crashing.
+(define max-ticks-per-frame 4)
(define draw-hook (make-hook 2))
(define game-agenda (make-agenda))
@@ -57,11 +63,15 @@
"Call the update callback. The update callback will be called as
many times as tick-interval can divide LAG. The return value
is the unused accumulator time."
- (if (>= lag tick-interval)
- (begin
- (tick-agenda! game-agenda)
- (update (- lag tick-interval)))
- lag))
+ (define (iter ticks)
+ (cond ((>= ticks max-ticks-per-frame)
+ lag)
+ ((>= lag tick-interval)
+ (tick-agenda! game-agenda)
+ (update (- lag tick-interval)))
+ (else
+ lag)))
+ (iter 0))
(define (alpha lag)
"Calculate interpolation factor in the range [0, 1] for the