Wed, 08 Nov 2023 21:46:29 +0100
add projection matrix to AscWindow
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/linked_list.h> |
universe@0 | 34 | #include <cx/printf.h> |
universe@0 | 35 | |
universe@6 | 36 | #include <GL/glew.h> |
universe@6 | 37 | |
universe@0 | 38 | static void asc_gl_debug_callback( |
universe@0 | 39 | GLenum source, GLenum type, GLuint id, GLenum severity, |
universe@0 | 40 | GLsizei length, const GLchar* message, |
universe@0 | 41 | const void* userParam |
universe@0 | 42 | ) { |
universe@0 | 43 | cxmutstr buf = cx_asprintf( |
universe@0 | 44 | "source = %d, id = %u, type = %d, severity= %d, message = %.*s", |
universe@0 | 45 | source, id, type, severity, length, message); |
universe@0 | 46 | if (type == GL_DEBUG_TYPE_ERROR) { |
universe@0 | 47 | asc_error(buf.ptr); |
universe@0 | 48 | } else { |
universe@0 | 49 | asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr); |
universe@0 | 50 | } |
universe@0 | 51 | cx_strfree(&buf); |
universe@0 | 52 | } |
universe@0 | 53 | |
universe@0 | 54 | |
universe@0 | 55 | static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { |
universe@7 | 56 | for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { |
universe@7 | 57 | if (asc_context.windows[i].id == id) { |
universe@7 | 58 | asc_context.windows[i].dimensions.width = width; |
universe@7 | 59 | asc_context.windows[i].dimensions.height = height; |
universe@12 | 60 | asc_mat4f_ortho(asc_context.windows[i].projection, 0, (float) width, (float) height, 0); |
universe@0 | 61 | return; |
universe@0 | 62 | } |
universe@0 | 63 | } |
universe@0 | 64 | } |
universe@0 | 65 | |
universe@0 | 66 | bool asc_loop_next(void) { |
universe@0 | 67 | // dispatch SDL events |
universe@0 | 68 | SDL_Event event; |
universe@0 | 69 | while (SDL_PollEvent(&event)) { |
universe@0 | 70 | switch (event.type) { |
universe@9 | 71 | case SDL_QUIT: |
universe@9 | 72 | asc_set_flag(&asc_context.flags, ASC_FLAG_QUIT); |
universe@9 | 73 | break; |
universe@0 | 74 | case SDL_WINDOWEVENT: { |
universe@0 | 75 | if (event.window.type == SDL_WINDOWEVENT_RESIZED) |
universe@0 | 76 | asc_event_window_resized( |
universe@0 | 77 | event.window.windowID, |
universe@0 | 78 | event.window.data1, |
universe@0 | 79 | event.window.data2 |
universe@0 | 80 | ); |
universe@0 | 81 | break; |
universe@0 | 82 | } |
universe@0 | 83 | case SDL_KEYDOWN: |
universe@0 | 84 | // TODO: remove this code and implement key press map instead |
universe@0 | 85 | if (event.key.keysym.sym == SDLK_ESCAPE) |
universe@0 | 86 | return false; |
universe@0 | 87 | break; |
universe@0 | 88 | case SDL_KEYUP: |
universe@0 | 89 | // TODO: implement key press map |
universe@0 | 90 | break; |
universe@0 | 91 | } |
universe@0 | 92 | } |
universe@0 | 93 | |
universe@0 | 94 | // sync the windows |
universe@7 | 95 | for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { |
universe@7 | 96 | if (asc_context.windows[i].id > 0) { |
universe@7 | 97 | asc_window_sync(&asc_context.windows[i]); |
universe@7 | 98 | } |
universe@0 | 99 | } |
universe@9 | 100 | |
universe@9 | 101 | return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT); |
universe@0 | 102 | } |
universe@0 | 103 | |
universe@0 | 104 | void asc_window_settings_init_defaults(AscWindowSettings* settings) { |
universe@0 | 105 | settings->depth_size = 24; |
universe@0 | 106 | settings->vsync = 1; |
universe@3 | 107 | settings->dimensions.width = 800; |
universe@3 | 108 | settings->dimensions.height = 600; |
universe@0 | 109 | settings->fullscreen = 0; |
universe@0 | 110 | settings->gl_major_version = 3; |
universe@0 | 111 | settings->gl_minor_version = 3; |
universe@0 | 112 | settings->title = "Ascended Window"; |
universe@0 | 113 | } |
universe@0 | 114 | |
universe@7 | 115 | AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { |
universe@7 | 116 | if (index >= ASC_MAX_WINDOWS) { |
universe@7 | 117 | asc_error("Maximum number of windows exceeded."); |
universe@7 | 118 | return NULL; |
universe@7 | 119 | } |
universe@7 | 120 | AscWindow *window = &asc_context.windows[index]; |
universe@7 | 121 | if (window->id > 0) { |
universe@7 | 122 | asc_error("Cannot create window - slot already occupied."); |
universe@7 | 123 | asc_dprintf("Tried to create window with index %u", index); |
universe@7 | 124 | return NULL; |
universe@7 | 125 | } |
universe@7 | 126 | |
universe@0 | 127 | Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; |
universe@0 | 128 | flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; |
universe@0 | 129 | |
universe@0 | 130 | window->window = SDL_CreateWindow( |
universe@0 | 131 | settings->title, |
universe@0 | 132 | SDL_WINDOWPOS_CENTERED, |
universe@0 | 133 | SDL_WINDOWPOS_CENTERED, |
universe@3 | 134 | settings->dimensions.width, |
universe@3 | 135 | settings->dimensions.height, |
universe@0 | 136 | flags |
universe@0 | 137 | ); |
universe@0 | 138 | if (window->window == NULL) { |
universe@0 | 139 | asc_error(SDL_GetError()); |
universe@7 | 140 | return NULL; |
universe@0 | 141 | } |
universe@0 | 142 | |
universe@0 | 143 | window->id = SDL_GetWindowID(window->window); |
universe@3 | 144 | SDL_GetWindowSize(window->window, |
universe@3 | 145 | &window->dimensions.width, |
universe@3 | 146 | &window->dimensions.height |
universe@3 | 147 | ); |
universe@12 | 148 | asc_mat4f_ortho( |
universe@12 | 149 | window->projection, |
universe@12 | 150 | 0, |
universe@12 | 151 | (float) window->dimensions.width, |
universe@12 | 152 | (float) window->dimensions.height, |
universe@12 | 153 | 0 |
universe@12 | 154 | ); |
universe@0 | 155 | |
universe@0 | 156 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
universe@0 | 157 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); |
universe@0 | 158 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); |
universe@0 | 159 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); |
universe@0 | 160 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
universe@0 | 161 | window->glctx = SDL_GL_CreateContext(window->window); |
universe@0 | 162 | if (window->glctx == NULL) { |
universe@0 | 163 | asc_dprintf("Creating GL context failed for window %u", window->id); |
universe@0 | 164 | } else { |
universe@0 | 165 | glewExperimental = GL_TRUE; |
universe@0 | 166 | GLenum err = glewInit(); |
universe@0 | 167 | if (err == GLEW_OK) { |
universe@0 | 168 | SDL_GL_SetSwapInterval(settings->vsync); |
universe@0 | 169 | glEnable(GL_DEPTH_TEST); |
universe@0 | 170 | glEnable(GL_BLEND); |
universe@0 | 171 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
universe@0 | 172 | glEnable(GL_DEBUG_OUTPUT); |
universe@0 | 173 | glDebugMessageCallback(asc_gl_debug_callback, NULL); |
universe@0 | 174 | asc_dprintf("Window %u initialized", window->id); |
universe@7 | 175 | return window; |
universe@0 | 176 | } else { |
universe@0 | 177 | asc_error(glewGetErrorString(err)); |
universe@0 | 178 | } |
universe@0 | 179 | } |
universe@0 | 180 | |
universe@0 | 181 | // cleanup on error |
universe@0 | 182 | if (window->glctx != NULL) { |
universe@0 | 183 | SDL_GL_DeleteContext(window->glctx); |
universe@0 | 184 | } |
universe@0 | 185 | window->glctx = NULL; |
universe@0 | 186 | SDL_DestroyWindow(window->window); |
universe@0 | 187 | window->window = NULL; |
universe@0 | 188 | window->id = 0; |
universe@0 | 189 | } |
universe@0 | 190 | |
universe@7 | 191 | void asc_window_destroy(AscWindow* window) { |
universe@7 | 192 | // safeguard |
universe@7 | 193 | if (window->id == 0) return; |
universe@7 | 194 | |
universe@7 | 195 | // destroy the GL context and the window |
universe@0 | 196 | if (window->glctx != NULL) { |
universe@0 | 197 | SDL_GL_DeleteContext(window->glctx); |
universe@0 | 198 | } |
universe@0 | 199 | if (window->window != NULL) { |
universe@0 | 200 | SDL_DestroyWindow(window->window); |
universe@0 | 201 | } |
universe@0 | 202 | |
universe@0 | 203 | // clean the data |
universe@0 | 204 | asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); |
universe@0 | 205 | memset(window, 0, sizeof(AscWindow)); |
universe@0 | 206 | } |
universe@0 | 207 | |
universe@0 | 208 | void asc_window_sync(AscWindow const* window) { |
universe@0 | 209 | SDL_GL_MakeCurrent(window->window, window->glctx); |
universe@0 | 210 | SDL_GL_SwapWindow(window->window); |
universe@3 | 211 | glViewport(0, 0, window->dimensions.width, window->dimensions.height); |
universe@0 | 212 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
universe@0 | 213 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
universe@0 | 214 | } |