From 0206708ad7e70c3a7018a6f160e630cc5b20aedc Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sat, 29 Aug 2026 06:04:06 +0700 Subject: [PATCH] Keep texture-cache in engine, add a tap>, rl/set-clipboard-text --- src/engine.clj | 15 ++++++++++++--- src/game.clj | 32 ++++++++++++++------------------ src/rl.clj | 9 +++++++++ src/user.clj | 21 ++++++++++++++++++++- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/engine.clj b/src/engine.clj index af838f1..2ff2303 100644 --- a/src/engine.clj +++ b/src/engine.clj @@ -4,9 +4,17 @@ [rl :as rl] [watch :as w])) -(defonce ^:private last-error (atom nil)) -(defonce ^:private game-state (atom nil)) -(defonce ^:private game-config (atom nil)) +(defonce last-error (atom nil)) +(defonce game-state (atom nil)) +(defonce game-config (atom nil)) +(defonce texture-cache (atom {})) +;; (reset! texture-cache {}) + +(defn load-texture-once [id path] + (or (@texture-cache path) + (let [t (assoc (rl/load-texture path) :name id)] + (swap! texture-cache assoc path t) + t))) (defn report-exception! "Stash the exception and print a marker line. Emacs watches the REPL output @@ -41,6 +49,7 @@ (reset! game-config {:config-path config-path :config config}) (try (reset! game-state (init config)) + (reset! texture-cache {}) (catch Throwable t (report-exception! t))) (while (not (rl/window-should-close?)) diff --git a/src/game.clj b/src/game.clj index 88ea70a..b3f4af4 100644 --- a/src/game.clj +++ b/src/game.clj @@ -3,7 +3,7 @@ (:use [util]) (:require - [engine] + [engine :as e] [watch :as w] [rl :as rl])) @@ -26,14 +26,6 @@ (def color-ground (rl/color-seg 0xE8CFA6)) (def color-tint-gray (rl/color-seg 0 0 0 100)) -(defonce texture-cache (atom {})) - -(defn load-texture-once [path] - (or (@texture-cache path) - (let [t (rl/load-texture path)] - (swap! texture-cache assoc path t) - t))) - (defn idx [row col] (+ (* row cols) col)) (defn tile-at [game layer r c] (aget ^objects (get (:layers game) layer) (idx r c))) (defn tile-at-idx [game layer idx] (aget ^objects (get (:layers game) layer) idx)) @@ -56,12 +48,11 @@ ;; TODO: When we cannot find the image, no errors are being reported. Likely because raylib is failing silently ;; TODO: How do we get Raylib to reload images on the fly :tilemaps - {:grass-hill (load-texture-once (str tileset-path "Grass_Hill_Tiles_v2.png")) - :grass-stone (load-texture-once (str tileset-path "Darker_Grass_Tiles_v2.png")) - ;; :soil (load-texture-once (str tileset-path "Soil_Ground_HiIls_Tiles.png")) - :soil (load-texture-once (str tileset-path "Soil_Ground_Tiles.png")) - :grass-top (load-texture-once (str tileset-path "Grass_Tile_Layers.png")) - #_#_:bush (load-texture-once "/home/joe/Downloads/Try1.png")}})) + (mapv #(apply e/load-texture-once %) + [[:grass-hill (str tileset-path "Grass_Hill_Tiles_v2.png")] + [:grass-stone (str tileset-path "Darker_Grass_Tiles_v2.png")] + [:soil (str tileset-path "Soil_Ground_Tiles.png")] + [:grass-top (str tileset-path "Grass_Tile_Layers.png")]])})) (def edge-set {0 [3 3], @@ -278,7 +269,7 @@ (reset! texture-cache {})) (defn -main [& _args] - (engine/run-game! + (e/run-game! {:title "Siam Farmer" :width screen-width :height screen-height @@ -291,6 +282,11 @@ (comment (future (-main)) - (reset! @#'engine/game-state (init-game {})) - (swap! @#'engine/game-state :assoc {}) + ;; TODO: I hate this, terrible way to read game state, maybe pass an atom in? + (reset! e/game-state (init-game {})) + (swap! e/game-state :assoc {}) + + hello + (:tilemaps @e/game-state) + :-) diff --git a/src/rl.clj b/src/rl.clj index 80293e4..6c6d71a 100644 --- a/src/rl.clj +++ b/src/rl.clj @@ -398,6 +398,15 @@ (float font-size) (float spacing)) ::vector-2)) + +;; TODO: LoadFont + +(def ^:private set-clipboard-text-raw!* + (ffi/make-downcall "SetClipboardText" [::mem/c-string] ::mem/void)) + +(defn set-clipboard-text!* [text] + (set-clipboard-text-raw!* (mem/serialize text ::mem/c-string arena))) + (defn alloc-pixels "An RGBA8888 pixel buffer, native so UpdateTexture can read it directly." ^MemorySegment [^long n-pixels] diff --git a/src/user.clj b/src/user.clj index c54c2c7..1e12f1e 100644 --- a/src/user.clj +++ b/src/user.clj @@ -1,2 +1,21 @@ (ns user - (:require [game])) + (:require + [clojure.pprint :as pprint] + [game])) + +(defonce tap-out (atom nil)) + +(defn tap-here! [] + (reset! tap-out *out*)) + +(defonce tap-fn + (fn [v] + (when-let [^java.io.Writer w @tap-out] + (binding [*out* w + *print-length* 50 + *print-level* 4] + (pprint/pprint v))))) + +(defonce _tap (add-tap tap-fn)) + +(tap-here!)