src/window.c

Thu, 18 Apr 2024 22:53:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Apr 2024 22:53:55 +0200
changeset 65
9c44c55d327a
parent 64
f18dc427f86f
permissions
-rw-r--r--

consistently refer to windows by ID - fixes #381

This change discovered that the font cache is completely broken. We created issue #387 for this.

     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 <GL/glew.h>
    35 void asc_window_settings_init_defaults(AscWindowSettings* settings) {
    36     settings->dimensions.width = 800;
    37     settings->dimensions.height = 600;
    38     settings->fullscreen = 0;
    39     settings->glsettings.depth_size = 24;
    40     settings->glsettings.vsync = 1;
    41     settings->glsettings.gl_major_version = 4;
    42     settings->glsettings.gl_minor_version = 0;
    43     settings->title = "Ascended Window";
    44 }
    46 void asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
    47     if (index >= ASC_MAX_WINDOWS) {
    48         asc_error("Maximum number of windows exceeded.");
    49         return;
    50     }
    51     AscWindow *window = &asc_context.windows[index];
    52     if (window->id > 0) {
    53         asc_error("Cannot create window - slot already occupied.");
    54         asc_dprintf("Tried to create window with index %u twice", index);
    55         return;
    56     }
    57     if (window->ui != NULL) {
    58         asc_dprintf("Window with index %u has a dangling UI pointer", index);
    59         asc_scene_node_free(window->ui);
    60     }
    62     Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
    63     flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
    65     window->window = SDL_CreateWindow(
    66             settings->title,
    67             SDL_WINDOWPOS_CENTERED,
    68             SDL_WINDOWPOS_CENTERED,
    69             settings->dimensions.width,
    70             settings->dimensions.height,
    71             flags
    72     );
    73     if (window->window == NULL) {
    74         asc_error(SDL_GetError());
    75         return;
    76     }
    78     window->id = SDL_GetWindowID(window->window);
    79     SDL_GetWindowSize(window->window,
    80             &window->dimensions.width,
    81             &window->dimensions.height
    82     );
    83     window->resized = true; // count initial sizing as resize
    85     if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
    86         window->ui = asc_scene_node_empty();
    87         asc_dprintf("Window %u initialized", window->id);
    88         asc_context.active_window = index;
    89     } else {
    90         asc_dprintf("Creating GL context failed for window %u", window->id);
    91         // cleanup on error
    92         SDL_DestroyWindow(window->window);
    93         window->window = NULL;
    94         window->id = 0;
    95     }
    96 }
    98 void asc_window_destroy(unsigned int index) {
    99     // safeguard
   100     if (asc_context.windows[index].id == 0) return;
   102     // this window cannot be active anymore
   103     if (asc_context.active_window == index) {
   104         asc_context.active_window = ASC_MAX_WINDOWS;
   105     }
   107     // pointer to the window
   108     AscWindow *window = &asc_context.windows[index];
   110     // for releasing OpenGL resources, we need to make the context current
   111     asc_gl_context_activate(&window->glctx);
   113     // destroy all scenes
   114     asc_scene_node_free(window->ui);
   115     window->ui = NULL;
   117     // release context related data
   118     asc_gl_context_destroy(&window->glctx);
   120     // destroy window
   121     if (window->window != NULL) {
   122         SDL_DestroyWindow(window->window);
   123     }
   125     // if another window was active, make the other context current again
   126     if (asc_context.active_window < ASC_MAX_WINDOWS) {
   127         asc_gl_context_activate(&asc_active_window->glctx);
   128     }
   130     // clean the data
   131     asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
   132     memset(window, 0, sizeof(AscWindow));
   133 }
   135 void asc_window_sync(unsigned int index) {
   136     // necessary safeguard
   137     if (asc_context.windows[index].id == 0) return;
   139     // active the window that shall be synced temporarily
   140     unsigned int active_index = asc_context.active_window;
   141     if (index != active_index) {
   142         asc_window_activate(index);
   143     }
   145     // Clear the color buffer for the window frame
   146     int window_width = asc_active_window->dimensions.width;
   147     int window_height = asc_active_window->dimensions.height;
   148     glViewport(0, 0, window_width, window_height);
   149     glClear(GL_COLOR_BUFFER_BIT);
   150     asc_recti viewport = {0, 0, window_width, window_height};
   152     // Draw the UI
   153     AscCamera ui_camera;
   154     asc_camera_ortho(&ui_camera, viewport);
   155     asc_scene_draw(asc_active_window->ui, viewport, &ui_camera);
   157     // Swap Buffers
   158     SDL_GL_SwapWindow(asc_active_window->window);
   160     // Clear Flags
   161     asc_active_window->resized = false;
   163     if (index != active_index) {
   164         asc_window_activate(active_index);
   165     }
   166 }
   168 void asc_window_activate(unsigned int index) {
   169     asc_context.active_window = index;
   170     asc_gl_context_activate(&asc_active_window->glctx);
   171 }

mercurial