Don't draw an error, use watch system

This commit is contained in:
Joseph Ferano 2026-08-16 15:53:51 +07:00
parent 02359b3799
commit 7834ff7356
2 changed files with 14 additions and 20 deletions

View File

@ -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!)))

View File

@ -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