Skip to content

Commit fb9a99e

Browse files
authored
Merge pull request #8 from hozayn/memory_leaks_fixes
Tutorials 12-16 fixes
2 parents 14ff91f + 8e32c9a commit fb9a99e

File tree

5 files changed

+62
-20
lines changed

5 files changed

+62
-20
lines changed

12-color-modulation.lisp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
:initform (error "Must supply a renderer"))
1515
(width
1616
:accessor tex-width
17-
:initform 0 )
17+
:initform 0)
1818
(height
1919
:accessor tex-height
2020
:initform 0)
2121
(texture
2222
:accessor tex-texture
2323
:initform nil)))
2424

25+
(defun free-tex (tex)
26+
(with-slots (texture) tex
27+
(sdl2:destroy-texture texture)))
28+
2529
(defun load-texture-from-file (renderer filename)
2630
(let ((tex (make-instance 'tex :renderer renderer)))
2731
(with-slots (renderer texture width height) tex
@@ -30,7 +34,8 @@
3034
(setf height (sdl2:surface-height surface))
3135
(sdl2:set-color-key surface :true (sdl2:map-rgb (sdl2:surface-format surface)
3236
0 #xFF #xFF))
33-
(setf texture (sdl2:create-texture-from-surface renderer surface))))
37+
(setf texture (sdl2:create-texture-from-surface renderer surface))
38+
(sdl2:free-surface surface)))
3439
tex))
3540

3641
(defun set-color (tex r g b)
@@ -89,4 +94,8 @@
8994
(sdl2:render-clear renderer)
9095
(set-color texture r g b)
9196
(render texture 0 0)
92-
(sdl2:render-present renderer))))))
97+
(sdl2:render-present renderer)))
98+
99+
;; Clean up
100+
(free-tex texture)
101+
(sdl2-image:quit))))

13-alpha-blending.lisp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
:initform (error "Must supply a renderer"))
1515
(width
1616
:accessor tex-width
17-
:initform 0 )
17+
:initform 0)
1818
(height
1919
:accessor tex-height
2020
:initform 0)
2121
(texture
2222
:accessor tex-texture
2323
:initform nil)))
2424

25+
(defun free-tex (tex)
26+
(with-slots (texture) tex
27+
(sdl2:destroy-texture texture)))
28+
2529
(defun load-texture-from-file (renderer filename)
2630
(let ((tex (make-instance 'tex :renderer renderer)))
2731
(with-slots (renderer texture width height) tex
@@ -30,7 +34,8 @@
3034
(setf height (sdl2:surface-height surface))
3135
(sdl2:set-color-key surface :true (sdl2:map-rgb (sdl2:surface-format surface)
3236
0 #xFF #xFF))
33-
(setf texture (sdl2:create-texture-from-surface renderer surface))))
37+
(setf texture (sdl2:create-texture-from-surface renderer surface))
38+
(sdl2:free-surface surface)))
3439
tex))
3540

3641
(defun set-color (tex r g b)
@@ -91,4 +96,9 @@
9196
(render bg-texture 0 0)
9297
(set-alpha modulated-texture alpha)
9398
(render modulated-texture 0 0)
94-
(sdl2:render-present renderer))))))
99+
(sdl2:render-present renderer)))
100+
101+
;; Clean up
102+
(free-tex bg-texture)
103+
(free-tex modulated-texture)
104+
(sdl2-image:quit))))

14-animated-sprites-and-vsync.lisp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
:initform (error "Must supply a renderer"))
1515
(width
1616
:accessor tex-width
17-
:initform 0 )
17+
:initform 0)
1818
(height
1919
:accessor tex-height
2020
:initform 0)
2121
(texture
2222
:accessor tex-texture
2323
:initform nil)))
2424

25+
(defun free-tex (tex)
26+
(with-slots (texture) tex
27+
(sdl2:destroy-texture texture)))
28+
2529
(defun load-texture-from-file (renderer filename)
2630
(let ((tex (make-instance 'tex :renderer renderer)))
2731
(with-slots (renderer texture width height) tex
@@ -30,7 +34,8 @@
3034
(setf height (sdl2:surface-height surface))
3135
(sdl2:set-color-key surface :true (sdl2:map-rgb (sdl2:surface-format surface)
3236
0 #xFF #xFF))
33-
(setf texture (sdl2:create-texture-from-surface renderer surface))))
37+
(setf texture (sdl2:create-texture-from-surface renderer surface))
38+
(sdl2:free-surface surface)))
3439
tex))
3540

3641
(defun set-color (tex r g b)
@@ -97,4 +102,8 @@
97102
(incf current-sprite-frame)
98103
(when (>= current-sprite-frame sprite-frames)
99104
(setf current-sprite-frame 0))
100-
(setf (sdl2:rect-x clip) (* current-sprite-frame (sdl2:rect-width clip)))))))))
105+
(setf (sdl2:rect-x clip) (* current-sprite-frame (sdl2:rect-width clip))))))
106+
107+
;; Clean up
108+
(free-tex spritesheet-tex)
109+
(sdl2-image:quit))))

