diff options
author | David Thompson <dthompson@member.fsf.org> | 2013-08-19 19:15:37 -0400 |
---|---|---|
committer | David Thompson <dthompson@member.fsf.org> | 2013-08-19 19:15:37 -0400 |
commit | 3950c90856f79d4420bffb714e2e4d1ab0826612 (patch) | |
tree | b92171c9e51cf42fcc93744aa89ff9e6dfe6a6c4 | |
parent | b5809b7c7841ae236f6bfff8c3eeebe8e3b080db (diff) |
Only unlock the game loop mutex when the REPL server is waiting.
-rw-r--r-- | 2d/game-loop.scm | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/2d/game-loop.scm b/2d/game-loop.scm index 431ddf7..53e1768 100644 --- a/2d/game-loop.scm +++ b/2d/game-loop.scm @@ -52,6 +52,9 @@ ;;; (define *fps* 0) +;; The REPL sets this flag when it needs to evaluate something. +;; Only the REPL server thread will mutate this variable. +(define *repl-waiting* #f) (define game-loop-mutex (make-mutex 'unchecked-unlock)) ;;; @@ -62,10 +65,12 @@ ;; unlock it afterwards. (add-hook! before-eval-hook (lambda (exp) + (set! *repl-waiting* #t) (lock-mutex game-loop-mutex))) (add-hook! after-eval-hook (lambda (exp) + (set! *repl-waiting* #f) (when (equal? (mutex-owner game-loop-mutex) (current-thread)) (unlock-mutex game-loop-mutex)))) @@ -183,6 +188,16 @@ is the unused accumulator time." (define (time-left current-time next-time) (max (floor (- next-time current-time)) 0)) +(define (frame-sleep time) + "Sleep for time milliseconds. Unlock the mutex beforehand if the +REPL server is waiting to evaluate something." + (if *repl-waiting* + (begin + (unlock-mutex game-loop-mutex) + (SDL:delay time) + (lock-mutex game-loop-mutex)) + (SDL:delay time))) + ;;; ;;; Game Loop ;;; @@ -196,10 +211,7 @@ is the unused accumulator time." (remainder (update accumulator))) (render) (accumulate-fps! dt) - (unlock-mutex game-loop-mutex) - ;; Sleep for a bit if there's time in between frames - (SDL:delay (time-left (SDL:get-ticks) next-time)) - (lock-mutex game-loop-mutex) + (frame-sleep (time-left (SDL:get-ticks) next-time)) (game-loop time (+ next-time tick-interval) remainder))) |