Wed, 08 Nov 2023 21:53:21 +0100
use OpenGL 4.0 by default
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * Copyright 2023 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "ascension/window.h" #include "ascension/context.h" #include "ascension/error.h" #include "ascension/utils.h" #include <cx/printf.h> #include <GL/glew.h> static void asc_gl_debug_callback( GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam ) { cxmutstr buf = cx_asprintf( "source = %d, id = %u, type = %d, severity= %d, message = %.*s", source, id, type, severity, length, message); if (type == GL_DEBUG_TYPE_ERROR) { asc_error(buf.ptr); } else { asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr); } cx_strfree(&buf); } static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { if (asc_context.windows[i].id == id) { asc_context.windows[i].dimensions.width = width; asc_context.windows[i].dimensions.height = height; asc_mat4f_ortho(asc_context.windows[i].projection, 0, (float) width, (float) height, 0); return; } } } bool asc_loop_next(void) { // dispatch SDL events SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: asc_set_flag(&asc_context.flags, ASC_FLAG_QUIT); break; case SDL_WINDOWEVENT: { if (event.window.type == SDL_WINDOWEVENT_RESIZED) asc_event_window_resized( event.window.windowID, event.window.data1, event.window.data2 ); break; } case SDL_KEYDOWN: // TODO: remove this code and implement key press map instead if (event.key.keysym.sym == SDLK_ESCAPE) return false; break; case SDL_KEYUP: // TODO: implement key press map break; } } // sync the windows for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { if (asc_context.windows[i].id > 0) { asc_window_sync(&asc_context.windows[i]); } } return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT); } void asc_window_settings_init_defaults(AscWindowSettings* settings) { settings->depth_size = 24; settings->vsync = 1; settings->dimensions.width = 800; settings->dimensions.height = 600; settings->fullscreen = 0; settings->gl_major_version = 4; settings->gl_minor_version = 0; settings->title = "Ascended Window"; } AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { if (index >= ASC_MAX_WINDOWS) { asc_error("Maximum number of windows exceeded."); return NULL; } AscWindow *window = &asc_context.windows[index]; if (window->id > 0) { asc_error("Cannot create window - slot already occupied."); asc_dprintf("Tried to create window with index %u", index); return NULL; } Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; window->window = SDL_CreateWindow( settings->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, settings->dimensions.width, settings->dimensions.height, flags ); if (window->window == NULL) { asc_error(SDL_GetError()); return NULL; } window->id = SDL_GetWindowID(window->window); SDL_GetWindowSize(window->window, &window->dimensions.width, &window->dimensions.height ); asc_mat4f_ortho( window->projection, 0, (float) window->dimensions.width, (float) window->dimensions.height, 0 ); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); window->glctx = SDL_GL_CreateContext(window->window); if (window->glctx == NULL) { asc_dprintf("Creating GL context failed for window %u", window->id); } else { glewExperimental = GL_TRUE; GLenum err = glewInit(); if (err == GLEW_OK) { SDL_GL_SetSwapInterval(settings->vsync); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_DEBUG_OUTPUT); glDebugMessageCallback(asc_gl_debug_callback, NULL); asc_dprintf("Window %u initialized", window->id); return window; } else { asc_error(glewGetErrorString(err)); } } // cleanup on error if (window->glctx != NULL) { SDL_GL_DeleteContext(window->glctx); } window->glctx = NULL; SDL_DestroyWindow(window->window); window->window = NULL; window->id = 0; } void asc_window_destroy(AscWindow* window) { // safeguard if (window->id == 0) return; // destroy the GL context and the window if (window->glctx != NULL) { SDL_GL_DeleteContext(window->glctx); } if (window->window != NULL) { SDL_DestroyWindow(window->window); } // clean the data asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); memset(window, 0, sizeof(AscWindow)); } void asc_window_sync(AscWindow const* window) { SDL_GL_MakeCurrent(window->window, window->glctx); SDL_GL_SwapWindow(window->window); glViewport(0, 0, window->dimensions.width, window->dimensions.height); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); }