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