src/glcontext.c

Thu, 18 Apr 2024 22:53:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 18 Apr 2024 22:53:55 +0200
changeset 65
9c44c55d327a
parent 50
d8d2e4817db1
permissions
-rw-r--r--

consistently refer to windows by ID - fixes #381

This change discovered that the font cache is completely broken. We created issue #387 for this.

universe@44 1 /*
universe@44 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@44 3 * Copyright 2023 Mike Becker. All rights reserved.
universe@44 4 *
universe@44 5 * Redistribution and use in source and binary forms, with or without
universe@44 6 * modification, are permitted provided that the following conditions are met:
universe@44 7 *
universe@44 8 * 1. Redistributions of source code must retain the above copyright
universe@44 9 * notice, this list of conditions and the following disclaimer.
universe@44 10 *
universe@44 11 * 2. Redistributions in binary form must reproduce the above copyright
universe@44 12 * notice, this list of conditions and the following disclaimer in the
universe@44 13 * documentation and/or other materials provided with the distribution.
universe@44 14 *
universe@44 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@44 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@44 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@44 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@44 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@44 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@44 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@44 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@44 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@44 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@44 25 * POSSIBILITY OF SUCH DAMAGE.
universe@44 26 */
universe@44 27
universe@44 28 #include "ascension/glcontext.h"
universe@44 29 #include "ascension/error.h"
universe@44 30
universe@44 31 #include <cx/printf.h>
universe@44 32
universe@44 33 #include <GL/glew.h>
universe@44 34
universe@44 35 static void asc_gl_debug_callback(
universe@44 36 GLenum source, GLenum type, GLuint id, GLenum severity,
universe@44 37 GLsizei length, const GLchar* message,
universe@44 38 const void* userParam
universe@44 39 ) {
universe@44 40 cxmutstr buf = cx_asprintf(
universe@44 41 "source = %d, id = %u, type = %d, severity= %d, message = %.*s",
universe@44 42 source, id, type, severity, length, message);
universe@44 43 if (type == GL_DEBUG_TYPE_ERROR) {
universe@44 44 asc_error(buf.ptr);
universe@44 45 } else {
universe@44 46 asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr);
universe@44 47 }
universe@44 48 cx_strfree(&buf);
universe@44 49 }
universe@44 50
universe@44 51 static void asc_shader_initialize_predefined(AscGLContext *ctx) {
universe@44 52 AscShaderSprite *sprite = &ctx->shader.sprite;
universe@44 53 sprite->base = asc_shader_easy_compile_and_link("shader/sprite_vtx.glsl", "shader/sprite_frag.glsl");
universe@44 54 sprite->depth = glGetUniformLocation(sprite->base.id, "depth");
universe@50 55 sprite->tex = glGetUniformLocation(sprite->base.id, "texture");
universe@44 56 }
universe@44 57
universe@44 58 static void asc_shader_destroy_predefined(AscGLContext *ctx) {
universe@44 59 asc_shader_program_destroy(ctx->shader.sprite.base);
universe@44 60 }
universe@44 61
universe@44 62 bool asc_gl_context_initialize(
universe@44 63 AscGLContext *ctx,
universe@44 64 SDL_Window *window,
universe@44 65 AscGLContextSettings const *settings
universe@44 66 ) {
universe@44 67 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
universe@44 68 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);
universe@44 69 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version);
universe@44 70 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size);
universe@44 71 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
universe@44 72 ctx->glctx = SDL_GL_CreateContext(window);
universe@44 73 if (ctx->glctx == NULL) return false;
universe@44 74 ctx->window = window;
universe@44 75
universe@44 76 glewExperimental = GL_TRUE;
universe@44 77 GLenum err = glewInit();
universe@44 78 if (err == GLEW_OK) {
universe@44 79 SDL_GL_SetSwapInterval(settings->vsync);
universe@44 80
universe@44 81 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
universe@44 82 glEnable(GL_DEBUG_OUTPUT);
universe@44 83 glDebugMessageCallback(asc_gl_debug_callback, NULL);
universe@44 84
universe@44 85 if (!asc_primitives_init(&ctx->primitives)) {
universe@44 86 asc_error("Creating primitive meshes failed");
universe@44 87 SDL_GL_DeleteContext(ctx->glctx);
universe@44 88 return false;
universe@44 89 }
universe@44 90
universe@44 91 asc_shader_initialize_predefined(ctx);
universe@44 92
universe@44 93 return true;
universe@44 94 } else {
universe@44 95 asc_error(glewGetErrorString(err));
universe@44 96 SDL_GL_DeleteContext(ctx->glctx);
universe@44 97 return false;
universe@44 98 }
universe@44 99 }
universe@44 100
universe@44 101 void asc_gl_context_destroy(AscGLContext *ctx) {
universe@44 102 if (ctx->glctx == NULL) return;
universe@44 103 SDL_GL_MakeCurrent(ctx->window, ctx->glctx);
universe@44 104
universe@44 105 asc_shader_destroy_predefined(ctx);
universe@44 106 asc_primitives_destroy(&ctx->primitives);
universe@44 107
universe@44 108 // destroy the GL context and the window
universe@44 109 SDL_GL_DeleteContext(ctx->glctx);
universe@44 110 ctx->glctx = NULL;
universe@44 111 }

mercurial