Keep texture-cache in engine, add a tap>, rl/set-clipboard-text

This commit is contained in:
Joseph Ferano 2026-08-29 06:04:06 +07:00
parent c80f2a515e
commit 0206708ad7
4 changed files with 55 additions and 22 deletions

View File

@ -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?))

View File

@ -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)
:-)

View File

@ -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]

View File

@ -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!)