From 3950c90856f79d4420bffb714e2e4d1ab0826612 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 19 Aug 2013 19:15:37 -0400 Subject: Only unlock the game loop mutex when the REPL server is waiting. --- 2d/game-loop.scm | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to '2d') 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))) -- cgit v1.2.3