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!
[{:keys [title width height config-path init input update draw unload watch-fn]
:or {title "Untitled" width 1200 height 900 watch-fn (constantly nil)}}]
(rl/set-trace-log-level! rl/log-warning)
(rl/init-window! width height title)
(rl/set-target-fps! 60)
(let [config (if config-path
(edn/read-string (slurp config-path))
{})]
(reset! game-config {:config-path config-path :config config})
(try
(reset! game-state (init config))
(catch Throwable t
(println t)))
(while (not (rl/window-should-close?))
(if @last-error
(Thread/sleep 50)
(let [config (:config @game-config)]
(rl/with-drawing!
(try
(swap! game-state #(input config %))
(swap! game-state #(update config %))
(draw config @game-state)
(w/watch! (watch-fn config @game-state))
(catch Throwable t
(println t)
(reset! last-error t)
(w/watch! (merge (watch-fn config @game-state) {:error t}))))))))
(try
(when unload
(unload @game-state))
(catch Throwable t
(println t)))
(rl/close-window!)
(reset! last-error nil)
(reset! game-state nil)
(reset! game-config nil)))
(if (rl/window-ready?)
(println "Warning: Existing raylib window open, ignoring!")
(do
(rl/set-trace-log-level! rl/log-warning)
(rl/init-window! width height title)
(rl/set-target-fps! 60)
(let [config (if config-path
(edn/read-string (slurp config-path))
{})]
(reset! game-config {:config-path config-path :config config})
(try
(reset! game-state (init config))
(catch Throwable t
(println t)))
(while (not (rl/window-should-close?))
(if @last-error
(Thread/sleep 50)
(let [config (:config @game-config)]
(rl/with-drawing!
(try
(swap! game-state #(input config %))
(swap! game-state #(update config %))
(draw config @game-state)
(w/watch! (watch-fn config @game-state))
(catch Throwable t
(println t)
(reset! last-error t)
(w/watch! (merge (watch-fn config @game-state) {:error t}))))))))
(try
(when unload
(unload @game-state))
(catch Throwable t
(println t)))
(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-trace-log-level! "SetTraceLogLevel" [::mem/int] ::mem/void)
(defcfn window-should-close? "WindowShouldClose" [] ::bool)
(defcfn window-ready? "IsWindowReady" [] ::bool)
(defcfn begin-drawing! "BeginDrawing" [] ::mem/void)
(defcfn end-drawing! "EndDrawing" [] ::mem/void)
@ -259,10 +260,6 @@
(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!*
(ffi/make-downcall "DrawText"
[::mem/c-string ::mem/int ::mem/int ::mem/int ::color] ::mem/void))
@ -343,6 +340,10 @@
(defn texture-seg [tex]
(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]}]
(mem/serialize {:source source :left (int left) :top (int top)
:right (int right) :bottom (int bottom) :layout (int layout)}