diff options
-rw-r--r-- | 2d/game.scm | 63 |
1 files changed, 16 insertions, 47 deletions
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)) |