From 257b4c2498640dc31c3d807e04c1637cf5ba4967 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 24 Apr 2014 21:22:06 -0400 Subject: Run game loop with a prompt. * 2d/game.scm (game-loop-status, game-running?, game-paused?) (pause-game, resume-game, paused-agenda): Delete. (run-game-loop): Call game loop with prompt. (quit-game): Abort to game loop prompt. --- 2d/game.scm | 63 ++++++++++++++++--------------------------------------------- 1 file changed, 16 insertions(+), 47 deletions(-) (limited to '2d/game.scm') diff --git a/2d/game.scm b/2d/game.scm index 770d3b1..7f24193 100644 --- a/2d/game.scm +++ b/2d/game.scm @@ -32,38 +32,29 @@ #:export (ticks-per-second tick-interval game-agenda - paused-agenda draw-hook run-game-loop - quit-game - pause-game - resume-game - game-running? - game-paused?)) + quit-game)) ;;; ;;; Game Loop ;;; -;; Possible states are: -;; * stopped -;; * running -;; * paused -(define game-loop-status (make-parameter 'stopped)) (define ticks-per-second 60) (define tick-interval (make-parameter 0)) (define draw-hook (make-hook 2)) (define game-agenda (make-agenda)) -;; This agenda is only ticked when the game loop is in the paused -;; state. Useful for things like the REPL that should be run even -;; though the game is paused. -(define paused-agenda (make-agenda)) (define (run-game-loop) "Start the game loop." - (parameterize ((game-loop-status 'running) - (tick-interval (floor (/ 1000 ticks-per-second)))) - (game-loop (SDL:get-ticks) 0))) + (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." @@ -90,34 +81,12 @@ is the unused accumulator time." (define (game-loop previous-time lag) "Update game state, and render. PREVIOUS-TIME is the time in milliseconds of the last iteration of the game loop." - (when (game-running?) - (let* ((current-time (SDL:get-ticks)) - (dt (- current-time previous-time))) - (process-events) - (let ((lag (update (+ lag dt)))) - (draw dt (alpha lag)) - (game-loop current-time lag))))) - -;;; -;;; State management -;;; - -(define (game-running?) - (or (eq? (game-loop-status) 'running) - (eq? (game-loop-status) 'paused))) - -(define (game-paused?) - (eq? (game-loop-status) 'paused)) - -(define (pause-game) - "Pauses the game loop. Useful when developing." - (game-loop-status 'paused)) - -(define (resume-game) - "Resumes the game loop." - (when (game-paused?) - (game-loop-status 'running))) + (let* ((current-time (SDL:get-ticks)) + (dt (- current-time previous-time))) + (process-events) + (let ((lag (update (+ lag dt)))) + (draw dt (alpha lag)) + (game-loop current-time lag)))) (define (quit-game) - "Finish the current frame and terminate the game loop." - (game-loop-status 'stopped)) + (abort-to-prompt 'game-loop-prompt #f)) -- cgit v1.2.3