diff options
author | David Thompson <dthompson2@worcester.edu> | 2013-07-31 23:51:23 -0400 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2013-07-31 23:51:23 -0400 |
commit | 7805e74c68d7efca9d4419724747abf23286f457 (patch) | |
tree | af8ba52ea14f26ef154e454f969f2862862a81c0 /2d | |
parent | a3aa623daff26b34aa7e7eed656e73c236213351 (diff) |
Add a global mutex that allows the REPL server to be used more safely while the game loop is running.
Diffstat (limited to '2d')
-rw-r--r-- | 2d/game-loop.scm | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/2d/game-loop.scm b/2d/game-loop.scm index 199cb80..431ddf7 100644 --- a/2d/game-loop.scm +++ b/2d/game-loop.scm @@ -52,6 +52,22 @@ ;;; (define *fps* 0) +(define game-loop-mutex (make-mutex 'unchecked-unlock)) + +;;; +;;; REPL Hooks +;;; + +;; Lock game loop mutex before evaluating code from REPL server and +;; unlock it afterwards. +(add-hook! before-eval-hook + (lambda (exp) + (lock-mutex game-loop-mutex))) + +(add-hook! after-eval-hook + (lambda (exp) + (when (equal? (mutex-owner game-loop-mutex) (current-thread)) + (unlock-mutex game-loop-mutex)))) ;;; ;;; Hooks @@ -180,8 +196,10 @@ 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) (game-loop time (+ next-time tick-interval) remainder))) @@ -189,6 +207,7 @@ is the unused accumulator time." (define (run-game-loop) "Spawns a REPL server and starts the main game loop." (spawn-server) + (lock-mutex game-loop-mutex) (agenda-schedule show-fps) (let ((time (SDL:get-ticks))) (game-loop time (+ time tick-interval) 0))) |