24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 * POSSIBILITY OF SUCH DAMAGE. |
25 * POSSIBILITY OF SUCH DAMAGE. |
26 */ |
26 */ |
27 |
27 |
28 #include "ascension/core.h" |
28 #include "ascension/core.h" |
29 #include "ascension/utils.h" |
|
30 |
29 |
31 #include <cx/linked_list.h> |
30 #include <cx/linked_list.h> |
32 #include <cx/printf.h> |
31 #include <cx/printf.h> |
33 |
32 |
34 static void asc_gl_debug_callback( |
33 #include <SDL2/SDL.h> |
35 GLenum source, GLenum type, GLuint id, GLenum severity, |
34 #include <SDL2/SDL_ttf.h> |
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 |
35 |
50 AscContext asc_context; |
36 AscContext asc_context; |
51 |
37 |
52 // forward declarations |
38 // forward declare the destructor functions that reside in other units |
53 static void asc_window_destroy_impl(AscWindow* window); |
39 void asc_window_destroy_impl(void* obj); |
54 |
40 |
55 void asc_context_initialize(void) { |
41 void asc_context_initialize(void) { |
56 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) |
42 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED)) |
57 return; |
43 return; |
58 asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); |
44 asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); |
135 |
121 |
136 void asc_clear_error(void) { |
122 void asc_clear_error(void) { |
137 cxBufferClear(&asc_context.error_buffer); |
123 cxBufferClear(&asc_context.error_buffer); |
138 asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); |
124 asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR); |
139 } |
125 } |
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->dimensions.width = width; |
|
146 w->dimensions.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->dimensions.width = 800; |
|
190 settings->dimensions.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->dimensions.width, |
|
206 settings->dimensions.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, |
|
216 &window->dimensions.width, |
|
217 &window->dimensions.height |
|
218 ); |
|
219 |
|
220 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
|
221 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); |
|
222 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); |
|
223 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); |
|
224 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
|
225 window->glctx = SDL_GL_CreateContext(window->window); |
|
226 if (window->glctx == NULL) { |
|
227 asc_dprintf("Creating GL context failed for window %u", window->id); |
|
228 } else { |
|
229 glewExperimental = GL_TRUE; |
|
230 GLenum err = glewInit(); |
|
231 if (err == GLEW_OK) { |
|
232 SDL_GL_SetSwapInterval(settings->vsync); |
|
233 glEnable(GL_DEPTH_TEST); |
|
234 glEnable(GL_BLEND); |
|
235 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
236 glEnable(GL_DEBUG_OUTPUT); |
|
237 glDebugMessageCallback(asc_gl_debug_callback, NULL); |
|
238 asc_dprintf("Window %u initialized", window->id); |
|
239 cxListAdd(asc_context.windows, window); |
|
240 return; |
|
241 } else { |
|
242 asc_error(glewGetErrorString(err)); |
|
243 } |
|
244 } |
|
245 |
|
246 // cleanup on error |
|
247 if (window->glctx != NULL) { |
|
248 SDL_GL_DeleteContext(window->glctx); |
|
249 } |
|
250 window->glctx = NULL; |
|
251 SDL_DestroyWindow(window->window); |
|
252 window->window = NULL; |
|
253 window->id = 0; |
|
254 } |
|
255 |
|
256 void asc_window_destroy_impl(AscWindow* window) { |
|
257 // destory the GL context and the window |
|
258 if (window->glctx != NULL) { |
|
259 SDL_GL_DeleteContext(window->glctx); |
|
260 } |
|
261 if (window->window != NULL) { |
|
262 SDL_DestroyWindow(window->window); |
|
263 } |
|
264 |
|
265 // clean the data |
|
266 asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); |
|
267 memset(window, 0, sizeof(AscWindow)); |
|
268 } |
|
269 |
|
270 void asc_window_destroy(AscWindow* window) { |
|
271 // find the window in the context and remove it |
|
272 bool found = false; |
|
273 CxMutIterator iter = cxListMutIterator(asc_context.windows); |
|
274 cx_foreach(AscWindow*, w, iter) { |
|
275 if (w == window) { |
|
276 found = true; |
|
277 cxIteratorFlagRemoval(iter); |
|
278 } |
|
279 } |
|
280 if (!found) asc_window_destroy_impl(window); |
|
281 } |
|
282 |
|
283 void asc_window_sync(AscWindow const* window) { |
|
284 SDL_GL_MakeCurrent(window->window, window->glctx); |
|
285 SDL_GL_SwapWindow(window->window); |
|
286 glViewport(0, 0, window->dimensions.width, window->dimensions.height); |
|
287 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
288 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
289 } |
|