Engine: Check if window already exists, fix load texture and add window-ready?

This commit is contained in:
Joseph Ferano 2026-08-23 07:41:12 +07:00
parent 93e8ea751f
commit 2186777587
2 changed files with 42 additions and 38 deletions

View File

@ -20,37 +20,40 @@
(defn run-game! (defn run-game!
[{:keys [title width height config-path init input update draw unload watch-fn] [{:keys [title width height config-path init input update draw unload watch-fn]
:or {title "Untitled" width 1200 height 900 watch-fn (constantly nil)}}] :or {title "Untitled" width 1200 height 900 watch-fn (constantly nil)}}]
(rl/set-trace-log-level! rl/log-warning) (if (rl/window-ready?)
(rl/init-window! width height title) (println "Warning: Existing raylib window open, ignoring!")
(rl/set-target-fps! 60) (do
(let [config (if config-path (rl/set-trace-log-level! rl/log-warning)
(edn/read-string (slurp config-path)) (rl/init-window! width height title)
{})] (rl/set-target-fps! 60)
(reset! game-config {:config-path config-path :config config}) (let [config (if config-path
(try (edn/read-string (slurp config-path))
(reset! game-state (init config)) {})]
(catch Throwable t (reset! game-config {:config-path config-path :config config})
(println t))) (try
(while (not (rl/window-should-close?)) (reset! game-state (init config))
(if @last-error (catch Throwable t
(Thread/sleep 50) (println t)))
(let [config (:config @game-config)] (while (not (rl/window-should-close?))
(rl/with-drawing! (if @last-error
(try (Thread/sleep 50)
(swap! game-state #(input config %)) (let [config (:config @game-config)]
(swap! game-state #(update config %)) (rl/with-drawing!
(draw config @game-state) (try
(w/watch! (watch-fn config @game-state)) (swap! game-state #(input config %))
(catch Throwable t (swap! game-state #(update config %))
(println t) (draw config @game-state)
(reset! last-error t) (w/watch! (watch-fn config @game-state))
(w/watch! (merge (watch-fn config @game-state) {:error t})))))))) (catch Throwable t
(try (println t)
(when unload (reset! last-error t)
(unload @game-state)) (w/watch! (merge (watch-fn config @game-state) {:error t}))))))))
(catch Throwable t (try
(println t))) (when unload
(rl/close-window!) (unload @game-state))
(reset! last-error nil) (catch Throwable t
(reset! game-state nil) (println t)))
(reset! game-config nil))) (rl/close-window!)
(reset! last-error nil)
(reset! game-state nil)
(reset! game-config nil)))))

View File

@ -179,6 +179,7 @@
(defcfn set-target-fps! "SetTargetFPS" [::mem/int] ::mem/void) (defcfn set-target-fps! "SetTargetFPS" [::mem/int] ::mem/void)
(defcfn set-trace-log-level! "SetTraceLogLevel" [::mem/int] ::mem/void) (defcfn set-trace-log-level! "SetTraceLogLevel" [::mem/int] ::mem/void)
(defcfn window-should-close? "WindowShouldClose" [] ::bool) (defcfn window-should-close? "WindowShouldClose" [] ::bool)
(defcfn window-ready? "IsWindowReady" [] ::bool)
(defcfn begin-drawing! "BeginDrawing" [] ::mem/void) (defcfn begin-drawing! "BeginDrawing" [] ::mem/void)
(defcfn end-drawing! "EndDrawing" [] ::mem/void) (defcfn end-drawing! "EndDrawing" [] ::mem/void)
@ -259,10 +260,6 @@
(defcfn measure-text "MeasureText" [::mem/c-string ::mem/int] ::mem/int) (defcfn measure-text "MeasureText" [::mem/c-string ::mem/int] ::mem/int)
(defn load-texture [path]
(let [tex (load-texture-raw* path)]
(assoc tex :seg (texture-seg tex))))
(def ^:private draw-text-raw!* (def ^:private draw-text-raw!*
(ffi/make-downcall "DrawText" (ffi/make-downcall "DrawText"
[::mem/c-string ::mem/int ::mem/int ::mem/int ::color] ::mem/void)) [::mem/c-string ::mem/int ::mem/int ::mem/int ::color] ::mem/void))
@ -343,6 +340,10 @@
(defn texture-seg [tex] (defn texture-seg [tex]
(mem/serialize tex ::texture arena)) (mem/serialize tex ::texture arena))
(defn load-texture [path]
(let [tex (load-texture-raw* path)]
(assoc tex :seg (texture-seg tex))))
(defn n-patch-info-seg [{:keys [source left top right bottom layout]}] (defn n-patch-info-seg [{:keys [source left top right bottom layout]}]
(mem/serialize {:source source :left (int left) :top (int top) (mem/serialize {:source source :left (int left) :top (int top)
:right (int right) :bottom (int bottom) :layout (int layout)} :right (int right) :bottom (int bottom) :layout (int layout)}