15-rotation-and-flipping.lisp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414
:initform (error "Must supply a renderer"))
1515
(width
1616
:accessor tex-width
17-
:initform 0 )
17+
:initform 0)
1818
(height
1919
:accessor tex-height
2020
:initform 0)
2121
(texture
2222
:accessor tex-texture
2323
:initform nil)))
2424

25+
(defun free-tex (tex)
26+
(with-slots (texture) tex
27+
(sdl2:destroy-texture texture)))
28+
2529
(defun load-texture-from-file (renderer filename)
2630
(let ((tex (make-instance 'tex :renderer renderer)))
2731
(with-slots (renderer texture width height) tex
@@ -30,7 +34,8 @@
3034
(setf height (sdl2:surface-height surface))
3135
(sdl2:set-color-key surface :true (sdl2:map-rgb (sdl2:surface-format surface)
3236
0 #xFF #xFF))
33-
(setf texture (sdl2:create-texture-from-surface renderer surface))))
37+
(setf texture (sdl2:create-texture-from-surface renderer surface))
38+
(sdl2:free-surface surface)))
3439
tex))
3540

3641
(defun set-color (tex r g b)
@@ -56,7 +61,7 @@
5661
:w *screen-width*
5762
:h *screen-height*
5863
:flags '(:shown))
59-
(sdl2:with-renderer (,renderer ,window :index -1 :flags '(:accelerated))
64+
(sdl2:with-renderer (,renderer ,window :index -1 :flags '(:accelerated :presentvsync))
6065
,@body))))
6166

6267
(defun run ()
@@ -84,4 +89,8 @@
8489
(round (/ (- *screen-height* (tex-height texture)) 2))
8590
:angle degrees
8691
:flip flip)
87-
(sdl2:render-present renderer))))))
92+
(sdl2:render-present renderer)))
93+
94+
;; Clean up
95+
(free-tex texture)
96+
(sdl2-image:quit))))

16-true-type-fonts.lisp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
:initform (error "Must supply a renderer"))
1717
(width
1818
:accessor tex-width
19-
:initform 0 )
19+
:initform 0)
2020
(height
2121
:accessor tex-height
2222
:initform 0)
@@ -36,7 +36,8 @@
3636
(setf height (sdl2:surface-height surface))
3737
(sdl2:set-color-key surface :true (sdl2:map-rgb (sdl2:surface-format surface)
3838
0 #xFF #xFF))
39-
(setf texture (sdl2:create-texture-from-surface renderer surface))))
39+
(setf texture (sdl2:create-texture-from-surface renderer surface))
40+
(sdl2:free-surface surface)))
4041
tex))
4142

4243
(defun load-texture-from-text (renderer text)
@@ -45,7 +46,8 @@
4546
(let ((surface (sdl2-ttf:render-text-solid *font* text 0 0 0 0)))
4647
(setf width (sdl2:surface-width surface))
4748
(setf height (sdl2:surface-height surface))
48-
(setf texture (sdl2:create-texture-from-surface renderer surface))))
49+
(setf texture (sdl2:create-texture-from-surface renderer surface))
50+
(sdl2:free-surface surface)))
4951
tex))
5052

5153
(defun set-color (tex r g b)
@@ -71,7 +73,7 @@
7173
:w *screen-width*
7274
:h *screen-height*
7375
:flags '(:shown))
74-
(sdl2:with-renderer (,renderer ,window :index -1 :flags '(:accelerated))
76+
(sdl2:with-renderer (,renderer ,window :index -1 :flags '(:accelerated :presentvsync))
7577
,@body))))
7678

7779
(defun run ()
@@ -89,7 +91,10 @@
8991
(round (/ (- *screen-width* (tex-width texture)) 2))
9092
(round (/ (- *screen-height* (tex-height texture)) 2)))
9193
(sdl2:render-present renderer)))
94+
9295
;; clean up
93-
(free-tex texture))
94-
(sdl2-ttf:quit)
95-
(sdl2-image:quit)))
96+
(free-tex texture)
97+
(sdl2-ttf:close-font *font*)
98+
(setf *font* nil)
99+
(sdl2-ttf:quit)
100+
(sdl2-image:quit))))

0 commit comments

Comments
 (0)