src/window.c

Wed, 06 Mar 2024 23:12:56 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Mar 2024 23:12:56 +0100
changeset 34
45d29d7221cc
parent 29
1d001eb694dc
child 37
8a8cc6725b48
permissions
-rw-r--r--

fix const-warning

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  * Copyright 2023 Mike Becker. All rights reserved.
     4  *
     5  * Redistribution and use in source and binary forms, with or without
     6  * modification, are permitted provided that the following conditions are met:
     7  *
     8  *   1. Redistributions of source code must retain the above copyright
     9  *      notice, this list of conditions and the following disclaimer.
    10  *
    11  *   2. Redistributions in binary form must reproduce the above copyright
    12  *      notice, this list of conditions and the following disclaimer in the
    13  *      documentation and/or other materials provided with the distribution.
    14  *
    15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    25  * POSSIBILITY OF SUCH DAMAGE.
    26  */
    28 #include "ascension/window.h"
    29 #include "ascension/context.h"
    30 #include "ascension/error.h"
    31 #include "ascension/utils.h"
    33 #include <cx/printf.h>
    35 #include <GL/glew.h>
    37 static void asc_gl_debug_callback(
    38         GLenum source, GLenum type, GLuint id, GLenum severity,
    39         GLsizei length, const GLchar* message,
    40         const void* userParam
    41 ) {
    42     cxmutstr buf = cx_asprintf(
    43             "source = %d, id = %u, type = %d, severity= %d, message = %.*s",
    44             source, id, type, severity, length, message);
    45     if (type == GL_DEBUG_TYPE_ERROR) {
    46         asc_error(buf.ptr);
    47     } else {
    48         asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr);
    49     }
    50     cx_strfree(&buf);
    51 }
    53 void asc_window_settings_init_defaults(AscWindowSettings* settings) {
    54     settings->depth_size = 24;
    55     settings->vsync = 1;
    56     settings->dimensions.width = 800;
    57     settings->dimensions.height = 600;
    58     settings->fullscreen = 0;
    59     settings->gl_major_version = 4;
    60     settings->gl_minor_version = 0;
    61     settings->title = "Ascended Window";
    62 }
    64 static void asc_window_init_scenes(AscWindow *window) {
    65     asc_scene_init(&window->ui);
    66 }
    68 AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
    69     if (index >= ASC_MAX_WINDOWS) {
    70         asc_error("Maximum number of windows exceeded.");
    71         return NULL;
    72     }
    73     AscWindow *window = &asc_context.windows[index];
    74     if (window->id > 0) {
    75         asc_error("Cannot create window - slot already occupied.");
    76         asc_dprintf("Tried to create window with index %u", index);
    77         return NULL;
    78     }
    80     Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
    81     flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
    83     window->window = SDL_CreateWindow(
    84             settings->title,
    85             SDL_WINDOWPOS_CENTERED,
    86             SDL_WINDOWPOS_CENTERED,
    87             settings->dimensions.width,
    88             settings->dimensions.height,
    89             flags
    90     );
    91     if (window->window == NULL) {
    92         asc_error(SDL_GetError());
    93         return NULL;
    94     }
    96     window->id = SDL_GetWindowID(window->window);
    97     SDL_GetWindowSize(window->window,
    98             &window->dimensions.width,
    99             &window->dimensions.height
   100     );
   101     asc_mat4f_ortho(
   102             window->projection,
   103             0,
   104             (float) window->dimensions.width,
   105             (float) window->dimensions.height,
   106             0
   107     );
   109     SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
   110     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);
   111     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version);
   112     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size);
   113     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
   114     window->glctx = SDL_GL_CreateContext(window->window);
   115     if (window->glctx == NULL) {
   116         asc_dprintf("Creating GL context failed for window %u", window->id);
   117     } else {
   118         glewExperimental = GL_TRUE;
   119         GLenum err = glewInit();
   120         if (err == GLEW_OK) {
   121             SDL_GL_SetSwapInterval(settings->vsync);
   122             glEnable(GL_DEPTH_TEST);
   123             glEnable(GL_BLEND);
   124             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   125             glEnable(GL_DEBUG_OUTPUT);
   126             glDebugMessageCallback(asc_gl_debug_callback, NULL);
   128             asc_dprintf("Window %u initialized", window->id);
   129             if (asc_primitives_init(&window->primitives)) {
   130                 asc_window_init_scenes(window);
   131                 asc_context.active_window = window;
   132                 return window;
   133             } else {
   134                 asc_dprintf("!!! Creating primitives for window %u failed !!!", window->id);
   135             }
   136         } else {
   137             asc_error(glewGetErrorString(err));
   138         }
   139     }
   141     // cleanup on error
   142     if (window->glctx != NULL) {
   143         SDL_GL_DeleteContext(window->glctx);
   144     }
   145     window->glctx = NULL;
   146     SDL_DestroyWindow(window->window);
   147     window->window = NULL;
   148     window->id = 0;
   149 }
   151 void asc_window_destroy(AscWindow* window) {
   152     // safeguard
   153     if (window->id == 0) return;
   155     // this window cannot be active anymore
   156     if (asc_context.active_window == window) {
   157         asc_context.active_window = NULL;
   158     }
   160     // destroy all scenes
   161     asc_scene_destroy(&window->ui);
   163     // release context related data (we have to make the GL context current for this)
   164     SDL_GL_MakeCurrent(window->window, window->glctx);
   165     asc_primitives_destroy(&window->primitives);
   167     // destroy the GL context and the window
   168     if (window->glctx != NULL) {
   169         SDL_GL_DeleteContext(window->glctx);
   170     }
   171     if (window->window != NULL) {
   172         SDL_DestroyWindow(window->window);
   173     }
   175     // if another window was active, make the other context current again
   176     if (asc_context.active_window != NULL) {
   177         AscWindow const *aw = asc_context.active_window;
   178         SDL_GL_MakeCurrent(aw->window, aw->glctx);
   179     }
   181     // clean the data
   182     asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
   183     memset(window, 0, sizeof(AscWindow));
   184 }
   186 void asc_window_sync(AscWindow const* window) {
   187     AscWindow const *active_window = asc_context.active_window;
   188     if (window != active_window) {
   189         asc_window_activate(window);
   190     }
   192     // Clear viewport for new frame
   193     glViewport(0, 0, window->dimensions.width, window->dimensions.height);
   194     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
   195     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   197     // Draw the UI
   198     asc_scene_draw(&window->ui);
   200     // Swap Buffers
   201     SDL_GL_SwapWindow(window->window);
   203     if (window != active_window) {
   204         asc_window_activate(active_window);
   205     }
   206 }
   208 void asc_window_activate(AscWindow const *window) {
   209     SDL_GL_MakeCurrent(window->window, window->glctx);
   210     asc_context.active_window = (AscWindow *)window;
   211 }

mercurial