summaryrefslogtreecommitdiff
path: root/2d
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@member.fsf.org>2014-04-24 21:57:50 -0400
committerDavid Thompson <dthompson@member.fsf.org>2014-04-24 21:57:50 -0400
commitc9bb5b91b82900294413786f75b6ef021a59bb5a (patch)
tree45b612a76ec1d90db4a26b496b2ba2c8ae972ac8 /2d
parent42b7951d5a5104b16b1dd547fc98130b6232ca04 (diff)
Rename game loop procedures and remove parameter.
* 2d/game.scm (tick-interval): No longer a parameter. (run-game-loop, start-game-loop): Rename. (quit-game, stop-game-loop): Rename. * examples/animation.scm: Use start-game-loop and stop-game-loop. * examples/common.scm: Use start-game-loop and stop-game-loop. * examples/coroutine.scm: Use start-game-loop and stop-game-loop. * examples/font.scm: Use start-game-loop and stop-game-loop. * examples/guile-2048/guile-2048.scm: Use start-game-loop and stop-game-loop. * examples/particles.scm: Use start-game-loop and stop-game-loop. * examples/simple.scm: Use start-game-loop and stop-game-loop. * examples/tilemap.scm: Use start-game-loop and stop-game-loop.
Diffstat (limited to '2d')
-rw-r--r--2d/game.scm43
1 files changed, 21 insertions, 22 deletions
diff --git a/2d/game.scm b/2d/game.scm
index 14543e8..b25cd1d 100644
--- a/2d/game.scm
+++ b/2d/game.scm
@@ -29,33 +29,21 @@
#:use-module (2d event)
#:use-module (2d signal)
#:use-module (2d window)
- #:export (ticks-per-second
- tick-interval
+ #:export (tick-interval
game-agenda
draw-hook
- run-game-loop
- quit-game))
+ start-game-loop
+ stop-game-loop))
;;;
;;; Game Loop
;;;
-(define ticks-per-second 60)
-(define tick-interval (make-parameter 0))
+;; Update 60 times per second by default.
+(define tick-interval (floor (/ 1000 60)))
(define draw-hook (make-hook 2))
(define game-agenda (make-agenda))
-(define (run-game-loop)
- "Start the game loop."
- (parameterize ((tick-interval (floor (/ 1000 ticks-per-second))))
- (call-with-prompt
- 'game-loop-prompt
- (lambda ()
- (game-loop (SDL:get-ticks) 0))
- (lambda (cont callback)
- (when (procedure? callback)
- (callback cont))))))
-
(define (draw dt alpha)
"Render a frame."
(let ((width (signal-ref window-width))
@@ -69,20 +57,20 @@
"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))
+ (if (>= lag tick-interval)
(begin
(tick-agenda! game-agenda)
- (update (- lag (tick-interval))))
+ (update (- lag tick-interval)))
lag))
(define (alpha lag)
"Calculate interpolation factor in the range [0, 1] for the
leftover frame time LAG."
- (/ lag (tick-interval)))
+ (/ lag tick-interval))
(define (frame-sleep time)
"Sleep for the remainder of the frame that started at TIME."
- (let ((t (- (+ time (tick-interval))
+ (let ((t (- (+ time tick-interval)
(SDL:get-ticks))))
(usleep (max 0 (* t 1000)))))
@@ -97,5 +85,16 @@ milliseconds of the last iteration of the game loop."
(frame-sleep current-time)
(game-loop current-time lag))))
-(define (quit-game)
+(define (start-game-loop)
+ "Start the game loop."
+ (call-with-prompt
+ 'game-loop-prompt
+ (lambda ()
+ (game-loop (SDL:get-ticks) 0))
+ (lambda (cont callback)
+ (when (procedure? callback)
+ (callback cont)))))
+
+(define (stop-game-loop)
+ "Abort the game loop."
(abort-to-prompt 'game-loop-prompt #f))