src/core.c

changeset 6
302971e8599b
parent 3
1efd6da2ad53
     1.1 --- a/src/core.c	Mon Oct 30 18:54:16 2023 +0100
     1.2 +++ b/src/core.c	Wed Nov 01 20:09:49 2023 +0100
     1.3 @@ -26,31 +26,17 @@
     1.4   */
     1.5  
     1.6  #include "ascension/core.h"
     1.7 -#include "ascension/utils.h"
     1.8  
     1.9  #include <cx/linked_list.h>
    1.10  #include <cx/printf.h>
    1.11  
    1.12 -static void asc_gl_debug_callback(
    1.13 -        GLenum source, GLenum type, GLuint id, GLenum severity,
    1.14 -        GLsizei length, const GLchar* message,
    1.15 -        const void* userParam
    1.16 -) {
    1.17 -    cxmutstr buf = cx_asprintf(
    1.18 -            "source = %d, id = %u, type = %d, severity= %d, message = %.*s",
    1.19 -            source, id, type, severity, length, message);
    1.20 -    if (type == GL_DEBUG_TYPE_ERROR) {
    1.21 -        asc_error(buf.ptr);
    1.22 -    } else {
    1.23 -        asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr);
    1.24 -    }
    1.25 -    cx_strfree(&buf);
    1.26 -}
    1.27 +#include <SDL2/SDL.h>
    1.28 +#include <SDL2/SDL_ttf.h>
    1.29  
    1.30  AscContext asc_context;
    1.31  
    1.32 -// forward declarations
    1.33 -static void asc_window_destroy_impl(AscWindow* window);
    1.34 +// forward declare the destructor functions that reside in other units
    1.35 +void asc_window_destroy_impl(void* obj);
    1.36  
    1.37  void asc_context_initialize(void) {
    1.38      if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED))
    1.39 @@ -137,153 +123,3 @@
    1.40      cxBufferClear(&asc_context.error_buffer);
    1.41      asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
    1.42  }
    1.43 -
    1.44 -static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
    1.45 -    CxIterator iter = cxListIterator(asc_context.windows);
    1.46 -    cx_foreach(AscWindow*, w, iter) {
    1.47 -        if (w->id == id) {
    1.48 -            w->dimensions.width = width;
    1.49 -            w->dimensions.height = height;
    1.50 -            return;
    1.51 -        }
    1.52 -    }
    1.53 -}
    1.54 -
    1.55 -bool asc_loop_next(void) {
    1.56 -    // dispatch SDL events
    1.57 -    SDL_Event event;
    1.58 -    while (SDL_PollEvent(&event)) {
    1.59 -        switch (event.type) {
    1.60 -        case SDL_QUIT:return false;
    1.61 -        case SDL_WINDOWEVENT: {
    1.62 -            if (event.window.type == SDL_WINDOWEVENT_RESIZED)
    1.63 -                asc_event_window_resized(
    1.64 -                        event.window.windowID,
    1.65 -                        event.window.data1,
    1.66 -                        event.window.data2
    1.67 -                );
    1.68 -            break;
    1.69 -        }
    1.70 -        case SDL_KEYDOWN:
    1.71 -            // TODO: remove this code and implement key press map instead
    1.72 -            if (event.key.keysym.sym == SDLK_ESCAPE)
    1.73 -                return false;
    1.74 -            break;
    1.75 -        case SDL_KEYUP:
    1.76 -            // TODO: implement key press map
    1.77 -            break;
    1.78 -        }
    1.79 -    }
    1.80 -
    1.81 -    // sync the windows
    1.82 -    CxMutIterator windows = cxListMutIterator(asc_context.windows);
    1.83 -    cx_foreach(AscWindow*, w, windows) {
    1.84 -        asc_window_sync(w);
    1.85 -    }
    1.86 -    return true;
    1.87 -}
    1.88 -
    1.89 -void asc_window_settings_init_defaults(AscWindowSettings* settings) {
    1.90 -    settings->depth_size = 24;
    1.91 -    settings->vsync = 1;
    1.92 -    settings->dimensions.width = 800;
    1.93 -    settings->dimensions.height = 600;
    1.94 -    settings->fullscreen = 0;
    1.95 -    settings->gl_major_version = 3;
    1.96 -    settings->gl_minor_version = 3;
    1.97 -    settings->title = "Ascended Window";
    1.98 -}
    1.99 -
   1.100 -void asc_window_initialize(AscWindow* window, AscWindowSettings const* settings) {
   1.101 -    Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
   1.102 -    flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
   1.103 -
   1.104 -    window->window = SDL_CreateWindow(
   1.105 -            settings->title,
   1.106 -            SDL_WINDOWPOS_CENTERED,
   1.107 -            SDL_WINDOWPOS_CENTERED,
   1.108 -            settings->dimensions.width,
   1.109 -            settings->dimensions.height,
   1.110 -            flags
   1.111 -    );
   1.112 -    if (window->window == NULL) {
   1.113 -        asc_error(SDL_GetError());
   1.114 -        return;
   1.115 -    }
   1.116 -
   1.117 -    window->id = SDL_GetWindowID(window->window);
   1.118 -    SDL_GetWindowSize(window->window,
   1.119 -            &window->dimensions.width,
   1.120 -            &window->dimensions.height
   1.121 -    );
   1.122 -
   1.123 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
   1.124 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);
   1.125 -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version);
   1.126 -    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size);
   1.127 -    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   1.128 -    window->glctx = SDL_GL_CreateContext(window->window);
   1.129 -    if (window->glctx == NULL) {
   1.130 -        asc_dprintf("Creating GL context failed for window %u", window->id);
   1.131 -    } else {
   1.132 -        glewExperimental = GL_TRUE;
   1.133 -        GLenum err = glewInit();
   1.134 -        if (err == GLEW_OK) {
   1.135 -            SDL_GL_SetSwapInterval(settings->vsync);
   1.136 -            glEnable(GL_DEPTH_TEST);
   1.137 -            glEnable(GL_BLEND);
   1.138 -            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   1.139 -            glEnable(GL_DEBUG_OUTPUT);
   1.140 -            glDebugMessageCallback(asc_gl_debug_callback, NULL);
   1.141 -            asc_dprintf("Window %u initialized", window->id);
   1.142 -            cxListAdd(asc_context.windows, window);
   1.143 -            return;
   1.144 -        } else {
   1.145 -            asc_error(glewGetErrorString(err));
   1.146 -        }
   1.147 -    }
   1.148 -
   1.149 -    // cleanup on error
   1.150 -    if (window->glctx != NULL) {
   1.151 -        SDL_GL_DeleteContext(window->glctx);
   1.152 -    }
   1.153 -    window->glctx = NULL;
   1.154 -    SDL_DestroyWindow(window->window);
   1.155 -    window->window = NULL;
   1.156 -    window->id = 0;
   1.157 -}
   1.158 -
   1.159 -void asc_window_destroy_impl(AscWindow* window) {
   1.160 -    // destory the GL context and the window
   1.161 -    if (window->glctx != NULL) {
   1.162 -        SDL_GL_DeleteContext(window->glctx);
   1.163 -    }
   1.164 -    if (window->window != NULL) {
   1.165 -        SDL_DestroyWindow(window->window);
   1.166 -    }
   1.167 -
   1.168 -    // clean the data
   1.169 -    asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
   1.170 -    memset(window, 0, sizeof(AscWindow));
   1.171 -}
   1.172 -
   1.173 -void asc_window_destroy(AscWindow* window) {
   1.174 -    // find the window in the context and remove it
   1.175 -    bool found = false;
   1.176 -    CxMutIterator iter = cxListMutIterator(asc_context.windows);
   1.177 -    cx_foreach(AscWindow*, w, iter) {
   1.178 -        if (w == window) {
   1.179 -            found = true;
   1.180 -            cxIteratorFlagRemoval(iter);
   1.181 -        }
   1.182 -    }
   1.183 -    if (!found) asc_window_destroy_impl(window);
   1.184 -}
   1.185 -
   1.186 -void asc_window_sync(AscWindow const* window) {
   1.187 -    SDL_GL_MakeCurrent(window->window, window->glctx);
   1.188 -    SDL_GL_SwapWindow(window->window);
   1.189 -    glViewport(0, 0, window->dimensions.width, window->dimensions.height);
   1.190 -    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
   1.191 -    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.192 -}

mercurial