Wed, 02 Jul 2025 23:21:17 +0200
resolve TODOs regarding input.h
a) mouse position must be integer, because it can be negative (though rarely)
b) we should not trade "access complexity" for space in the scancodes array
/* * 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/glcontext.h" #include "ascension/shader.h" #include "ascension/error.h" #include <GL/glew.h> #include <cx/array_list.h> static void asc_gl_debug_callback( GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, __attribute__((__unused__)) const void *userParam ) { if (type == GL_DEBUG_TYPE_ERROR) { asc_error("OpenGL source = %d, id = %u, type = %d, severity= %d, message = %.*s", source, id, type, severity, length, message); } else { asc_dprintf("OpenGL source = %d, id = %u, type = %d, severity= %d, message = %.*s", source, id, type, severity, length, message); } } struct asc_gl_context_cleanup_data { int type; union { void(*f1)(void); void(*f2)(void*); void(*f3)(void*, void*); }; void *memory; void *additional_data; }; static void asc_gl_context_cleanup(struct asc_gl_context_cleanup_data *data) { switch (data->type) { case 1: data->f1(); break; case 2: data->f2(data->memory); break; default: data->f3(data->memory, data->additional_data); break; } } bool asc_gl_context_initialize( AscGLContext *ctx, SDL_Window *window, const AscGLContextSettings *settings ) { 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); ctx->glctx = SDL_GL_CreateContext(window); if (ctx->glctx == NULL) return false; ctx->window = window; glewExperimental = GL_TRUE; GLenum err = glewInit(); if (err == GLEW_OK) { SDL_GL_SetSwapInterval(settings->vsync); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEBUG_OUTPUT); glDebugMessageCallback(asc_gl_debug_callback, NULL); // Create the cleanup functions array ctx->cleanup_funcs = cxArrayListCreateSimple(sizeof(struct asc_gl_context_cleanup_data), 8); cxDefineDestructor(ctx->cleanup_funcs, asc_gl_context_cleanup); // Create the shaders array ctx->shaders = cxArrayListCreateSimple(CX_STORE_POINTERS, 32); cxDefineDestructor(ctx->shaders, asc_shader_free); ctx->active_program = 0; return true; } else { asc_error("glewInit failed: %s", glewGetErrorString(err)); SDL_GL_DeleteContext(ctx->glctx); return false; } } void asc_gl_context_destroy(AscGLContext *ctx) { if (ctx->glctx == NULL) return; SDL_GL_MakeCurrent(ctx->window, ctx->glctx); cxListFree(ctx->shaders); cxListFree(ctx->cleanup_funcs); // destroy the GL context and the window SDL_GL_DeleteContext(ctx->glctx); ctx->glctx = NULL; } void asc_gl_context_add_cleanup_func(AscGLContext *ctx, void(*func)(void)) { struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs); data->type = 1; data->f1 = func; } void asc_gl_context_add_cleanup_func2(AscGLContext *ctx, void(*func)(void*), void *memory) { struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs); data->type = 2; data->f2 = func; data->memory = memory; } void asc_gl_context_add_cleanup_func3(AscGLContext *ctx, void(*func)(void*,void*), void *memory, void *additional_data) { struct asc_gl_context_cleanup_data *data = cxListEmplace(ctx->cleanup_funcs); data->type = 3; data->f3 = func; data->memory = memory; data->additional_data = additional_data; }