Skip to content

Commit 2b46819

Browse files
Removed memory leaks in Open GL example
1 parent b9d6f53 commit 2b46819

File tree

1 file changed

+92
-97
lines changed

1 file changed

+92
-97
lines changed

examples/gl-example.lisp

+92-97
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@
1010
(setf (gl:glaref gl-array i) (aref lisp-array i)))
1111
gl-array))
1212

13-
;;Text, as texutres, are loaded upside down and mirrored. Plan accordingly!
14-
(defparameter *vertex-color-texture-array* (create-gl-array :float #(1.0 1.0 1.0 0.0 1.0
15-
1.0 1.0 1.0 1.0 1.0
16-
1.0 1.0 1.0 0.0 0.0
17-
1.0 1.0 1.0 1.0 0.0)))
18-
19-
;;since we're using an orthographic projection we need the width and height of our texture
20-
(defparameter *vertex-position-array* 'nil)
21-
22-
(defparameter *element-attribute-array* (create-gl-array :unsigned-short #(0 1 2 3)))
23-
24-
(defparameter *projection-matrix* (kit.math:ortho-matrix 0 300 0 300 -10 10))
25-
26-
(defparameter *translation-matrix* (sb-cga:translate* 150.0 150.0 0.0))
27-
2813
(defun gl-example ()
2914
(with-init (:everything)
3015
(sdl2-ttf:init)
@@ -33,7 +18,14 @@
3318
(gl-make-current my-window gl-context)
3419
(gl:viewport 0 0 300 300)
3520
;;the texture-surface is the actual loaded image object
36-
(let* ((font (sdl2-ttf:open-font (asdf:system-relative-pathname 'sdl2-ttf-examples "examples/PROBE_10PX_OTF.otf")
21+
(let* ((vertex-color-texture-array (create-gl-array :float #(1.0 1.0 1.0 0.0 1.0
22+
1.0 1.0 1.0 1.0 1.0
23+
1.0 1.0 1.0 0.0 0.0
24+
1.0 1.0 1.0 1.0 0.0)))
25+
(element-attribute-array (create-gl-array :unsigned-short #(0 1 2 3)))
26+
(projection-matrix (kit.math:ortho-matrix 0 300 0 300 -10 10))
27+
(translation-matrix (sb-cga:translate* 150.0 150.0 0.0))
28+
(font (sdl2-ttf:open-font (asdf:system-relative-pathname 'sdl2-ttf-examples "examples/PROBE_10PX_OTF.otf")
3729
10))
3830
(texture-surface (sdl2-ttf:render-text-blended font
3931
"hello world"
@@ -66,87 +58,90 @@
6658
(gl:link-program shader-program)
6759
(gl:use-program shader-program)
6860

69-
(let ((width (/ (surface-width texture-surface) 2.0))
70-
(height (/ (surface-height texture-surface) 2.0)))
71-
(setf *vertex-position-array* (create-gl-array :float (make-array 8
72-
:initial-contents `(,(- width) ,(- height)
73-
,width ,(- height)
74-
,(- width) ,height
75-
,width ,height)))))
76-
(gl:bind-vertex-array vao)
77-
78-
(gl:bind-buffer :array-buffer (first buffers))
79-
(gl:buffer-data :array-buffer :static-draw *vertex-position-array*)
80-
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "position")
81-
2
82-
:float
83-
:false
84-
(* 2 (cffi:foreign-type-size :float))
85-
(cffi:null-pointer))
86-
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
87-
88-
(gl:bind-buffer :array-buffer (second buffers))
89-
(gl:buffer-data :array-buffer :static-draw *vertex-color-texture-array*)
90-
91-
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "input_color")
92-
3
93-
:float
94-
:false
95-
(* 5 (cffi:foreign-type-size :float))
96-
(cffi:null-pointer))
97-
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "input_color"))
98-
99-
;;Texture coordinates
100-
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "tex_coord")
101-
2
102-
:float
103-
:false
104-
(* 5 (cffi:foreign-type-size :float))
105-
(* 3 (cffi:foreign-type-size :float)))
106-
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "tex_coord"))
61+
(let* ((width (/ (surface-width texture-surface) 2.0))
62+
(height (/ (surface-height texture-surface) 2.0))
63+
(vertex-position-array (create-gl-array :float (make-array 8
64+
:initial-contents `(,(- width) ,(- height)
65+
,width ,(- height)
66+
,(- width) ,height
67+
,width ,height)))))
68+
(gl:bind-vertex-array vao)
69+
70+
(gl:bind-buffer :array-buffer (first buffers))
71+
(gl:buffer-data :array-buffer :static-draw vertex-position-array)
72+
(gl:free-gl-array vertex-position-array)
73+
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "position")
74+
2
75+
:float
76+
:false
77+
(* 2 (cffi:foreign-type-size :float))
78+
(cffi:null-pointer))
79+
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
10780

108-
;;Bind the projection matrix
109-
(gl:uniform-matrix (gl:get-uniform-location shader-program "mvp")
110-
4
111-
(vector (sb-cga:matrix* *projection-matrix* *translation-matrix*))
112-
'nil)
81+
(gl:bind-buffer :array-buffer (second buffers))
82+
(gl:buffer-data :array-buffer :static-draw vertex-color-texture-array)
83+
(gl:free-gl-array vertex-color-texture-array)
84+
85+
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "input_color")
86+
3
87+
:float
88+
:false
89+
(* 5 (cffi:foreign-type-size :float))
90+
(cffi:null-pointer))
91+
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "input_color"))
92+
93+
;;Texture coordinates
94+
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "tex_coord")
95+
2
96+
:float
97+
:false
98+
(* 5 (cffi:foreign-type-size :float))
99+
(* 3 (cffi:foreign-type-size :float)))
100+
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "tex_coord"))
113101

114-
;;Binding the texture object for configuration
115-
(gl:bind-texture :texture-2d texture)
116-
(gl:tex-parameter :texture-2d :texture-wrap-s :clamp-to-border)
117-
(gl:tex-parameter :texture-2d :texture-wrap-t :clamp-to-border)
118-
(gl:generate-mipmap :texture-2d)
119-
(gl:tex-parameter :texture-2d :texture-min-filter :linear)
120-
(gl:tex-parameter :texture-2d :texture-mag-filter :linear)
121-
(gl:tex-image-2d :texture-2d
122-
0
123-
:rgba
124-
(surface-width texture-surface)
125-
(surface-height texture-surface)
126-
0
127-
:rgba
128-
:unsigned-byte
129-
;;Note this does NOT need to be freed because it's a dereferenced pointer belonging to struct, not a pointer to a pointer! It will be freed when free-surface is called later
130-
(surface-pixels texture-surface))
131-
(gl:bind-buffer :element-array-buffer (third buffers))
132-
(gl:buffer-data :element-array-buffer :static-draw *element-attribute-array*)
102+
;;Bind the projection matrix
103+
(gl:uniform-matrix (gl:get-uniform-location shader-program "mvp")
104+
4
105+
(vector (sb-cga:matrix* projection-matrix translation-matrix))
106+
'nil)
133107

134-
(with-event-loop (:method :poll)
135-
(:idle ()
136-
(gl:clear-color 0.0 0.0 0.0 0.0)
137-
(gl:clear :color-buffer)
138-
(gl:draw-elements :triangle-strip
139-
(gl:make-null-gl-array :unsigned-short)
140-
:count 4)
141-
(gl-swap-window my-window))
142-
(:quit ()
143-
(when (> (sdl2-ttf:was-init) 0)
144-
(sdl2-ttf:close-font font)
145-
(free-surface texture-surface)
146-
(sdl2-ttf:quit))
147-
(gl:disable-vertex-attrib-array vao)
148-
(gl:delete-buffers buffers)
149-
(gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
150-
(gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "input_color"))
151-
(gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "tex_coord"))
152-
t)))))))
108+
;;Binding the texture object for configuration
109+
(gl:bind-texture :texture-2d texture)
110+
(gl:tex-parameter :texture-2d :texture-wrap-s :clamp-to-border)
111+
(gl:tex-parameter :texture-2d :texture-wrap-t :clamp-to-border)
112+
(gl:generate-mipmap :texture-2d)
113+
(gl:tex-parameter :texture-2d :texture-min-filter :linear)
114+
(gl:tex-parameter :texture-2d :texture-mag-filter :linear)
115+
(gl:tex-image-2d :texture-2d
116+
0
117+
:rgba
118+
(surface-width texture-surface)
119+
(surface-height texture-surface)
120+
0
121+
:rgba
122+
:unsigned-byte
123+
;;Note this does NOT need to be freed because it's a dereferenced pointer belonging to struct, not a pointer to a pointer! It will be freed when free-surface is called later
124+
(surface-pixels texture-surface))
125+
(gl:bind-buffer :element-array-buffer (third buffers))
126+
(gl:buffer-data :element-array-buffer :static-draw element-attribute-array)
127+
(gl:free-gl-array element-attribute-array)
128+
129+
(with-event-loop (:method :poll)
130+
(:idle ()
131+
(gl:clear-color 0.0 0.0 0.0 0.0)
132+
(gl:clear :color-buffer)
133+
(gl:draw-elements :triangle-strip
134+
(gl:make-null-gl-array :unsigned-short)
135+
:count 4)
136+
(gl-swap-window my-window))
137+
(:quit ()
138+
(when (> (sdl2-ttf:was-init) 0)
139+
(sdl2-ttf:close-font font)
140+
(free-surface texture-surface)
141+
(sdl2-ttf:quit))
142+
(gl:disable-vertex-attrib-array vao)
143+
(gl:delete-buffers buffers)
144+
(gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
145+
(gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "input_color"))
146+
(gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "tex_coord"))
147+
t))))))))

0 commit comments

Comments
 (0)