src/window.c

changeset 16
c5dde81b6fb2
parent 13
f04a49b2aeee
child 29
1d001eb694dc
--- a/src/window.c	Wed Nov 08 23:17:07 2023 +0100
+++ b/src/window.c	Wed Nov 15 22:51:40 2023 +0100
@@ -45,61 +45,11 @@
     if (type == GL_DEBUG_TYPE_ERROR) {
         asc_error(buf.ptr);
     } else {
-        asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr);
+        asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr);
     }
     cx_strfree(&buf);
 }
 
-
-static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 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;
-            asc_mat4f_ortho(asc_context.windows[i].projection, 0, (float) width, (float) height, 0);
-            return;
-        }
-    }
-}
-
-bool asc_loop_next(void) {
-    // dispatch SDL events
-    SDL_Event event;
-    while (SDL_PollEvent(&event)) {
-        switch (event.type) {
-        case SDL_QUIT:
-            asc_set_flag(&asc_context.flags, ASC_FLAG_QUIT);
-            break;
-        case SDL_WINDOWEVENT: {
-            if (event.window.type == SDL_WINDOWEVENT_RESIZED)
-                asc_event_window_resized(
-                        event.window.windowID,
-                        event.window.data1,
-                        event.window.data2
-                );
-            break;
-        }
-        case SDL_KEYDOWN:
-            // TODO: remove this code and implement key press map instead
-            if (event.key.keysym.sym == SDLK_ESCAPE)
-                return false;
-            break;
-        case SDL_KEYUP:
-            // TODO: implement key press map
-            break;
-        }
-    }
-
-    // sync the windows
-    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 !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT);
-}
-
 void asc_window_settings_init_defaults(AscWindowSettings* settings) {
     settings->depth_size = 24;
     settings->vsync = 1;
@@ -170,8 +120,14 @@
             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
             glEnable(GL_DEBUG_OUTPUT);
             glDebugMessageCallback(asc_gl_debug_callback, NULL);
+
             asc_dprintf("Window %u initialized", window->id);
-            return window;
+            if (asc_primitives_init(&window->primitives)) {
+                asc_context.active_window = window;
+                return window;
+            } else {
+                asc_dprintf("!!! Creating primitives for window %u failed !!!", window->id);
+            }
         } else {
             asc_error(glewGetErrorString(err));
         }
@@ -191,6 +147,15 @@
     // safeguard
     if (window->id == 0) return;
 
+    // this window cannot be active anymore
+    if (asc_context.active_window == window) {
+        asc_context.active_window = NULL;
+    }
+
+    // release context related data (we have to make the GL context current for this)
+    SDL_GL_MakeCurrent(window->window, window->glctx);
+    asc_primitives_destroy(&window->primitives);
+
     // destroy the GL context and the window
     if (window->glctx != NULL) {
         SDL_GL_DeleteContext(window->glctx);
@@ -199,15 +164,32 @@
         SDL_DestroyWindow(window->window);
     }
 
+    // if another window was active, make the other context current again
+    if (asc_context.active_window != NULL) {
+        AscWindow const *aw = asc_context.active_window;
+        SDL_GL_MakeCurrent(aw->window, aw->glctx);
+    }
+
     // clean the data
     asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
     memset(window, 0, sizeof(AscWindow));
 }
 
 void asc_window_sync(AscWindow const* window) {
-    SDL_GL_MakeCurrent(window->window, window->glctx);
+    AscWindow const *active_window = asc_context.active_window;
+    if (window != active_window) {
+        asc_window_activate(window);
+    }
     SDL_GL_SwapWindow(window->window);
     glViewport(0, 0, window->dimensions.width, window->dimensions.height);
     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+    if (window != active_window) {
+        asc_window_activate(active_window);
+    }
 }
+
+void asc_window_activate(AscWindow const *window) {
+    SDL_GL_MakeCurrent(window->window, window->glctx);
+    asc_context.active_window = window;
+}

mercurial