src/window.c

changeset 65
9c44c55d327a
parent 64
f18dc427f86f
child 68
823c03733e42
--- a/src/window.c	Thu Apr 18 21:53:53 2024 +0200
+++ b/src/window.c	Thu Apr 18 22:53:55 2024 +0200
@@ -43,16 +43,16 @@
     settings->title = "Ascended Window";
 }
 
-AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
+void asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
     if (index >= ASC_MAX_WINDOWS) {
         asc_error("Maximum number of windows exceeded.");
-        return NULL;
+        return;
     }
     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 twice", index);
-        return NULL;
+        return;
     }
     if (window->ui != NULL) {
         asc_dprintf("Window with index %u has a dangling UI pointer", index);
@@ -72,7 +72,7 @@
     );
     if (window->window == NULL) {
         asc_error(SDL_GetError());
-        return NULL;
+        return;
     }
 
     window->id = SDL_GetWindowID(window->window);
@@ -85,27 +85,31 @@
     if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
         window->ui = asc_scene_node_empty();
         asc_dprintf("Window %u initialized", window->id);
-        asc_window_active = window;
-        return window;
+        asc_context.active_window = index;
     } else {
         asc_dprintf("Creating GL context failed for window %u", window->id);
         // cleanup on error
         SDL_DestroyWindow(window->window);
         window->window = NULL;
         window->id = 0;
-        return NULL;
     }
 }
 
-void asc_window_destroy(AscWindow* window) {
+void asc_window_destroy(unsigned int index) {
     // safeguard
-    if (window->id == 0) return;
+    if (asc_context.windows[index].id == 0) return;
 
     // this window cannot be active anymore
-    if (asc_window_active == window) {
-        asc_window_active = NULL;
+    if (asc_context.active_window == index) {
+        asc_context.active_window = ASC_MAX_WINDOWS;
     }
 
+    // pointer to the window
+    AscWindow *window = &asc_context.windows[index];
+
+    // for releasing OpenGL resources, we need to make the context current
+    asc_gl_context_activate(&window->glctx);
+
     // destroy all scenes
     asc_scene_node_free(window->ui);
     window->ui = NULL;
@@ -119,8 +123,8 @@
     }
 
     // if another window was active, make the other context current again
-    if (asc_window_active != NULL) {
-        asc_gl_context_activate(&asc_window_active->glctx);
+    if (asc_context.active_window < ASC_MAX_WINDOWS) {
+        asc_gl_context_activate(&asc_active_window->glctx);
     }
 
     // clean the data
@@ -128,15 +132,19 @@
     memset(window, 0, sizeof(AscWindow));
 }
 
-void asc_window_sync(AscWindow* window) {
-    AscWindow *active_window = asc_window_active;
-    if (window != active_window) {
-        asc_window_activate(window);
+void asc_window_sync(unsigned int index) {
+    // necessary safeguard
+    if (asc_context.windows[index].id == 0) return;
+
+    // active the window that shall be synced temporarily
+    unsigned int active_index = asc_context.active_window;
+    if (index != active_index) {
+        asc_window_activate(index);
     }
 
     // Clear the color buffer for the window frame
-    int window_width = window->dimensions.width;
-    int window_height = window->dimensions.height;
+    int window_width = asc_active_window->dimensions.width;
+    int window_height = asc_active_window->dimensions.height;
     glViewport(0, 0, window_width, window_height);
     glClear(GL_COLOR_BUFFER_BIT);
     asc_recti viewport = {0, 0, window_width, window_height};
@@ -144,20 +152,20 @@
     // Draw the UI
     AscCamera ui_camera;
     asc_camera_ortho(&ui_camera, viewport);
-    asc_scene_draw(window->ui, viewport, &ui_camera);
+    asc_scene_draw(asc_active_window->ui, viewport, &ui_camera);
 
     // Swap Buffers
-    SDL_GL_SwapWindow(window->window);
+    SDL_GL_SwapWindow(asc_active_window->window);
 
     // Clear Flags
-    window->resized = false;
+    asc_active_window->resized = false;
 
-    if (window != active_window) {
-        asc_window_activate(active_window);
+    if (index != active_index) {
+        asc_window_activate(active_index);
     }
 }
 
-void asc_window_activate(AscWindow *window) {
-    asc_gl_context_activate(&window->glctx);
-    asc_window_active = (AscWindow *)window;
+void asc_window_activate(unsigned int index) {
+    asc_context.active_window = index;
+    asc_gl_context_activate(&asc_active_window->glctx);
 }

mercurial