diff --git a/src/engine.clj b/src/engine.clj index e7e84eb..0a46bcb 100644 --- a/src/engine.clj +++ b/src/engine.clj @@ -6,15 +6,10 @@ (defonce ^:private last-error (atom nil)) -(reset! last-error (ex-info "something bad happened" {:reason "why"})) -(reset! last-error nil) - -(defn- draw-error! [err] - (rl/clear-background!* rl/cornflower-blue) - (rl/draw-rectangle!* 10 10 300 30 (rl/color-seg 0xFF0000)) - (rl/draw-text!* "Fix the code then left click anywhere with the mouse" 0 10 16 rl/white) - #_(rl/draw-text!* (str err) 50 100 20 rl/white) - (rl/draw-text!* "Error" 50 50 40 rl/white)) +(defn clear-error! + "Call from the REPL to resume the loop after an error froze it." + [] + (reset! last-error nil)) (defn run-game! [{:keys [title width height config-path init input update draw watch-fn] @@ -24,19 +19,16 @@ (rl/set-target-fps! 60) (let [state (atom (init (edn/read-string (slurp config-path))))] (while (not (rl/window-should-close?)) - (rl/with-drawing! - (if @last-error - (do - (draw-error! @last-error) - (when (rl/mouse-button-released? rl/mouse-button-left) - (reset! last-error nil))) + (if @last-error + (Thread/sleep 50) + (rl/with-drawing! (try (swap! state input) (swap! state update) (draw @state) (w/watch! (watch-fn @state)) - ;; TODO: Have Cider report this exception (catch Throwable t (println t) - (reset! last-error t)))))) + (reset! last-error t) + (w/watch! (merge (watch-fn @state) {:error t}))))))) (rl/close-window!))) diff --git a/src/rl.clj b/src/rl.clj index fc09863..bc7a643 100644 --- a/src/rl.clj +++ b/src/rl.clj @@ -120,12 +120,14 @@ ;;; --------------------------------------------------- macros -(defmacro with-drawing! [& body] +(defmacro with-drawing! + [& body] `(do + (begin-drawing!) (try - (begin-drawing!) ~@body - (end-drawing!)))) + (finally + (end-drawing!))))) ;;; --------------------------------------------------- native value allocation