Mon, 30 Oct 2023 17:59:37 +0100
fix possible double-invocation of asc_window_destroy_impl()
0 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * Copyright 2023 Mike Becker. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
25 | * POSSIBILITY OF SUCH DAMAGE. | |
26 | */ | |
27 | ||
28 | #include "ascension/core.h" | |
29 | #include "ascension/utils.h" | |
30 | ||
31 | #include <cx/linked_list.h> | |
32 | #include <cx/printf.h> | |
33 | ||
34 | static void asc_gl_debug_callback( | |
35 | GLenum source, GLenum type, GLuint id, GLenum severity, | |
36 | GLsizei length, const GLchar* message, | |
37 | const void* userParam | |
38 | ) { | |
39 | cxmutstr buf = cx_asprintf( | |
40 | "source = %d, id = %u, type = %d, severity= %d, message = %.*s", | |
41 | source, id, type, severity, length, message); | |
42 | if (type == GL_DEBUG_TYPE_ERROR) { | |
43 | asc_error(buf.ptr); | |
44 | } else { | |
45 | asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr); | |
46 | } | |
47 | cx_strfree(&buf); | |
48 | } | |
49 | ||
50 | AscContext asc_context; | |
51 | ||
52 | // forward declarations | |
53 | static void asc_window_destroy_impl(AscWindow* window); | |
54 | ||
55 | void asc_context_initialize(void) { | |
56 | if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) | |
57 | return; | |
58 | asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); | |
59 | ||
60 | // initialize error buffer | |
61 | cxBufferInit( | |
62 | &asc_context.error_buffer, | |
63 | NULL, | |
64 | 256, | |
65 | NULL, | |
66 | CX_BUFFER_AUTO_EXTEND | |
67 | ); | |
68 | ||
69 | // initialize lists | |
70 | asc_context.windows = cxLinkedListCreateSimple(CX_STORE_POINTERS); | |
71 | asc_context.windows->simple_destructor = | |
72 | (cx_destructor_func)asc_window_destroy_impl; | |
73 | ||
74 | // initialize SDL | |
75 | if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
76 | asc_error(SDL_GetError()); | |
77 | } else { | |
78 | if (TTF_Init() < 0) { | |
79 | asc_error(TTF_GetError()); | |
80 | } | |
81 | } | |
82 | SDL_ClearError(); | |
83 | asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED); | |
84 | asc_dprintf("Ascension context initialized."); | |
85 | } | |
86 | ||
87 | void asc_context_destroy(void) { | |
88 | // destroy lists | |
89 | cxListDestroy(asc_context.windows); | |
90 | ||
91 | // quit SDL | |
92 | if (TTF_WasInit()) | |
93 | TTF_Quit(); | |
94 | SDL_Quit(); | |
95 | ||
96 | // destroy the error buffer | |
97 | cxBufferDestroy(&asc_context.error_buffer); | |
98 | asc_context.flags = 0; | |
99 | asc_dprintf("Ascension context destroyed."); | |
100 | } | |
101 | ||
102 | void asc_error_cchar(char const* text) { | |
103 | asc_error_cxstr(cx_str(text)); | |
104 | } | |
105 | ||
106 | void asc_error_cuchar(unsigned char const* text) { | |
107 | asc_error_cxstr(cx_str((char const*)text)); | |
108 | } | |
109 | ||
110 | void asc_error_cxstr(cxstring text) { | |
111 | if (text.length == 0) return; | |
112 | ||
113 | // write error to debug output | |
114 | asc_dprintf("ERROR: %*.s", (int)text.length, text.ptr); | |
115 | ||
116 | // write error to buffer | |
117 | CxBuffer* buf = &asc_context.error_buffer; | |
118 | cxBufferWrite(text.ptr, 1, text.length, buf); | |
119 | cxBufferPut(buf, '\n'); | |
120 | ||
121 | asc_set_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); | |
122 | } | |
123 | ||
124 | bool asc_has_error(void) { | |
125 | return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR); | |
126 | } | |
127 | ||
128 | char const* asc_get_error(void) { | |
129 | // we zero-terminate the current buffer contents before providing them | |
130 | cxBufferPut(&asc_context.error_buffer, 0); | |
131 | --asc_context.error_buffer.pos; | |
132 | --asc_context.error_buffer.size; | |
133 | return asc_context.error_buffer.space; | |
134 | } | |
135 | ||
136 | void asc_clear_error(void) { | |
137 | cxBufferClear(&asc_context.error_buffer); | |
138 | asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); | |
139 | } | |
140 | ||
141 | static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { | |
142 | CxIterator iter = cxListIterator(asc_context.windows); | |
143 | cx_foreach(AscWindow*, w, iter) { | |
144 | if (w->id == id) { | |
145 | w->width = width; | |
146 | w->height = height; | |
147 | return; | |
148 | } | |
149 | } | |
150 | } | |
151 | ||
152 | bool asc_loop_next(void) { | |
153 | // dispatch SDL events | |
154 | SDL_Event event; | |
155 | while (SDL_PollEvent(&event)) { | |
156 | switch (event.type) { | |
157 | case SDL_QUIT:return false; | |
158 | case SDL_WINDOWEVENT: { | |
159 | if (event.window.type == SDL_WINDOWEVENT_RESIZED) | |
160 | asc_event_window_resized( | |
161 | event.window.windowID, | |
162 | event.window.data1, | |
163 | event.window.data2 | |
164 | ); | |
165 | break; | |
166 | } | |
167 | case SDL_KEYDOWN: | |
168 | // TODO: remove this code and implement key press map instead | |
169 | if (event.key.keysym.sym == SDLK_ESCAPE) | |
170 | return false; | |
171 | break; | |
172 | case SDL_KEYUP: | |
173 | // TODO: implement key press map | |
174 | break; | |
175 | } | |
176 | } | |
177 | ||
178 | // sync the windows | |
179 | CxMutIterator windows = cxListMutIterator(asc_context.windows); | |
180 | cx_foreach(AscWindow*, w, windows) { | |
181 | asc_window_sync(w); | |
182 | } | |
183 | return true; | |
184 | } | |
185 | ||
186 | void asc_window_settings_init_defaults(AscWindowSettings* settings) { | |
187 | settings->depth_size = 24; | |
188 | settings->vsync = 1; | |
189 | settings->width = 800; | |
190 | settings->height = 600; | |
191 | settings->fullscreen = 0; | |
192 | settings->gl_major_version = 3; | |
193 | settings->gl_minor_version = 3; | |
194 | settings->title = "Ascended Window"; | |
195 | } | |
196 | ||
197 | void asc_window_initialize(AscWindow* window, AscWindowSettings const* settings) { | |
198 | Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; | |
199 | flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; | |
200 | ||
201 | window->window = SDL_CreateWindow( | |
202 | settings->title, | |
203 | SDL_WINDOWPOS_CENTERED, | |
204 | SDL_WINDOWPOS_CENTERED, | |
205 | settings->width, | |
206 | settings->height, | |
207 | flags | |
208 | ); | |
209 | if (window->window == NULL) { | |
210 | asc_error(SDL_GetError()); | |
211 | return; | |
212 | } | |
213 | ||
214 | window->id = SDL_GetWindowID(window->window); | |
215 | SDL_GetWindowSize(window->window, &window->width, &window->height); | |
216 | ||
217 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
218 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); | |
219 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); | |
220 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); | |
221 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
222 | window->glctx = SDL_GL_CreateContext(window->window); | |
223 | if (window->glctx == NULL) { | |
224 | asc_dprintf("Creating GL context failed for window %u", window->id); | |
225 | } else { | |
226 | glewExperimental = GL_TRUE; | |
227 | GLenum err = glewInit(); | |
228 | if (err == GLEW_OK) { | |
229 | SDL_GL_SetSwapInterval(settings->vsync); | |
230 | glEnable(GL_DEPTH_TEST); | |
231 | glEnable(GL_BLEND); | |
232 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
233 | glEnable(GL_DEBUG_OUTPUT); | |
234 | glDebugMessageCallback(asc_gl_debug_callback, NULL); | |
235 | asc_dprintf("Window %u initialized", window->id); | |
236 | cxListAdd(asc_context.windows, window); | |
237 | return; | |
238 | } else { | |
239 | asc_error(glewGetErrorString(err)); | |
240 | } | |
241 | } | |
242 | ||
243 | // cleanup on error | |
244 | if (window->glctx != NULL) { | |
245 | SDL_GL_DeleteContext(window->glctx); | |
246 | } | |
247 | window->glctx = NULL; | |
248 | SDL_DestroyWindow(window->window); | |
249 | window->window = NULL; | |
250 | window->id = 0; | |
251 | } | |
252 | ||
253 | void asc_window_destroy_impl(AscWindow* window) { | |
254 | // destory the GL context and the window | |
255 | if (window->glctx != NULL) { | |
256 | SDL_GL_DeleteContext(window->glctx); | |
257 | } | |
258 | if (window->window != NULL) { | |
259 | SDL_DestroyWindow(window->window); | |
260 | } | |
261 | ||
262 | // clean the data | |
263 | asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); | |
264 | memset(window, 0, sizeof(AscWindow)); | |
265 | } | |
266 | ||
267 | void asc_window_destroy(AscWindow* window) { | |
268 | // find the window in the context and remove it | |
2
bb2bfff31f1d
fix possible double-invocation of asc_window_destroy_impl()
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
269 | bool found = false; |
0 | 270 | CxMutIterator iter = cxListMutIterator(asc_context.windows); |
271 | cx_foreach(AscWindow*, w, iter) { | |
272 | if (w == window) { | |
2
bb2bfff31f1d
fix possible double-invocation of asc_window_destroy_impl()
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
273 | found = true; |
0 | 274 | cxIteratorFlagRemoval(iter); |
275 | } | |
276 | } | |
2
bb2bfff31f1d
fix possible double-invocation of asc_window_destroy_impl()
Mike Becker <universe@uap-core.de>
parents:
0
diff
changeset
|
277 | if (!found) asc_window_destroy_impl(window); |
0 | 278 | } |
279 | ||
280 | void asc_window_sync(AscWindow const* window) { | |
281 | SDL_GL_MakeCurrent(window->window, window->glctx); | |
282 | SDL_GL_SwapWindow(window->window); | |
283 | glViewport(0, 0, window->width, window->height); | |
284 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
285 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
286 | } |