summaryrefslogtreecommitdiff
path: root/2d
diff options
context:
space:
mode:
authorDavid Thompson <dthompson@member.fsf.org>2013-08-19 19:15:37 -0400
committerDavid Thompson <dthompson@member.fsf.org>2013-08-19 19:15:37 -0400
commit3950c90856f79d4420bffb714e2e4d1ab0826612 (patch)
treeb92171c9e51cf42fcc93744aa89ff9e6dfe6a6c4 /2d
parentb5809b7c7841ae236f6bfff8c3eeebe8e3b080db (diff)
Only unlock the game loop mutex when the REPL server is waiting.
Diffstat (limited to '2d')
-rw-r--r--2d/game-loop.scm20
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)))