Make game-state global, implement hash-table for textures, free them at the end

This commit is contained in:
Joseph Ferano 2024-11-11 17:23:23 +07:00
parent 97fd389f9e
commit 55f875cb44

View File

@ -7,15 +7,22 @@
(in-package :raylib-user) (in-package :raylib-user)
(defparameter *terrain-txt*
(rl:load-texture (uiop:native-namestring (asdf:system-relative-pathname
'cl-raylib
"assets/Terrain/Ground/Tilemap_Flat.png"))))
(defparameter *move-speed* 2.0) (defparameter *move-speed* 2.0)
(defstruct game-state (defstruct game-state
(textures (make-hash-table))
(player-pos (vec 100 100))) (player-pos (vec 100 100)))
(defparameter *game-state* nil)
(defun bind-texture (state key path)
(setf (gethash key (game-state-textures state)) (rl:load-texture (uiop:native-namestring path))))
(defun game-init ()
(let ((state (make-game-state)))
(bind-texture state 'terrain "~/Development/tinyswords/assets/Terrain/Ground/Tilemap_Flat.png")
state))
(defun game-input (state) (defun game-input (state)
(with-slots ((pos player-pos)) state (with-slots ((pos player-pos)) state
(let ((dx 0.0) (dy 0.0)) (let ((dx 0.0) (dy 0.0))
@ -28,23 +35,22 @@
(defun game-update (state) '()) (defun game-update (state) '())
(defun game-draw (state) (defun game-draw (state)
(rl:draw-rectangle-v (game-state-player-pos state) (rl:draw-texture-v (gethash 'terrain (game-state-textures state)) (vec 50 50) :white)
(vec 100 100)
:skyblue)
(rl:draw-texture-v *terrain-txt* (vec 500 400) (rl:make-color 1.0 1.0 1.0 1.0))
(rl:draw-fps 10 5)) (rl:draw-fps 10 5))
(defun main () (defun main ()
(let* ((screen-width 900) (let* ((screen-width 900)
(screen-height 700) (screen-height 500))
(game-state (make-game-state)))
(rl:with-window (screen-width screen-height "RTS") (rl:with-window (screen-width screen-height "RTS")
(rl:set-target-fps 60) (rl:set-target-fps 60)
(setf *game-state* (game-init))
(loop :until (rl:window-should-close) (loop :until (rl:window-should-close)
:do (game-input game-state) :do (game-input *game-state*)
(game-update game-state) (game-update *game-state*)
(rl:with-drawing (rl:with-drawing
(rl:clear-background :raywhite) (rl:clear-background :raywhite)
(game-draw game-state)))))) (game-draw *game-state*)))
(loop for value being the hash-values of (game-state-textures *game-state*)
do (rl:unload-texture value)))))
(main) (main)