Fix error handling logic.
authorDavid Thompson <dthompson2@worcester.edu>
Sun, 26 Aug 2018 02:07:07 +0000 (22:07 -0400)
committerDavid Thompson <dthompson2@worcester.edu>
Sun, 26 Aug 2018 02:07:07 +0000 (22:07 -0400)
* chickadee.scm (call-with-error-handling): Call the correct error
handler. Return #t if an error occurred, #f otherwise.
(run-game): Reset current-time and buffer after recovering from an
error.

chickadee.scm

index 52cbcce..74faa56 100644 (file)
 (define (call-with-error-handling handler thunk)
   (let ((stack #f))
     (catch #t
-      thunk
+      (lambda ()
+        (thunk)
+        #f)
       (lambda (key . args)
-        (error stack key args))
+        (handler stack key args)
+        #t)
       (lambda (key . args)
         (set! stack (make-stack #t 3))))))
 
                  (delta (- current-time previous-time)))
             (let update-loop ((buffer (+ buffer delta)))
               (if (>= buffer timestep)
-                  (begin
-                    (with-error-handling error (update timestep))
-                    (update-loop (- buffer timestep)))
+                  ;; Short-circuit the update loop if an error
+                  ;; occurred, and reset the current time to now in
+                  ;; order to discard the undefined amount of time
+                  ;; that was spent handling the error.
+                  (if (with-error-handling error (update timestep))
+                      (loop (time) 0)
+                      (update-loop (- buffer timestep)))
                   (begin
                     ;; We render upon every iteration of the loop, and
                     ;; thus rendering is decoupled from updating.
                     ;; It's possible to render multiple times before
                     ;; an update is performed.
-                    (with-error-handling error (render (/ buffer timestep)))
-                    (loop current-time buffer)))))))
+                    (if (with-error-handling error (render (/ buffer timestep)))
+                        (loop (time) 0)
+                        (loop current-time buffer))))))))
       (lambda (cont callback)
         #f))))