IBuffered protocol we snapshot and resture on exception

This commit is contained in:
Joseph Ferano 2026-09-05 08:30:51 +07:00
parent c301cd9037
commit 8eb9a8b7f9

View File

@ -4,11 +4,32 @@
[rl :as rl] [rl :as rl]
[watch :as w])) [watch :as w]))
(defprotocol IBuffered
(cells [this])
(snapshot! [this])
(restore! [this]))
(deftype IntGrid [^ints cells ^ints backup]
IBuffered
(cells [_] cells)
(snapshot! [_] (System/arraycopy cells 0 backup 0 (alength cells)))
(restore! [_] (System/arraycopy backup 0 cells 0 (alength cells))))
(deftype RawIntGrid [^ints cells]
IBuffered
(cells [_] cells)
(snapshot! [_])
(restore! [_]))
(defonce last-error (atom nil)) (defonce last-error (atom nil))
(defonce game-state (atom nil)) (defonce game-state (atom nil))
(defonce game-config (atom nil)) (defonce game-config (atom nil))
(defonce texture-cache (atom {})) (defonce texture-cache (atom {}))
;; (reset! texture-cache {}) ;; (reset! texture-cache {})
(defonce buffered-objs (atom []))
;; TODO: Setup release flags and get this to work with make-int-grid
;; (def debug? (not= "false" (System/getProperty "siam.debug")))
(defn load-texture-once [id path] (defn load-texture-once [id path]
(or (@texture-cache path) (or (@texture-cache path)
@ -16,11 +37,10 @@
(swap! texture-cache assoc path t) (swap! texture-cache assoc path t)
t))) t)))
(defn load-texture-once [id path] (defn make-int-grid [n empty-val]
(or (@texture-cache path) (let [g (->IntGrid (int-array n empty-val) (int-array n empty-val))]
(let [t (assoc (rl/load-texture path) :name id)] (swap! buffered-objs conj g)
(swap! texture-cache assoc path t) g))
t)))
(defn report-exception! (defn report-exception!
"Stash the exception and print a marker line. Emacs watches the REPL output "Stash the exception and print a marker line. Emacs watches the REPL output
@ -59,16 +79,22 @@
(report-exception! t))) (report-exception! t)))
(while (not (rl/window-should-close?)) (while (not (rl/window-should-close?))
(if @last-error (if @last-error
(Thread/sleep 50) (do (rl/with-drawing!)
(Thread/sleep 50))
(let [config (:config @game-config)] (let [config (:config @game-config)]
(try
(doseq [buf @buffered-objs] (snapshot! buf))
(swap! game-state #(update config %))
(catch Throwable t
(doseq [buf @buffered-objs]
(restore! buf))
(report-exception! t)))
(rl/with-drawing! (rl/with-drawing!
(try (try
(swap! game-state #(update config %))
(draw config @game-state) (draw config @game-state)
(w/watch! (watch-fn config @game-state)) (w/watch! (watch-fn config @game-state))
(catch Throwable t (catch Throwable t
(report-exception! t) (report-exception! t)))))))
(w/watch! (merge (watch-fn config @game-state) {:error t}))))))))
(try (try
(when unload (when unload
(doseq [[_ tex] @texture-cache] (doseq [[_ tex] @texture-cache]
@ -77,6 +103,7 @@
(catch Throwable t (catch Throwable t
(report-exception! t))) (report-exception! t)))
(rl/close-window!) (rl/close-window!)
(reset! buffered-objs [])
(reset! texture-cache {}) (reset! texture-cache {})
(reset! last-error nil) (reset! last-error nil)
(reset! game-state nil) (reset! game-state nil)