107 lines
3.6 KiB
Clojure
107 lines
3.6 KiB
Clojure
(ns sprite-atlas
|
|
(:require
|
|
[clojure.java.io :as io]
|
|
[engine]
|
|
[rl :as rl]))
|
|
|
|
(def screen-width 1250)
|
|
(def screen-height 780)
|
|
|
|
(defonce state (atom {}))
|
|
|
|
;; (defn tile-source [game]
|
|
;; {:x
|
|
;; :y
|
|
;; :width
|
|
;; :height
|
|
;; :name })
|
|
|
|
(defn init-sprite-atlas [config]
|
|
(let [path (:sprite-path @state)]
|
|
(-> (if (.exists (io/file 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]
|
|
(let [game (cond-> game
|
|
(rl/mouse-button-pressed? rl/mouse-button-left)
|
|
(assoc :drag-start (rl/get-mouse-position))
|
|
|
|
(rl/mouse-button-released? rl/mouse-button-left)
|
|
(assoc :event-drag-ended true)
|
|
|
|
:always
|
|
(as-> g (cond-> g
|
|
(contains? g :drag-start)
|
|
(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}]
|
|
(let [start-x (min ax bx)
|
|
start-y (min ay by)
|
|
end-x (max ax bx)
|
|
end-y (max ay by)]
|
|
{:x start-x :y start-y :w (- end-x start-x) :h (- end-y start-y)}))
|
|
|
|
(defn update-sprite-atlas [_ game]
|
|
(cond-> game
|
|
(contains? game :event-drag-ended)
|
|
(-> (assoc :selected-sprite (get-selection-rect (:drag-start game) (:current-mouse-pos game)))
|
|
(dissoc :event-drag-ended :drag-start :current-mouse-pos))))
|
|
|
|
(defn draw-sprite-atlas [_ {:keys [atlas zoom drag-start current-mouse-pos selected-sprite auto-tile-info]}]
|
|
(rl/clear-background!* rl/black)
|
|
(let [current-scale (* (/ screen-height (:height atlas)) (max 0.1 (* zoom 0.1)))]
|
|
(when atlas
|
|
(rl/draw-texture-ex!* (:seg atlas) (rl/vec2-seg 0 0) 0.0 current-scale rl/white)
|
|
(when drag-start
|
|
(let [start-x (:x drag-start)
|
|
start-y (:y drag-start)
|
|
end-x (:x 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)))
|
|
#_(when 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)))
|
|
(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]
|
|
{:zoom (:zoom game)})
|
|
|
|
(defn unload-sprite-atlas [game]
|
|
(rl/unload-texture! (:atlas game)))
|
|
|
|
(defn -main [& sprite-path]
|
|
(swap! state assoc :sprite-path (first sprite-path))
|
|
(engine/run-game!
|
|
{:title "Sprite Atlas"
|
|
:width screen-width
|
|
:height screen-height
|
|
:init #'init-sprite-atlas
|
|
:input #'handle-sprite-atlas-input
|
|
:update #'update-sprite-atlas
|
|
:draw #'draw-sprite-atlas
|
|
:unload #'unload-sprite-atlas
|
|
:watch-fn #'watch-sprite-atlas}))
|
|
|
|
(comment
|
|
(future (-main "source-assets/Sprout Lands - Sprites - premium pack/Sprout Lands - Sprites - premium pack/Tilesets/ground tiles/New tiles/Grass_tiles_v2.png"))
|
|
:-)
|