src/glcontext.c

Sun, 01 Jun 2025 16:35:23 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 Jun 2025 16:35:23 +0200
changeset 137
f8e6e0ae61a8
parent 115
e5f8c99b0987
permissions
-rw-r--r--

remove pre-defined dummy textures by introducing conditional compilation for shaders

and by the way resolves #645

/*
 * 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/error.h"

#include "ascension/2d/sprite.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);
    }
}

static int asc_shader_initialize_predefined(AscGLContext *ctx) {
    // TODO: check if we can replace that by lazy loading shaders just like fonts
    int ret = 0;
    ret |= asc_shader_sprite_init(&ctx->shader.sprite_uv, false);
    ret |= asc_shader_sprite_init(&ctx->shader.sprite_rect, true);
    ret |= asc_error_catch_all_gl();
    return ret;
}

static void asc_shader_destroy_predefined(AscGLContext *ctx) {
    asc_shader_program_destroy(&ctx->shader.sprite_uv.program);
    asc_shader_program_destroy(&ctx->shader.sprite_rect.program);
}

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,
        AscGLContextSettings const *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);

        if (asc_shader_initialize_predefined(ctx)) {
            asc_error("Initializing predefined shaders failed");
            SDL_GL_DeleteContext(ctx->glctx);
            return false;
        }

        ctx->cleanup_funcs = cxArrayListCreateSimple(sizeof(struct asc_gl_context_cleanup_data), 8);
        cxDefineDestructor(ctx->cleanup_funcs, asc_gl_context_cleanup);

        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->cleanup_funcs);
    asc_shader_destroy_predefined(ctx);

    // 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;
}

mercurial