From 33bba60a9b83e97d31761dc0b893db7f1808bb1f Mon Sep 17 00:00:00 2001 From: Yan Date: Sat, 8 Jan 2022 02:15:42 +0100 Subject: [PATCH] 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"))))