From 2bf5b6c741c51d5224429d58c27d41d66ec7dcfd Mon Sep 17 00:00:00 2001 From: Yan Date: Sat, 8 Jan 2022 01:56:38 +0100 Subject: [PATCH 1/5] Refactor tutorial 14 --- ...14.lisp => 14-animated-sprites-and-vsync.lisp | 15 ++++++++------- {14 => assets/14}/character.png | Bin sdl2-tutorial.asd | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) rename 14/tutorial-14.lisp => 14-animated-sprites-and-vsync.lisp (89%) rename {14 => assets/14}/character.png (100%) diff --git a/14/tutorial-14.lisp b/14-animated-sprites-and-vsync.lisp similarity index 89% rename from 14/tutorial-14.lisp rename to 14-animated-sprites-and-vsync.lisp index 05def6b..428617a 100644 --- a/14/tutorial-14.lisp +++ b/14-animated-sprites-and-vsync.lisp @@ -1,8 +1,9 @@ -(defpackage #:sdl2-tutorial-14 - (:use :common-lisp) - (:export :main)) +(defpackage #:sdl2-tutorial-14-animated-sprites-and-vsync + (:use :cl) + (:export :run) + (:import-from :sdl2-tutorial-utils :asset-pathname)) -(in-package :sdl2-tutorial-14) +(in-package #:sdl2-tutorial-14-animated-sprites-and-vsync) (defparameter *screen-width* 640) (defparameter *screen-height* 480) @@ -54,7 +55,7 @@ (defmacro with-window-renderer ((window renderer) &body body) `(sdl2:with-init (:video) (sdl2:with-window (,window - :title "SDL2 Tutorial" + :title "SDL2 Tutorial 14" :w *screen-width* :h *screen-height* :flags '(:shown)) @@ -70,10 +71,10 @@ (defmacro clamp-decf (x delta) `(setf ,x (clamp (- ,x ,delta)))) -(defun main() +(defun run () (with-window-renderer (window renderer) (sdl2-image:init '(:png)) - (let ((spritesheet-tex (load-texture-from-file renderer "14/character.png")) + (let ((spritesheet-tex (load-texture-from-file renderer (asset-pathname "assets/14/character.png"))) (clip (sdl2:make-rect 0 0 64 96)) (sprite-frames 4) (current-sprite-frame 0) diff --git a/14/character.png b/assets/14/character.png similarity index 100% rename from 14/character.png rename to assets/14/character.png diff --git a/sdl2-tutorial.asd b/sdl2-tutorial.asd index eecee57..70f1883 100644 --- a/sdl2-tutorial.asd +++ b/sdl2-tutorial.asd @@ -18,7 +18,7 @@ (:file "11-clip-rendering-and-sprite-sheets") (:file "12-color-modulation") (:file "13-alpha-blending") - (:file "14/tutorial-14") + (:file "14-animated-sprites-and-vsync") (:file "15/tutorial-15") (:file "16/tutorial-16")) :in-order-to ((test-op (test-op "sdl2-tutorial/tests")))) From cfec61dc402d162ae679a8f8563dc1525251a722 Mon Sep 17 00:00:00 2001 From: Yan Date: Sat, 8 Jan 2022 02:02:29 +0100 Subject: [PATCH 2/5] Refactor tutorial 15 --- ...rial-15.lisp => 15-rotation-and-flipping.lisp | 15 ++++++++------- {15 => assets/15}/arrow.png | Bin sdl2-tutorial.asd | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) rename 15/tutorial-15.lisp => 15-rotation-and-flipping.lisp (89%) rename {15 => assets/15}/arrow.png (100%) diff --git a/15/tutorial-15.lisp b/15-rotation-and-flipping.lisp similarity index 89% rename from 15/tutorial-15.lisp rename to 15-rotation-and-flipping.lisp index 99699a0..9d980e8 100644 --- a/15/tutorial-15.lisp +++ b/15-rotation-and-flipping.lisp @@ -1,8 +1,9 @@ -(defpackage #:sdl2-tutorial-15 - (:use :common-lisp) - (:export :main)) +(defpackage #:sdl2-tutorial-15-rotation-and-flipping + (:use :cl) + (:export :run) + (:import-from :sdl2-tutorial-utils :asset-pathname)) -(in-package :sdl2-tutorial-15) +(in-package #:sdl2-tutorial-15-rotation-and-flipping) (defparameter *screen-width* 640) (defparameter *screen-height* 480) @@ -51,17 +52,17 @@ (defmacro with-window-renderer ((window renderer) &body body) `(sdl2:with-init (:video) (sdl2:with-window (,window - :title "SDL2 Tutorial" + :title "SDL2 Tutorial 15" :w *screen-width* :h *screen-height* :flags '(:shown)) (sdl2:with-renderer (,renderer ,window :index -1 :flags '(:accelerated)) ,@body)))) -(defun main() +(defun run () (with-window-renderer (window renderer) (sdl2-image:init '(:png)) - (let ((texture (load-texture-from-file renderer "15/arrow.png")) + (let ((texture (load-texture-from-file renderer (asset-pathname "assets/15/arrow.png"))) (flip :none) (degrees 0) (delta 60)) diff --git a/15/arrow.png b/assets/15/arrow.png similarity index 100% rename from 15/arrow.png rename to assets/15/arrow.png diff --git a/sdl2-tutorial.asd b/sdl2-tutorial.asd index 70f1883..baea08f 100644 --- a/sdl2-tutorial.asd +++ b/sdl2-tutorial.asd @@ -19,7 +19,7 @@ (:file "12-color-modulation") (:file "13-alpha-blending") (:file "14-animated-sprites-and-vsync") - (:file "15/tutorial-15") + (:file "15-rotation-and-flipping") (:file "16/tutorial-16")) :in-order-to ((test-op (test-op "sdl2-tutorial/tests")))) From 33bba60a9b83e97d31761dc0b893db7f1808bb1f Mon Sep 17 00:00:00 2001 From: Yan Date: Sat, 8 Jan 2022 02:15:42 +0100 Subject: [PATCH 3/5] Refactor tutorial 16 --- ...utorial-16.lisp => 16-true-type-fonts.lisp | 22 +++++++++++------- {16 => assets/16}/Pacifico.ttf | Bin sdl2-tutorial.asd | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) rename 16/tutorial-16.lisp => 16-true-type-fonts.lisp (86%) rename {16 => assets/16}/Pacifico.ttf (100%) diff --git a/16/tutorial-16.lisp b/16-true-type-fonts.lisp similarity index 86% rename from 16/tutorial-16.lisp rename to 16-true-type-fonts.lisp index 367933f..05b8c41 100644 --- a/16/tutorial-16.lisp +++ b/16-true-type-fonts.lisp @@ -1,8 +1,9 @@ -(defpackage #:sdl2-tutorial-16 - (:use :common-lisp) - (:export :main)) +(defpackage #:sdl2-tutorial-16-true-type-fonts + (:use :cl) + (:export :run) + (:import-from :sdl2-tutorial-utils :asset-pathname)) -(in-package :sdl2-tutorial-16) +(in-package #:sdl2-tutorial-16-true-type-fonts) (defparameter *screen-width* 640) (defparameter *screen-height* 480) @@ -23,6 +24,10 @@ :accessor tex-texture :initform nil))) +(defun free-tex (tex) + (with-slots (texture) tex + (sdl2:destroy-texture texture))) + (defun load-texture-from-file (renderer filename) (let ((tex (make-instance 'tex :renderer renderer))) (with-slots (renderer texture width height) tex @@ -62,18 +67,18 @@ (defmacro with-window-renderer ((window renderer) &body body) `(sdl2:with-init (:video) (sdl2:with-window (,window - :title "SDL2 Tutorial" + :title "SDL2 Tutorial 16" :w *screen-width* :h *screen-height* :flags '(:shown)) (sdl2:with-renderer (,renderer ,window :index -1 :flags '(:accelerated)) ,@body)))) -(defun main() +(defun run () (with-window-renderer (window renderer) (sdl2-image:init '(:png)) (sdl2-ttf:init) - (setf *font* (sdl2-ttf:open-font "16/Pacifico.ttf" 28)) + (setf *font* (sdl2-ttf:open-font (asset-pathname "assets/16/Pacifico.ttf") 28)) (let ((texture (load-texture-from-text renderer "The quick brown fox jumps over the lazy dog"))) (sdl2:with-event-loop (:method :poll) (:quit () t) @@ -83,6 +88,7 @@ (render texture (round (/ (- *screen-width* (tex-width texture)) 2)) (round (/ (- *screen-height* (tex-height texture)) 2))) - (sdl2:render-present renderer)))) + (sdl2:render-present renderer))) + (free-tex texture)) (sdl2-ttf:quit) (sdl2-image:quit))) diff --git a/16/Pacifico.ttf b/assets/16/Pacifico.ttf similarity index 100% rename from 16/Pacifico.ttf rename to assets/16/Pacifico.ttf diff --git a/sdl2-tutorial.asd b/sdl2-tutorial.asd index baea08f..7350e3a 100644 --- a/sdl2-tutorial.asd +++ b/sdl2-tutorial.asd @@ -20,7 +20,7 @@ (:file "13-alpha-blending") (:file "14-animated-sprites-and-vsync") (:file "15-rotation-and-flipping") - (:file "16/tutorial-16")) + (:file "16-true-type-fonts")) :in-order-to ((test-op (test-op "sdl2-tutorial/tests")))) From ea0d2fef50c0408f76f04d6b0713f82ccfee08b7 Mon Sep 17 00:00:00 2001 From: Yan Date: Sat, 8 Jan 2022 02:17:55 +0100 Subject: [PATCH 4/5] Get rid of warning in tutorial 16 by setting default flip to :none WARNING: Unknown mask symbol NIL, treated as 0; expected one of: (:HORIZONTAL :NONE :VERTICAL) --- 16-true-type-fonts.lisp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/16-true-type-fonts.lisp b/16-true-type-fonts.lisp index 05b8c41..dd28629 100644 --- a/16-true-type-fonts.lisp +++ b/16-true-type-fonts.lisp @@ -51,7 +51,7 @@ (defun set-color (tex r g b) (sdl2:set-texture-color-mod (tex-texture tex) r g b)) -(defun render (tex x y &key clip angle center flip) +(defun render (tex x y &key clip angle center (flip :none)) (with-slots (renderer texture width height) tex (sdl2:render-copy-ex renderer texture From bfa61c4cb671f1a7373363c31e3ebfad268aa378 Mon Sep 17 00:00:00 2001 From: Yan Date: Sat, 8 Jan 2022 02:56:57 +0100 Subject: [PATCH 5/5] Add clean up comment to tutorial 16 Consistency... --- 16-true-type-fonts.lisp | 1 + 1 file changed, 1 insertion(+) diff --git a/16-true-type-fonts.lisp b/16-true-type-fonts.lisp index dd28629..82d328d 100644 --- a/16-true-type-fonts.lisp +++ b/16-true-type-fonts.lisp @@ -89,6 +89,7 @@ (round (/ (- *screen-width* (tex-width texture)) 2)) (round (/ (- *screen-height* (tex-height texture)) 2))) (sdl2:render-present renderer))) + ;; clean up (free-tex texture)) (sdl2-ttf:quit) (sdl2-image:quit)))