src/window.c

Thu, 18 Apr 2024 21:53:53 +0200

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

make use of the asc_window_active macro

universe@0 1 /*
universe@0 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@0 3 * Copyright 2023 Mike Becker. All rights reserved.
universe@0 4 *
universe@0 5 * Redistribution and use in source and binary forms, with or without
universe@0 6 * modification, are permitted provided that the following conditions are met:
universe@0 7 *
universe@0 8 * 1. Redistributions of source code must retain the above copyright
universe@0 9 * notice, this list of conditions and the following disclaimer.
universe@0 10 *
universe@0 11 * 2. Redistributions in binary form must reproduce the above copyright
universe@0 12 * notice, this list of conditions and the following disclaimer in the
universe@0 13 * documentation and/or other materials provided with the distribution.
universe@0 14 *
universe@0 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@0 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@0 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@0 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@0 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@0 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@0 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@0 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@0 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@0 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@0 25 * POSSIBILITY OF SUCH DAMAGE.
universe@0 26 */
universe@0 27
universe@6 28 #include "ascension/window.h"
universe@7 29 #include "ascension/context.h"
universe@7 30 #include "ascension/error.h"
universe@9 31 #include "ascension/utils.h"
universe@0 32
universe@6 33 #include <GL/glew.h>
universe@6 34
universe@0 35 void asc_window_settings_init_defaults(AscWindowSettings* settings) {
universe@3 36 settings->dimensions.width = 800;
universe@3 37 settings->dimensions.height = 600;
universe@0 38 settings->fullscreen = 0;
universe@44 39 settings->glsettings.depth_size = 24;
universe@44 40 settings->glsettings.vsync = 1;
universe@44 41 settings->glsettings.gl_major_version = 4;
universe@44 42 settings->glsettings.gl_minor_version = 0;
universe@0 43 settings->title = "Ascended Window";
universe@0 44 }
universe@0 45
universe@7 46 AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
universe@7 47 if (index >= ASC_MAX_WINDOWS) {
universe@7 48 asc_error("Maximum number of windows exceeded.");
universe@7 49 return NULL;
universe@7 50 }
universe@7 51 AscWindow *window = &asc_context.windows[index];
universe@7 52 if (window->id > 0) {
universe@7 53 asc_error("Cannot create window - slot already occupied.");
universe@47 54 asc_dprintf("Tried to create window with index %u twice", index);
universe@7 55 return NULL;
universe@7 56 }
universe@47 57 if (window->ui != NULL) {
universe@47 58 asc_dprintf("Window with index %u has a dangling UI pointer", index);
universe@47 59 asc_scene_node_free(window->ui);
universe@47 60 }
universe@7 61
universe@0 62 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
universe@0 63 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
universe@0 64
universe@0 65 window->window = SDL_CreateWindow(
universe@0 66 settings->title,
universe@0 67 SDL_WINDOWPOS_CENTERED,
universe@0 68 SDL_WINDOWPOS_CENTERED,
universe@3 69 settings->dimensions.width,
universe@3 70 settings->dimensions.height,
universe@0 71 flags
universe@0 72 );
universe@0 73 if (window->window == NULL) {
universe@0 74 asc_error(SDL_GetError());
universe@7 75 return NULL;
universe@0 76 }
universe@0 77
universe@0 78 window->id = SDL_GetWindowID(window->window);
universe@3 79 SDL_GetWindowSize(window->window,
universe@3 80 &window->dimensions.width,
universe@3 81 &window->dimensions.height
universe@3 82 );
universe@37 83 window->resized = true; // count initial sizing as resize
universe@0 84
universe@44 85 if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
universe@47 86 window->ui = asc_scene_node_empty();
universe@44 87 asc_dprintf("Window %u initialized", window->id);
universe@64 88 asc_window_active = window;
universe@44 89 return window;
universe@44 90 } else {
universe@0 91 asc_dprintf("Creating GL context failed for window %u", window->id);
universe@44 92 // cleanup on error
universe@44 93 SDL_DestroyWindow(window->window);
universe@44 94 window->window = NULL;
universe@44 95 window->id = 0;
universe@44 96 return NULL;
universe@0 97 }
universe@0 98 }
universe@0 99
universe@7 100 void asc_window_destroy(AscWindow* window) {
universe@7 101 // safeguard
universe@7 102 if (window->id == 0) return;
universe@7 103
universe@16 104 // this window cannot be active anymore
universe@64 105 if (asc_window_active == window) {
universe@64 106 asc_window_active = NULL;
universe@16 107 }
universe@16 108
universe@29 109 // destroy all scenes
universe@47 110 asc_scene_node_free(window->ui);
universe@47 111 window->ui = NULL;
universe@29 112
universe@44 113 // release context related data
universe@44 114 asc_gl_context_destroy(&window->glctx);
universe@16 115
universe@44 116 // destroy window
universe@0 117 if (window->window != NULL) {
universe@0 118 SDL_DestroyWindow(window->window);
universe@0 119 }
universe@0 120
universe@16 121 // if another window was active, make the other context current again
universe@64 122 if (asc_window_active != NULL) {
universe@64 123 asc_gl_context_activate(&asc_window_active->glctx);
universe@16 124 }
universe@16 125
universe@0 126 // clean the data
universe@0 127 asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
universe@0 128 memset(window, 0, sizeof(AscWindow));
universe@0 129 }
universe@0 130
universe@37 131 void asc_window_sync(AscWindow* window) {
universe@64 132 AscWindow *active_window = asc_window_active;
universe@16 133 if (window != active_window) {
universe@16 134 asc_window_activate(window);
universe@16 135 }
universe@29 136
universe@47 137 // Clear the color buffer for the window frame
universe@47 138 int window_width = window->dimensions.width;
universe@47 139 int window_height = window->dimensions.height;
universe@47 140 glViewport(0, 0, window_width, window_height);
universe@47 141 glClear(GL_COLOR_BUFFER_BIT);
universe@47 142 asc_recti viewport = {0, 0, window_width, window_height};
universe@47 143
universe@29 144 // Draw the UI
universe@47 145 AscCamera ui_camera;
universe@47 146 asc_camera_ortho(&ui_camera, viewport);
universe@47 147 asc_scene_draw(window->ui, viewport, &ui_camera);
universe@29 148
universe@29 149 // Swap Buffers
universe@29 150 SDL_GL_SwapWindow(window->window);
universe@29 151
universe@37 152 // Clear Flags
universe@37 153 window->resized = false;
universe@37 154
universe@16 155 if (window != active_window) {
universe@16 156 asc_window_activate(active_window);
universe@16 157 }
universe@0 158 }
universe@16 159
universe@44 160 void asc_window_activate(AscWindow *window) {
universe@44 161 asc_gl_context_activate(&window->glctx);
universe@64 162 asc_window_active = (AscWindow *)window;
universe@16 163 }

mercurial