sprite-atlas: experimenting
This commit is contained in:
parent
2186777587
commit
9df558f749
@ -4,16 +4,30 @@
|
|||||||
[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))
|
||||||
|
|
||||||
@ -23,7 +37,12 @@
|
|||||||
: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)
|
||||||
|
(let [current-scale (* (/ screen-height (:height atlas)) (max 0.1 (* zoom 0.1)))]
|
||||||
(when atlas
|
(when atlas
|
||||||
(rl/draw-texture!* (:seg atlas) 0 0 rl/white)
|
(rl/draw-texture-ex!* (:seg atlas) (rl/vec2-seg 0 0) 0.0 current-scale rl/white)
|
||||||
(when drag-start
|
(when drag-start
|
||||||
(let [start-x (:x drag-start)
|
(let [start-x (:x drag-start)
|
||||||
start-y (:y drag-start)
|
start-y (:y drag-start)
|
||||||
end-x (:x current-mouse-pos)
|
end-x (:x current-mouse-pos)
|
||||||
end-y (:y current-mouse-pos)]
|
end-y (:y current-mouse-pos)]
|
||||||
(rl/draw-rectangle-lines!* start-x start-y (- end-x start-x) (- end-y start-y) rl/white)))
|
(rl/draw-rectangle-lines!* start-x start-y (- end-x start-x) (- end-y start-y) rl/white)))
|
||||||
(when selected-sprite
|
#_(when selected-sprite
|
||||||
(let [{:keys [x y w h]} selected-sprite]
|
(let [{:keys [x y w h]} selected-sprite]
|
||||||
(rl/draw-texture-rec!* (:seg atlas) (rl/rect-seg x y w h) (rl/vec2-seg 30 30) rl/white)))
|
(rl/draw-texture-rec!* (:seg atlas) (rl/rect-seg x y w h) (rl/vec2-seg 30 30) rl/white)))
|
||||||
(rl/draw-fps 10 10)))
|
(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"))
|
||||||
:-)
|
:-)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user