Thu, 21 Mar 2024 22:23:00 +0100
add correct interleaving of opaque and transparent sprites
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@0 | 33 | #include <cx/printf.h> |
universe@0 | 34 | |
universe@6 | 35 | #include <GL/glew.h> |
universe@6 | 36 | |
universe@0 | 37 | static void asc_gl_debug_callback( |
universe@0 | 38 | GLenum source, GLenum type, GLuint id, GLenum severity, |
universe@0 | 39 | GLsizei length, const GLchar* message, |
universe@0 | 40 | const void* userParam |
universe@0 | 41 | ) { |
universe@0 | 42 | cxmutstr buf = cx_asprintf( |
universe@0 | 43 | "source = %d, id = %u, type = %d, severity= %d, message = %.*s", |
universe@0 | 44 | source, id, type, severity, length, message); |
universe@0 | 45 | if (type == GL_DEBUG_TYPE_ERROR) { |
universe@0 | 46 | asc_error(buf.ptr); |
universe@0 | 47 | } else { |
universe@16 | 48 | asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr); |
universe@0 | 49 | } |
universe@0 | 50 | cx_strfree(&buf); |
universe@0 | 51 | } |
universe@0 | 52 | |
universe@0 | 53 | void asc_window_settings_init_defaults(AscWindowSettings* settings) { |
universe@0 | 54 | settings->depth_size = 24; |
universe@0 | 55 | settings->vsync = 1; |
universe@3 | 56 | settings->dimensions.width = 800; |
universe@3 | 57 | settings->dimensions.height = 600; |
universe@0 | 58 | settings->fullscreen = 0; |
universe@13 | 59 | settings->gl_major_version = 4; |
universe@13 | 60 | settings->gl_minor_version = 0; |
universe@0 | 61 | settings->title = "Ascended Window"; |
universe@0 | 62 | } |
universe@0 | 63 | |
universe@29 | 64 | static void asc_window_init_scenes(AscWindow *window) { |
universe@29 | 65 | asc_scene_init(&window->ui); |
universe@37 | 66 | asc_camera_ortho(&window->ui.cameras[0], (asc_recti){ |
universe@37 | 67 | 0, 0, window->dimensions |
universe@37 | 68 | }); |
universe@29 | 69 | } |
universe@29 | 70 | |
universe@7 | 71 | AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { |
universe@7 | 72 | if (index >= ASC_MAX_WINDOWS) { |
universe@7 | 73 | asc_error("Maximum number of windows exceeded."); |
universe@7 | 74 | return NULL; |
universe@7 | 75 | } |
universe@7 | 76 | AscWindow *window = &asc_context.windows[index]; |
universe@7 | 77 | if (window->id > 0) { |
universe@7 | 78 | asc_error("Cannot create window - slot already occupied."); |
universe@7 | 79 | asc_dprintf("Tried to create window with index %u", index); |
universe@7 | 80 | return NULL; |
universe@7 | 81 | } |
universe@7 | 82 | |
universe@0 | 83 | Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; |
universe@0 | 84 | flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; |
universe@0 | 85 | |
universe@0 | 86 | window->window = SDL_CreateWindow( |
universe@0 | 87 | settings->title, |
universe@0 | 88 | SDL_WINDOWPOS_CENTERED, |
universe@0 | 89 | SDL_WINDOWPOS_CENTERED, |
universe@3 | 90 | settings->dimensions.width, |
universe@3 | 91 | settings->dimensions.height, |
universe@0 | 92 | flags |
universe@0 | 93 | ); |
universe@0 | 94 | if (window->window == NULL) { |
universe@0 | 95 | asc_error(SDL_GetError()); |
universe@7 | 96 | return NULL; |
universe@0 | 97 | } |
universe@0 | 98 | |
universe@0 | 99 | window->id = SDL_GetWindowID(window->window); |
universe@3 | 100 | SDL_GetWindowSize(window->window, |
universe@3 | 101 | &window->dimensions.width, |
universe@3 | 102 | &window->dimensions.height |
universe@3 | 103 | ); |
universe@37 | 104 | window->resized = true; // count initial sizing as resize |
universe@0 | 105 | |
universe@0 | 106 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
universe@0 | 107 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); |
universe@0 | 108 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); |
universe@0 | 109 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); |
universe@0 | 110 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
universe@0 | 111 | window->glctx = SDL_GL_CreateContext(window->window); |
universe@0 | 112 | if (window->glctx == NULL) { |
universe@0 | 113 | asc_dprintf("Creating GL context failed for window %u", window->id); |
universe@0 | 114 | } else { |
universe@0 | 115 | glewExperimental = GL_TRUE; |
universe@0 | 116 | GLenum err = glewInit(); |
universe@0 | 117 | if (err == GLEW_OK) { |
universe@0 | 118 | SDL_GL_SetSwapInterval(settings->vsync); |
universe@41 | 119 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
universe@0 | 120 | glEnable(GL_DEBUG_OUTPUT); |
universe@0 | 121 | glDebugMessageCallback(asc_gl_debug_callback, NULL); |
universe@16 | 122 | |
universe@0 | 123 | asc_dprintf("Window %u initialized", window->id); |
universe@16 | 124 | if (asc_primitives_init(&window->primitives)) { |
universe@37 | 125 | asc_context.active_window = window; |
universe@29 | 126 | asc_window_init_scenes(window); |
universe@16 | 127 | return window; |
universe@16 | 128 | } else { |
universe@16 | 129 | asc_dprintf("!!! Creating primitives for window %u failed !!!", window->id); |
universe@16 | 130 | } |
universe@0 | 131 | } else { |
universe@0 | 132 | asc_error(glewGetErrorString(err)); |
universe@0 | 133 | } |
universe@0 | 134 | } |
universe@0 | 135 | |
universe@0 | 136 | // cleanup on error |
universe@0 | 137 | if (window->glctx != NULL) { |
universe@0 | 138 | SDL_GL_DeleteContext(window->glctx); |
universe@0 | 139 | } |
universe@0 | 140 | window->glctx = NULL; |
universe@0 | 141 | SDL_DestroyWindow(window->window); |
universe@0 | 142 | window->window = NULL; |
universe@0 | 143 | window->id = 0; |
universe@0 | 144 | } |
universe@0 | 145 | |
universe@7 | 146 | void asc_window_destroy(AscWindow* window) { |
universe@7 | 147 | // safeguard |
universe@7 | 148 | if (window->id == 0) return; |
universe@7 | 149 | |
universe@16 | 150 | // this window cannot be active anymore |
universe@16 | 151 | if (asc_context.active_window == window) { |
universe@16 | 152 | asc_context.active_window = NULL; |
universe@16 | 153 | } |
universe@16 | 154 | |
universe@29 | 155 | // destroy all scenes |
universe@29 | 156 | asc_scene_destroy(&window->ui); |
universe@29 | 157 | |
universe@16 | 158 | // release context related data (we have to make the GL context current for this) |
universe@16 | 159 | SDL_GL_MakeCurrent(window->window, window->glctx); |
universe@16 | 160 | asc_primitives_destroy(&window->primitives); |
universe@16 | 161 | |
universe@7 | 162 | // destroy the GL context and the window |
universe@0 | 163 | if (window->glctx != NULL) { |
universe@0 | 164 | SDL_GL_DeleteContext(window->glctx); |
universe@0 | 165 | } |
universe@0 | 166 | if (window->window != NULL) { |
universe@0 | 167 | SDL_DestroyWindow(window->window); |
universe@0 | 168 | } |
universe@0 | 169 | |
universe@16 | 170 | // if another window was active, make the other context current again |
universe@16 | 171 | if (asc_context.active_window != NULL) { |
universe@16 | 172 | AscWindow const *aw = asc_context.active_window; |
universe@16 | 173 | SDL_GL_MakeCurrent(aw->window, aw->glctx); |
universe@16 | 174 | } |
universe@16 | 175 | |
universe@0 | 176 | // clean the data |
universe@0 | 177 | asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); |
universe@0 | 178 | memset(window, 0, sizeof(AscWindow)); |
universe@0 | 179 | } |
universe@0 | 180 | |
universe@37 | 181 | void asc_window_sync(AscWindow* window) { |
universe@16 | 182 | AscWindow const *active_window = asc_context.active_window; |
universe@16 | 183 | if (window != active_window) { |
universe@16 | 184 | asc_window_activate(window); |
universe@16 | 185 | } |
universe@29 | 186 | |
universe@29 | 187 | // Draw the UI |
universe@29 | 188 | asc_scene_draw(&window->ui); |
universe@29 | 189 | |
universe@29 | 190 | // Swap Buffers |
universe@29 | 191 | SDL_GL_SwapWindow(window->window); |
universe@29 | 192 | |
universe@37 | 193 | // Clear Flags |
universe@37 | 194 | window->resized = false; |
universe@37 | 195 | |
universe@16 | 196 | if (window != active_window) { |
universe@16 | 197 | asc_window_activate(active_window); |
universe@16 | 198 | } |
universe@0 | 199 | } |
universe@16 | 200 | |
universe@16 | 201 | void asc_window_activate(AscWindow const *window) { |
universe@16 | 202 | SDL_GL_MakeCurrent(window->window, window->glctx); |
universe@34 | 203 | asc_context.active_window = (AscWindow *)window; |
universe@16 | 204 | } |