From 21867775878ff87f76b74d7c146198ed42cb94da Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 23 Aug 2026 07:41:12 +0700 Subject: [PATCH] Engine: Check if window already exists, fix load texture and add window-ready? --- src/engine.clj | 71 ++++++++++++++++++++++++++------------------------ src/rl.clj | 9 ++++--- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/src/engine.clj b/src/engine.clj index 2345a68..2425d43 100644 --- a/src/engine.clj +++ b/src/engine.clj @@ -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))))) diff --git a/src/rl.clj b/src/rl.clj index 6351b4c..3c7492e 100644 --- a/src/rl.clj +++ b/src/rl.clj @@ -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)}