From 55f875cb445838f2312ad14936f451589bbdfdf8 Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Mon, 11 Nov 2024 17:23:23 +0700 Subject: [PATCH] Make game-state global, implement hash-table for textures, free them at the end --- game.lisp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/game.lisp b/game.lisp index 14ae37d..7fc8f06 100644 --- a/game.lisp +++ b/game.lisp @@ -7,15 +7,22 @@ (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) (defstruct game-state + (textures (make-hash-table)) (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) (with-slots ((pos player-pos)) state (let ((dx 0.0) (dy 0.0)) @@ -28,23 +35,22 @@ (defun game-update (state) '()) (defun game-draw (state) - (rl:draw-rectangle-v (game-state-player-pos state) - (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-texture-v (gethash 'terrain (game-state-textures state)) (vec 50 50) :white) (rl:draw-fps 10 5)) (defun main () (let* ((screen-width 900) - (screen-height 700) - (game-state (make-game-state))) + (screen-height 500)) (rl:with-window (screen-width screen-height "RTS") (rl:set-target-fps 60) + (setf *game-state* (game-init)) (loop :until (rl:window-should-close) - :do (game-input game-state) - (game-update game-state) + :do (game-input *game-state*) + (game-update *game-state*) (rl:with-drawing (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)