src/glcontext.c

Wed, 10 Apr 2024 19:20:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Apr 2024 19:20:44 +0200
changeset 52
6d100092fcd4
parent 50
d8d2e4817db1
child 72
84472fb3adbd
permissions
-rw-r--r--

use new scale2d function in text update

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

mercurial