sprite-atlas: experimenting

This commit is contained in:
Joseph Ferano 2026-08-23 07:42:24 +07:00
parent 2186777587
commit 9df558f749

View File

@ -4,26 +4,45 @@
[engine] [engine]
[rl :as rl])) [rl :as rl]))
(def screen-width 1250)
(def screen-height 780)
(defonce state (atom {})) (defonce state (atom {}))
;; (defn tile-source [game]
;; {:x
;; :y
;; :width
;; :height
;; :name })
(defn init-sprite-atlas [config] (defn init-sprite-atlas [config]
(let [path (:sprite-path @state)] (let [path (:sprite-path @state)]
(if (.exists (io/file path)) (-> (if (.exists (io/file path))
{:atlas (rl/load-texture path)} {:atlas (rl/load-texture path)}
{}))) {})
(assoc :zoom 1))))
(defn- auto-tile [game cell-size cell-count]
(assoc game :auto-tile-info {:cell-size 16 :cell-count 11}))
(defn handle-sprite-atlas-input [_ game] (defn handle-sprite-atlas-input [_ game]
(cond-> game (let [game (cond-> game
(rl/mouse-button-pressed? rl/mouse-button-left) (rl/mouse-button-pressed? rl/mouse-button-left)
(assoc :drag-start (rl/get-mouse-position)) (assoc :drag-start (rl/get-mouse-position))
(rl/mouse-button-released? rl/mouse-button-left) (rl/mouse-button-released? rl/mouse-button-left)
(assoc :event-drag-ended true) (assoc :event-drag-ended true)
:always :always
(as-> g (cond-> g (as-> g (cond-> g
(contains? g :drag-start) (contains? g :drag-start)
(assoc :current-mouse-pos (rl/get-mouse-position)))))) (assoc :current-mouse-pos (rl/get-mouse-position)))))
move (rl/get-mouse-wheel-move)
game (cond-> game
(not (zero? move))
(update :zoom #(+ % move)))]
game))
(defn- get-selection-rect [{ax :x ay :y} {bx :x by :y}] (defn- get-selection-rect [{ax :x ay :y} {bx :x by :y}]
(let [start-x (min ax bx) (let [start-x (min ax bx)
@ -38,23 +57,33 @@
(-> (assoc :selected-sprite (get-selection-rect (:drag-start game) (:current-mouse-pos game))) (-> (assoc :selected-sprite (get-selection-rect (:drag-start game) (:current-mouse-pos game)))
(dissoc :event-drag-ended :drag-start :current-mouse-pos)))) (dissoc :event-drag-ended :drag-start :current-mouse-pos))))
(defn draw-sprite-atlas [_ {:keys [atlas drag-start current-mouse-pos selected-sprite]}] (defn draw-sprite-atlas [_ {:keys [atlas zoom drag-start current-mouse-pos selected-sprite auto-tile-info]}]
(rl/clear-background!* rl/black) (rl/clear-background!* rl/black)
(when atlas (let [current-scale (* (/ screen-height (:height atlas)) (max 0.1 (* zoom 0.1)))]
(rl/draw-texture!* (:seg atlas) 0 0 rl/white) (when atlas
(when drag-start (rl/draw-texture-ex!* (:seg atlas) (rl/vec2-seg 0 0) 0.0 current-scale rl/white)
(let [start-x (:x drag-start) (when drag-start
start-y (:y drag-start) (let [start-x (:x drag-start)
end-x (:x current-mouse-pos) start-y (:y drag-start)
end-y (:y current-mouse-pos)] end-x (:x current-mouse-pos)
(rl/draw-rectangle-lines!* start-x start-y (- end-x start-x) (- end-y start-y) rl/white))) end-y (:y current-mouse-pos)]
(when selected-sprite (rl/draw-rectangle-lines!* start-x start-y (- end-x start-x) (- end-y start-y) rl/white)))
(let [{:keys [x y w h]} selected-sprite] #_(when selected-sprite
(rl/draw-texture-rec!* (:seg atlas) (rl/rect-seg x y w h) (rl/vec2-seg 30 30) rl/white))) (let [{:keys [x y w h]} selected-sprite]
(rl/draw-fps 10 10))) (rl/draw-texture-rec!* (:seg atlas) (rl/rect-seg x y w h) (rl/vec2-seg 30 30) rl/white)))
(let [cell-size 16
rows 11
cols 12]
(doseq [row (range rows)
col (range cols)]
(rl/draw-rectangle-lines!* (* col cell-size current-scale)
(* row cell-size current-scale)
(* cell-size current-scale)
(* cell-size current-scale)
rl/gray))))))
(defn watch-sprite-atlas [config game] (defn watch-sprite-atlas [config game]
{}) {:zoom (:zoom game)})
(defn unload-sprite-atlas [game] (defn unload-sprite-atlas [game]
(rl/unload-texture! (:atlas game))) (rl/unload-texture! (:atlas game)))
@ -63,8 +92,8 @@
(swap! state assoc :sprite-path (first sprite-path)) (swap! state assoc :sprite-path (first sprite-path))
(engine/run-game! (engine/run-game!
{:title "Sprite Atlas" {:title "Sprite Atlas"
:width 1280 :width screen-width
:height 900 :height screen-height
:init #'init-sprite-atlas :init #'init-sprite-atlas
:input #'handle-sprite-atlas-input :input #'handle-sprite-atlas-input
:update #'update-sprite-atlas :update #'update-sprite-atlas
@ -73,5 +102,5 @@
:watch-fn #'watch-sprite-atlas})) :watch-fn #'watch-sprite-atlas}))
(comment (comment
(future (-main "./source-assets/thai_village.png")) (future (-main "source-assets/Sprout Lands - Sprites - premium pack/Sprout Lands - Sprites - premium pack/Tilesets/ground tiles/New tiles/Grass_tiles_v2.png"))
:-) :-)