summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2013-10-07 21:39:48 -0400
committerDavid Thompson <dthompson2@worcester.edu>2013-10-23 19:44:51 -0400
commitbba3d11def443f4b3452724925d0551cf9e8f8ee (patch)
treed8e831fe0284bafa5d2c32d50e346d478c2da940
parent2aab93ed1d2e675dc4ec12386a6a7f9ebc3a8db7 (diff)
Add a paused flag in addition to the running flag.
-rw-r--r--2d/game-loop.scm35
-rw-r--r--2d/game.scm4
2 files changed, 30 insertions, 9 deletions
diff --git a/2d/game-loop.scm b/2d/game-loop.scm
index 0337db0..168f6f7 100644
--- a/2d/game-loop.scm
+++ b/2d/game-loop.scm
@@ -39,7 +39,11 @@
replace-scene
pop-scene
run-game
- quit-game-loop!))
+ quit-game
+ pause-game
+ resume-game
+ game-running?
+ game-paused?))
;;;
;;; Constants
@@ -53,7 +57,6 @@
;;;
(define game-fps 0)
-(define running #f)
;;;
;;; Event Handling
@@ -206,7 +209,7 @@ INPUT, OUTPUT, and ERROR ports."
"Exit the current scene and resume the previous scene. If there is
no previous scene, the game loop will terminate."
(if (null? scenes)
- (quit-game-loop!)
+ (quit-game)
(begin
(set! next-scene (car scenes))
(set! scenes (cdr scenes)))))
@@ -231,11 +234,14 @@ the stack."
;;; Game Loop
;;;
+(define running? #f)
+(define paused? #f)
+
(define (game-loop last-time accumulator)
"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."
- (when running
+ (when running?
(let* ((current-time (SDL:get-ticks))
(dt (- current-time last-time))
(remainder (update (+ accumulator dt))))
@@ -250,12 +256,27 @@ time in milliseconds that has passed since the last game update."
(open-window (game-title game)
(game-resolution game)
(game-fullscreen? game))
- (set! running #t)
+ (set! running? #t)
+ (resume-game)
(set-initial-scene ((game-first-scene game)))
(spawn-server)
(game-loop (SDL:get-ticks) 0)
(close-window))
-(define (quit-game-loop!)
+(define (game-running?)
+ (running?))
+
+(define (game-paused?)
+ (paused?))
+
+(define (pause-game)
+ "Pauses the game loop. Useful when developing."
+ (set! paused? #t))
+
+(define (resume-game)
+ "Resumes the game loop."
+ (set! paused? #f))
+
+(define (quit-game)
"Finish the current frame and terminate the game loop."
- (set! running #f))
+ (set! running? #f))
diff --git a/2d/game.scm b/2d/game.scm
index 3bd314e..7bb0823 100644
--- a/2d/game.scm
+++ b/2d/game.scm
@@ -35,10 +35,10 @@
;; When no event callbacks are specified for a scene, these
;; (hopefully) convenient defaults will be used.
(define %default-scene-events
- `((quit . ,(lambda (state) (quit-game-loop!)))
+ `((quit . ,(lambda (state) (quit-game)))
(key-down . ,(lambda (state key mode unicode)
(when (any-equal? key 'escape 'q)
- (quit-game-loop!))))))
+ (quit-game))))))
(define (default-scene-events)
(copy-tree %default-scene-events))