Tue, 07 Nov 2023 20:59:10 +0100
use the context flag to quit the application
0 | 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 | ||
6
302971e8599b
move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
28 | #include "ascension/window.h" |
7 | 29 | #include "ascension/context.h" |
30 | #include "ascension/error.h" | |
9
6ad1a4213954
use the context flag to quit the application
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
31 | #include "ascension/utils.h" |
0 | 32 | |
33 | #include <cx/linked_list.h> | |
34 | #include <cx/printf.h> | |
35 | ||
6
302971e8599b
move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
36 | #include <GL/glew.h> |
302971e8599b
move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
37 | |
0 | 38 | static void asc_gl_debug_callback( |
39 | GLenum source, GLenum type, GLuint id, GLenum severity, | |
40 | GLsizei length, const GLchar* message, | |
41 | const void* userParam | |
42 | ) { | |
43 | cxmutstr buf = cx_asprintf( | |
44 | "source = %d, id = %u, type = %d, severity= %d, message = %.*s", | |
45 | source, id, type, severity, length, message); | |
46 | if (type == GL_DEBUG_TYPE_ERROR) { | |
47 | asc_error(buf.ptr); | |
48 | } else { | |
49 | asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr); | |
50 | } | |
51 | cx_strfree(&buf); | |
52 | } | |
53 | ||
54 | ||
55 | static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { | |
7 | 56 | for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { |
57 | if (asc_context.windows[i].id == id) { | |
58 | asc_context.windows[i].dimensions.width = width; | |
59 | asc_context.windows[i].dimensions.height = height; | |
0 | 60 | return; |
61 | } | |
62 | } | |
63 | } | |
64 | ||
65 | bool asc_loop_next(void) { | |
66 | // dispatch SDL events | |
67 | SDL_Event event; | |
68 | while (SDL_PollEvent(&event)) { | |
69 | switch (event.type) { | |
9
6ad1a4213954
use the context flag to quit the application
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
70 | case SDL_QUIT: |
6ad1a4213954
use the context flag to quit the application
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
71 | asc_set_flag(&asc_context.flags, ASC_FLAG_QUIT); |
6ad1a4213954
use the context flag to quit the application
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
72 | break; |
0 | 73 | case SDL_WINDOWEVENT: { |
74 | if (event.window.type == SDL_WINDOWEVENT_RESIZED) | |
75 | asc_event_window_resized( | |
76 | event.window.windowID, | |
77 | event.window.data1, | |
78 | event.window.data2 | |
79 | ); | |
80 | break; | |
81 | } | |
82 | case SDL_KEYDOWN: | |
83 | // TODO: remove this code and implement key press map instead | |
84 | if (event.key.keysym.sym == SDLK_ESCAPE) | |
85 | return false; | |
86 | break; | |
87 | case SDL_KEYUP: | |
88 | // TODO: implement key press map | |
89 | break; | |
90 | } | |
91 | } | |
92 | ||
93 | // sync the windows | |
7 | 94 | for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) { |
95 | if (asc_context.windows[i].id > 0) { | |
96 | asc_window_sync(&asc_context.windows[i]); | |
97 | } | |
0 | 98 | } |
9
6ad1a4213954
use the context flag to quit the application
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
99 | |
6ad1a4213954
use the context flag to quit the application
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
100 | return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT); |
0 | 101 | } |
102 | ||
103 | void asc_window_settings_init_defaults(AscWindowSettings* settings) { | |
104 | settings->depth_size = 24; | |
105 | settings->vsync = 1; | |
3 | 106 | settings->dimensions.width = 800; |
107 | settings->dimensions.height = 600; | |
0 | 108 | settings->fullscreen = 0; |
109 | settings->gl_major_version = 3; | |
110 | settings->gl_minor_version = 3; | |
111 | settings->title = "Ascended Window"; | |
112 | } | |
113 | ||
7 | 114 | AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { |
115 | if (index >= ASC_MAX_WINDOWS) { | |
116 | asc_error("Maximum number of windows exceeded."); | |
117 | return NULL; | |
118 | } | |
119 | AscWindow *window = &asc_context.windows[index]; | |
120 | if (window->id > 0) { | |
121 | asc_error("Cannot create window - slot already occupied."); | |
122 | asc_dprintf("Tried to create window with index %u", index); | |
123 | return NULL; | |
124 | } | |
125 | ||
0 | 126 | Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; |
127 | flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; | |
128 | ||
129 | window->window = SDL_CreateWindow( | |
130 | settings->title, | |
131 | SDL_WINDOWPOS_CENTERED, | |
132 | SDL_WINDOWPOS_CENTERED, | |
3 | 133 | settings->dimensions.width, |
134 | settings->dimensions.height, | |
0 | 135 | flags |
136 | ); | |
137 | if (window->window == NULL) { | |
138 | asc_error(SDL_GetError()); | |
7 | 139 | return NULL; |
0 | 140 | } |
141 | ||
142 | window->id = SDL_GetWindowID(window->window); | |
3 | 143 | SDL_GetWindowSize(window->window, |
144 | &window->dimensions.width, | |
145 | &window->dimensions.height | |
146 | ); | |
0 | 147 | |
148 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
149 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); | |
150 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); | |
151 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); | |
152 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
153 | window->glctx = SDL_GL_CreateContext(window->window); | |
154 | if (window->glctx == NULL) { | |
155 | asc_dprintf("Creating GL context failed for window %u", window->id); | |
156 | } else { | |
157 | glewExperimental = GL_TRUE; | |
158 | GLenum err = glewInit(); | |
159 | if (err == GLEW_OK) { | |
160 | SDL_GL_SetSwapInterval(settings->vsync); | |
161 | glEnable(GL_DEPTH_TEST); | |
162 | glEnable(GL_BLEND); | |
163 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
164 | glEnable(GL_DEBUG_OUTPUT); | |
165 | glDebugMessageCallback(asc_gl_debug_callback, NULL); | |
166 | asc_dprintf("Window %u initialized", window->id); | |
7 | 167 | return window; |
0 | 168 | } else { |
169 | asc_error(glewGetErrorString(err)); | |
170 | } | |
171 | } | |
172 | ||
173 | // cleanup on error | |
174 | if (window->glctx != NULL) { | |
175 | SDL_GL_DeleteContext(window->glctx); | |
176 | } | |
177 | window->glctx = NULL; | |
178 | SDL_DestroyWindow(window->window); | |
179 | window->window = NULL; | |
180 | window->id = 0; | |
181 | } | |
182 | ||
7 | 183 | void asc_window_destroy(AscWindow* window) { |
184 | // safeguard | |
185 | if (window->id == 0) return; | |
186 | ||
187 | // destroy the GL context and the window | |
0 | 188 | if (window->glctx != NULL) { |
189 | SDL_GL_DeleteContext(window->glctx); | |
190 | } | |
191 | if (window->window != NULL) { | |
192 | SDL_DestroyWindow(window->window); | |
193 | } | |
194 | ||
195 | // clean the data | |
196 | asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); | |
197 | memset(window, 0, sizeof(AscWindow)); | |
198 | } | |
199 | ||
200 | void asc_window_sync(AscWindow const* window) { | |
201 | SDL_GL_MakeCurrent(window->window, window->glctx); | |
202 | SDL_GL_SwapWindow(window->window); | |
3 | 203 | glViewport(0, 0, window->dimensions.width, window->dimensions.height); |
0 | 204 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
205 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
206 | } |