--- a/src/window.c Wed Nov 01 20:09:49 2023 +0100 +++ b/src/window.c Wed Nov 01 21:00:33 2023 +0100 @@ -26,6 +26,8 @@ */ #include "ascension/window.h" +#include "ascension/context.h" +#include "ascension/error.h" #include <cx/linked_list.h> #include <cx/printf.h> @@ -50,11 +52,10 @@ static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { - CxIterator iter = cxListIterator(asc_context.windows); - cx_foreach(AscWindow*, w, iter) { - if (w->id == id) { - w->dimensions.width = width; - w->dimensions.height = height; + for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { + if (asc_context.windows[i].id == id) { + asc_context.windows[i].dimensions.width = width; + asc_context.windows[i].dimensions.height = height; return; } } @@ -87,9 +88,10 @@ } // sync the windows - CxMutIterator windows = cxListMutIterator(asc_context.windows); - cx_foreach(AscWindow*, w, windows) { - asc_window_sync(w); + for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { + if (asc_context.windows[i].id > 0) { + asc_window_sync(&asc_context.windows[i]); + } } return true; } @@ -105,7 +107,18 @@ settings->title = "Ascended Window"; } -void asc_window_initialize(AscWindow* window, AscWindowSettings const* settings) { +AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { + if (index >= ASC_MAX_WINDOWS) { + asc_error("Maximum number of windows exceeded."); + return NULL; + } + AscWindow *window = &asc_context.windows[index]; + if (window->id > 0) { + asc_error("Cannot create window - slot already occupied."); + asc_dprintf("Tried to create window with index %u", index); + return NULL; + } + Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; @@ -119,7 +132,7 @@ ); if (window->window == NULL) { asc_error(SDL_GetError()); - return; + return NULL; } window->id = SDL_GetWindowID(window->window); @@ -147,8 +160,7 @@ glEnable(GL_DEBUG_OUTPUT); glDebugMessageCallback(asc_gl_debug_callback, NULL); asc_dprintf("Window %u initialized", window->id); - cxListAdd(asc_context.windows, window); - return; + return window; } else { asc_error(glewGetErrorString(err)); } @@ -164,8 +176,11 @@ window->id = 0; } -void asc_window_destroy_impl(AscWindow* window) { - // destory the GL context and the window +void asc_window_destroy(AscWindow* window) { + // safeguard + if (window->id == 0) return; + + // destroy the GL context and the window if (window->glctx != NULL) { SDL_GL_DeleteContext(window->glctx); } @@ -178,19 +193,6 @@ memset(window, 0, sizeof(AscWindow)); } -void asc_window_destroy(AscWindow* window) { - // find the window in the context and remove it - bool found = false; - CxMutIterator iter = cxListMutIterator(asc_context.windows); - cx_foreach(AscWindow*, w, iter) { - if (w == window) { - found = true; - cxIteratorFlagRemoval(iter); - } - } - if (!found) asc_window_destroy_impl(window); -} - void asc_window_sync(AscWindow const* window) { SDL_GL_MakeCurrent(window->window, window->glctx); SDL_GL_SwapWindow(window->window);