src/window.c

Tue, 26 Mar 2024 20:37:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 26 Mar 2024 20:37:21 +0100
changeset 44
b3da4096c607
parent 41
df81d493716e
child 47
44457f6cb0a2
permissions
-rw-r--r--

create own compilation unit for GL context - fixes shader not being created per context

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@29 46 static void asc_window_init_scenes(AscWindow *window) {
universe@29 47 asc_scene_init(&window->ui);
universe@37 48 asc_camera_ortho(&window->ui.cameras[0], (asc_recti){
universe@37 49 0, 0, window->dimensions
universe@37 50 });
universe@29 51 }
universe@29 52
universe@7 53 AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
universe@7 54 if (index >= ASC_MAX_WINDOWS) {
universe@7 55 asc_error("Maximum number of windows exceeded.");
universe@7 56 return NULL;
universe@7 57 }
universe@7 58 AscWindow *window = &asc_context.windows[index];
universe@7 59 if (window->id > 0) {
universe@7 60 asc_error("Cannot create window - slot already occupied.");
universe@7 61 asc_dprintf("Tried to create window with index %u", index);
universe@7 62 return NULL;
universe@7 63 }
universe@7 64
universe@0 65 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
universe@0 66 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
universe@0 67
universe@0 68 window->window = SDL_CreateWindow(
universe@0 69 settings->title,
universe@0 70 SDL_WINDOWPOS_CENTERED,
universe@0 71 SDL_WINDOWPOS_CENTERED,
universe@3 72 settings->dimensions.width,
universe@3 73 settings->dimensions.height,
universe@0 74 flags
universe@0 75 );
universe@0 76 if (window->window == NULL) {
universe@0 77 asc_error(SDL_GetError());
universe@7 78 return NULL;
universe@0 79 }
universe@0 80
universe@0 81 window->id = SDL_GetWindowID(window->window);
universe@3 82 SDL_GetWindowSize(window->window,
universe@3 83 &window->dimensions.width,
universe@3 84 &window->dimensions.height
universe@3 85 );
universe@37 86 window->resized = true; // count initial sizing as resize
universe@0 87
universe@44 88 if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) {
universe@44 89 asc_dprintf("Window %u initialized", window->id);
universe@44 90 asc_context.active_window = window;
universe@44 91 asc_window_init_scenes(window);
universe@44 92 return window;
universe@44 93 } else {
universe@0 94 asc_dprintf("Creating GL context failed for window %u", window->id);
universe@44 95 // cleanup on error
universe@44 96 SDL_DestroyWindow(window->window);
universe@44 97 window->window = NULL;
universe@44 98 window->id = 0;
universe@44 99 return NULL;
universe@0 100 }
universe@0 101 }
universe@0 102
universe@7 103 void asc_window_destroy(AscWindow* window) {
universe@7 104 // safeguard
universe@7 105 if (window->id == 0) return;
universe@7 106
universe@16 107 // this window cannot be active anymore
universe@16 108 if (asc_context.active_window == window) {
universe@16 109 asc_context.active_window = NULL;
universe@16 110 }
universe@16 111
universe@29 112 // destroy all scenes
universe@29 113 asc_scene_destroy(&window->ui);
universe@29 114
universe@44 115 // release context related data
universe@44 116 asc_gl_context_destroy(&window->glctx);
universe@16 117
universe@44 118 // destroy window
universe@0 119 if (window->window != NULL) {
universe@0 120 SDL_DestroyWindow(window->window);
universe@0 121 }
universe@0 122
universe@16 123 // if another window was active, make the other context current again
universe@16 124 if (asc_context.active_window != NULL) {
universe@44 125 asc_gl_context_activate(&asc_context.active_window->glctx);
universe@16 126 }
universe@16 127
universe@0 128 // clean the data
universe@0 129 asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
universe@0 130 memset(window, 0, sizeof(AscWindow));
universe@0 131 }
universe@0 132
universe@37 133 void asc_window_sync(AscWindow* window) {
universe@44 134 AscWindow *active_window = asc_context.active_window;
universe@16 135 if (window != active_window) {
universe@16 136 asc_window_activate(window);
universe@16 137 }
universe@29 138
universe@29 139 // Draw the UI
universe@29 140 asc_scene_draw(&window->ui);
universe@29 141
universe@29 142 // Swap Buffers
universe@29 143 SDL_GL_SwapWindow(window->window);
universe@29 144
universe@37 145 // Clear Flags
universe@37 146 window->resized = false;
universe@37 147
universe@16 148 if (window != active_window) {
universe@16 149 asc_window_activate(active_window);
universe@16 150 }
universe@0 151 }
universe@16 152
universe@44 153 void asc_window_activate(AscWindow *window) {
universe@44 154 asc_gl_context_activate(&window->glctx);
universe@34 155 asc_context.active_window = (AscWindow *)window;
universe@16 156 }

mercurial