src/window.c

changeset 44
b3da4096c607
parent 41
df81d493716e
child 47
44457f6cb0a2
     1.1 --- a/src/window.c	Thu Mar 21 23:01:09 2024 +0100
     1.2 +++ b/src/window.c	Tue Mar 26 20:37:21 2024 +0100
     1.3 @@ -30,34 +30,16 @@
     1.4  #include "ascension/error.h"
     1.5  #include "ascension/utils.h"
     1.6  
     1.7 -#include <cx/printf.h>
     1.8 -
     1.9  #include <GL/glew.h>
    1.10  
    1.11 -static void asc_gl_debug_callback(
    1.12 -        GLenum source, GLenum type, GLuint id, GLenum severity,
    1.13 -        GLsizei length, const GLchar* message,
    1.14 -        const void* userParam
    1.15 -) {
    1.16 -    cxmutstr buf = cx_asprintf(
    1.17 -            "source = %d, id = %u, type = %d, severity= %d, message = %.*s",
    1.18 -            source, id, type, severity, length, message);
    1.19 -    if (type == GL_DEBUG_TYPE_ERROR) {
    1.20 -        asc_error(buf.ptr);
    1.21 -    } else {
    1.22 -        asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr);
    1.23 -    }
    1.24 -    cx_strfree(&buf);
    1.25 -}
    1.26 -
    1.27  void asc_window_settings_init_defaults(AscWindowSettings* settings) {
    1.28 -    settings->depth_size = 24;
    1.29 -    settings->vsync = 1;
    1.30      settings->dimensions.width = 800;
    1.31      settings->dimensions.height = 600;
    1.32      settings->fullscreen = 0;
    1.33 -    settings->gl_major_version = 4;
    1.34 -    settings->gl_minor_version = 0;
    1.35 +    settings->glsettings.depth_size = 24;
    1.36 +    settings->glsettings.vsync = 1;
    1.37 +    settings->glsettings.gl_major_version = 4;
    1.38 +    settings->glsettings.gl_minor_version = 0;
    1.39      settings->title = "Ascended Window";
    1.40  }
    1.41  
    1.42 @@ -103,44 +85,19 @@
    1.43      );
    1.44      window->resized = true; // count initial sizing as resize
    1.45  
    1.46 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    1.47 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);
    1.48 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version);
    1.49 -    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size);
    1.50 -    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    1.51 -    window->glctx = SDL_GL_CreateContext(window->window);
    1.52 -    if (window->glctx == NULL) {
    1.53 +    if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
    1.54 +        asc_dprintf("Window %u initialized", window->id);
    1.55 +        asc_context.active_window = window;
    1.56 +        asc_window_init_scenes(window);
    1.57 +        return window;
    1.58 +    } else {
    1.59          asc_dprintf("Creating GL context failed for window %u", window->id);
    1.60 -    } else {
    1.61 -        glewExperimental = GL_TRUE;
    1.62 -        GLenum err = glewInit();
    1.63 -        if (err == GLEW_OK) {
    1.64 -            SDL_GL_SetSwapInterval(settings->vsync);
    1.65 -            glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    1.66 -            glEnable(GL_DEBUG_OUTPUT);
    1.67 -            glDebugMessageCallback(asc_gl_debug_callback, NULL);
    1.68 -
    1.69 -            asc_dprintf("Window %u initialized", window->id);
    1.70 -            if (asc_primitives_init(&window->primitives)) {
    1.71 -                asc_context.active_window = window;
    1.72 -                asc_window_init_scenes(window);
    1.73 -                return window;
    1.74 -            } else {
    1.75 -                asc_dprintf("!!! Creating primitives for window %u failed !!!", window->id);
    1.76 -            }
    1.77 -        } else {
    1.78 -            asc_error(glewGetErrorString(err));
    1.79 -        }
    1.80 +        // cleanup on error
    1.81 +        SDL_DestroyWindow(window->window);
    1.82 +        window->window = NULL;
    1.83 +        window->id = 0;
    1.84 +        return NULL;
    1.85      }
    1.86 -
    1.87 -    // cleanup on error
    1.88 -    if (window->glctx != NULL) {
    1.89 -        SDL_GL_DeleteContext(window->glctx);
    1.90 -    }
    1.91 -    window->glctx = NULL;
    1.92 -    SDL_DestroyWindow(window->window);
    1.93 -    window->window = NULL;
    1.94 -    window->id = 0;
    1.95  }
    1.96  
    1.97  void asc_window_destroy(AscWindow* window) {
    1.98 @@ -155,22 +112,17 @@
    1.99      // destroy all scenes
   1.100      asc_scene_destroy(&window->ui);
   1.101  
   1.102 -    // release context related data (we have to make the GL context current for this)
   1.103 -    SDL_GL_MakeCurrent(window->window, window->glctx);
   1.104 -    asc_primitives_destroy(&window->primitives);
   1.105 +    // release context related data
   1.106 +    asc_gl_context_destroy(&window->glctx);
   1.107  
   1.108 -    // destroy the GL context and the window
   1.109 -    if (window->glctx != NULL) {
   1.110 -        SDL_GL_DeleteContext(window->glctx);
   1.111 -    }
   1.112 +    // destroy window
   1.113      if (window->window != NULL) {
   1.114          SDL_DestroyWindow(window->window);
   1.115      }
   1.116  
   1.117      // if another window was active, make the other context current again
   1.118      if (asc_context.active_window != NULL) {
   1.119 -        AscWindow const *aw = asc_context.active_window;
   1.120 -        SDL_GL_MakeCurrent(aw->window, aw->glctx);
   1.121 +        asc_gl_context_activate(&asc_context.active_window->glctx);
   1.122      }
   1.123  
   1.124      // clean the data
   1.125 @@ -179,7 +131,7 @@
   1.126  }
   1.127  
   1.128  void asc_window_sync(AscWindow* window) {
   1.129 -    AscWindow const *active_window = asc_context.active_window;
   1.130 +    AscWindow *active_window = asc_context.active_window;
   1.131      if (window != active_window) {
   1.132          asc_window_activate(window);
   1.133      }
   1.134 @@ -198,7 +150,7 @@
   1.135      }
   1.136  }
   1.137  
   1.138 -void asc_window_activate(AscWindow const *window) {
   1.139 -    SDL_GL_MakeCurrent(window->window, window->glctx);
   1.140 +void asc_window_activate(AscWindow *window) {
   1.141 +    asc_gl_context_activate(&window->glctx);
   1.142      asc_context.active_window = (AscWindow *)window;
   1.143  }

mercurial