src/window.c

changeset 44
b3da4096c607
parent 41
df81d493716e
child 47
44457f6cb0a2
--- a/src/window.c	Thu Mar 21 23:01:09 2024 +0100
+++ b/src/window.c	Tue Mar 26 20:37:21 2024 +0100
@@ -30,34 +30,16 @@
 #include "ascension/error.h"
 #include "ascension/utils.h"
 
-#include <cx/printf.h>
-
 #include <GL/glew.h>
 
-static void asc_gl_debug_callback(
-        GLenum source, GLenum type, GLuint id, GLenum severity,
-        GLsizei length, const GLchar* message,
-        const void* userParam
-) {
-    cxmutstr buf = cx_asprintf(
-            "source = %d, id = %u, type = %d, severity= %d, message = %.*s",
-            source, id, type, severity, length, message);
-    if (type == GL_DEBUG_TYPE_ERROR) {
-        asc_error(buf.ptr);
-    } else {
-        asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr);
-    }
-    cx_strfree(&buf);
-}
-
 void asc_window_settings_init_defaults(AscWindowSettings* settings) {
-    settings->depth_size = 24;
-    settings->vsync = 1;
     settings->dimensions.width = 800;
     settings->dimensions.height = 600;
     settings->fullscreen = 0;
-    settings->gl_major_version = 4;
-    settings->gl_minor_version = 0;
+    settings->glsettings.depth_size = 24;
+    settings->glsettings.vsync = 1;
+    settings->glsettings.gl_major_version = 4;
+    settings->glsettings.gl_minor_version = 0;
     settings->title = "Ascended Window";
 }
 
@@ -103,44 +85,19 @@
     );
     window->resized = true; // count initial sizing as resize
 
-    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
-    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);
-    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version);
-    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size);
-    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-    window->glctx = SDL_GL_CreateContext(window->window);
-    if (window->glctx == NULL) {
-        asc_dprintf("Creating GL context failed for window %u", window->id);
+    if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
+        asc_dprintf("Window %u initialized", window->id);
+        asc_context.active_window = window;
+        asc_window_init_scenes(window);
+        return window;
     } else {
-        glewExperimental = GL_TRUE;
-        GLenum err = glewInit();
-        if (err == GLEW_OK) {
-            SDL_GL_SetSwapInterval(settings->vsync);
-            glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
-            glEnable(GL_DEBUG_OUTPUT);
-            glDebugMessageCallback(asc_gl_debug_callback, NULL);
-
-            asc_dprintf("Window %u initialized", window->id);
-            if (asc_primitives_init(&window->primitives)) {
-                asc_context.active_window = window;
-                asc_window_init_scenes(window);
-                return window;
-            } else {
-                asc_dprintf("!!! Creating primitives for window %u failed !!!", window->id);
-            }
-        } else {
-            asc_error(glewGetErrorString(err));
-        }
+        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;
     }
-
-    // cleanup on error
-    if (window->glctx != NULL) {
-        SDL_GL_DeleteContext(window->glctx);
-    }
-    window->glctx = NULL;
-    SDL_DestroyWindow(window->window);
-    window->window = NULL;
-    window->id = 0;
 }
 
 void asc_window_destroy(AscWindow* window) {
@@ -155,22 +112,17 @@
     // destroy all scenes
     asc_scene_destroy(&window->ui);
 
-    // 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);
+    // release context related data
+    asc_gl_context_destroy(&window->glctx);
 
-    // destroy the GL context and the window
-    if (window->glctx != NULL) {
-        SDL_GL_DeleteContext(window->glctx);
-    }
+    // destroy window
     if (window->window != NULL) {
         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);
+        asc_gl_context_activate(&asc_context.active_window->glctx);
     }
 
     // clean the data
@@ -179,7 +131,7 @@
 }
 
 void asc_window_sync(AscWindow* window) {
-    AscWindow const *active_window = asc_context.active_window;
+    AscWindow *active_window = asc_context.active_window;
     if (window != active_window) {
         asc_window_activate(window);
     }
@@ -198,7 +150,7 @@
     }
 }
 
-void asc_window_activate(AscWindow const *window) {
-    SDL_GL_MakeCurrent(window->window, window->glctx);
+void asc_window_activate(AscWindow *window) {
+    asc_gl_context_activate(&window->glctx);
     asc_context.active_window = (AscWindow *)window;
 }

mercurial