Mon, 18 Dec 2023 19:04:44 +0100
add first draft of a scene graph structure
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/printf.h> | |
34 | ||
6
302971e8599b
move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
35 | #include <GL/glew.h> |
302971e8599b
move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
36 | |
0 | 37 | static void asc_gl_debug_callback( |
38 | GLenum source, GLenum type, GLuint id, GLenum severity, | |
39 | GLsizei length, const GLchar* message, | |
40 | const void* userParam | |
41 | ) { | |
42 | cxmutstr buf = cx_asprintf( | |
43 | "source = %d, id = %u, type = %d, severity= %d, message = %.*s", | |
44 | source, id, type, severity, length, message); | |
45 | if (type == GL_DEBUG_TYPE_ERROR) { | |
46 | asc_error(buf.ptr); | |
47 | } else { | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
48 | asc_dprintf("GL debug: %.*s", (int)buf.length, buf.ptr); |
0 | 49 | } |
50 | cx_strfree(&buf); | |
51 | } | |
52 | ||
53 | void asc_window_settings_init_defaults(AscWindowSettings* settings) { | |
54 | settings->depth_size = 24; | |
55 | settings->vsync = 1; | |
3 | 56 | settings->dimensions.width = 800; |
57 | settings->dimensions.height = 600; | |
0 | 58 | settings->fullscreen = 0; |
13
f04a49b2aeee
use OpenGL 4.0 by default
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
59 | settings->gl_major_version = 4; |
f04a49b2aeee
use OpenGL 4.0 by default
Mike Becker <universe@uap-core.de>
parents:
12
diff
changeset
|
60 | settings->gl_minor_version = 0; |
0 | 61 | settings->title = "Ascended Window"; |
62 | } | |
63 | ||
7 | 64 | AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { |
65 | if (index >= ASC_MAX_WINDOWS) { | |
66 | asc_error("Maximum number of windows exceeded."); | |
67 | return NULL; | |
68 | } | |
69 | AscWindow *window = &asc_context.windows[index]; | |
70 | if (window->id > 0) { | |
71 | asc_error("Cannot create window - slot already occupied."); | |
72 | asc_dprintf("Tried to create window with index %u", index); | |
73 | return NULL; | |
74 | } | |
75 | ||
0 | 76 | Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; |
77 | flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; | |
78 | ||
79 | window->window = SDL_CreateWindow( | |
80 | settings->title, | |
81 | SDL_WINDOWPOS_CENTERED, | |
82 | SDL_WINDOWPOS_CENTERED, | |
3 | 83 | settings->dimensions.width, |
84 | settings->dimensions.height, | |
0 | 85 | flags |
86 | ); | |
87 | if (window->window == NULL) { | |
88 | asc_error(SDL_GetError()); | |
7 | 89 | return NULL; |
0 | 90 | } |
91 | ||
92 | window->id = SDL_GetWindowID(window->window); | |
3 | 93 | SDL_GetWindowSize(window->window, |
94 | &window->dimensions.width, | |
95 | &window->dimensions.height | |
96 | ); | |
12
d89e0ebc76d2
add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
97 | asc_mat4f_ortho( |
d89e0ebc76d2
add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
98 | window->projection, |
d89e0ebc76d2
add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
99 | 0, |
d89e0ebc76d2
add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
100 | (float) window->dimensions.width, |
d89e0ebc76d2
add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
101 | (float) window->dimensions.height, |
d89e0ebc76d2
add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
102 | 0 |
d89e0ebc76d2
add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents:
9
diff
changeset
|
103 | ); |
0 | 104 | |
105 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
106 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); | |
107 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); | |
108 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); | |
109 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
110 | window->glctx = SDL_GL_CreateContext(window->window); | |
111 | if (window->glctx == NULL) { | |
112 | asc_dprintf("Creating GL context failed for window %u", window->id); | |
113 | } else { | |
114 | glewExperimental = GL_TRUE; | |
115 | GLenum err = glewInit(); | |
116 | if (err == GLEW_OK) { | |
117 | SDL_GL_SetSwapInterval(settings->vsync); | |
118 | glEnable(GL_DEPTH_TEST); | |
119 | glEnable(GL_BLEND); | |
120 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
121 | glEnable(GL_DEBUG_OUTPUT); | |
122 | glDebugMessageCallback(asc_gl_debug_callback, NULL); | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
123 | |
0 | 124 | asc_dprintf("Window %u initialized", window->id); |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
125 | if (asc_primitives_init(&window->primitives)) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
126 | asc_context.active_window = window; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
127 | return window; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
128 | } else { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
129 | asc_dprintf("!!! Creating primitives for window %u failed !!!", window->id); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
130 | } |
0 | 131 | } else { |
132 | asc_error(glewGetErrorString(err)); | |
133 | } | |
134 | } | |
135 | ||
136 | // cleanup on error | |
137 | if (window->glctx != NULL) { | |
138 | SDL_GL_DeleteContext(window->glctx); | |
139 | } | |
140 | window->glctx = NULL; | |
141 | SDL_DestroyWindow(window->window); | |
142 | window->window = NULL; | |
143 | window->id = 0; | |
144 | } | |
145 | ||
7 | 146 | void asc_window_destroy(AscWindow* window) { |
147 | // safeguard | |
148 | if (window->id == 0) return; | |
149 | ||
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
150 | // this window cannot be active anymore |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
151 | if (asc_context.active_window == window) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
152 | asc_context.active_window = NULL; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
153 | } |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
154 | |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
155 | // release context related data (we have to make the GL context current for this) |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
156 | SDL_GL_MakeCurrent(window->window, window->glctx); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
157 | asc_primitives_destroy(&window->primitives); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
158 | |
7 | 159 | // destroy the GL context and the window |
0 | 160 | if (window->glctx != NULL) { |
161 | SDL_GL_DeleteContext(window->glctx); | |
162 | } | |
163 | if (window->window != NULL) { | |
164 | SDL_DestroyWindow(window->window); | |
165 | } | |
166 | ||
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
167 | // if another window was active, make the other context current again |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
168 | if (asc_context.active_window != NULL) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
169 | AscWindow const *aw = asc_context.active_window; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
170 | SDL_GL_MakeCurrent(aw->window, aw->glctx); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
171 | } |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
172 | |
0 | 173 | // clean the data |
174 | asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); | |
175 | memset(window, 0, sizeof(AscWindow)); | |
176 | } | |
177 | ||
178 | void asc_window_sync(AscWindow const* window) { | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
179 | AscWindow const *active_window = asc_context.active_window; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
180 | if (window != active_window) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
181 | asc_window_activate(window); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
182 | } |
0 | 183 | SDL_GL_SwapWindow(window->window); |
3 | 184 | glViewport(0, 0, window->dimensions.width, window->dimensions.height); |
0 | 185 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
186 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
187 | if (window != active_window) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
188 | asc_window_activate(active_window); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
189 | } |
0 | 190 | } |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
191 | |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
192 | void asc_window_activate(AscWindow const *window) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
193 | SDL_GL_MakeCurrent(window->window, window->glctx); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
194 | asc_context.active_window = window; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
195 | } |