Wed, 08 Nov 2023 21:46:29 +0100
add projection matrix to AscWindow
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 */
28 #include "ascension/window.h"
29 #include "ascension/context.h"
30 #include "ascension/error.h"
31 #include "ascension/utils.h"
33 #include <cx/linked_list.h>
34 #include <cx/printf.h>
36 #include <GL/glew.h>
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 }
55 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
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;
60 asc_mat4f_ortho(asc_context.windows[i].projection, 0, (float) width, (float) height, 0);
61 return;
62 }
63 }
64 }
66 bool asc_loop_next(void) {
67 // dispatch SDL events
68 SDL_Event event;
69 while (SDL_PollEvent(&event)) {
70 switch (event.type) {
71 case SDL_QUIT:
72 asc_set_flag(&asc_context.flags, ASC_FLAG_QUIT);
73 break;
74 case SDL_WINDOWEVENT: {
75 if (event.window.type == SDL_WINDOWEVENT_RESIZED)
76 asc_event_window_resized(
77 event.window.windowID,
78 event.window.data1,
79 event.window.data2
80 );
81 break;
82 }
83 case SDL_KEYDOWN:
84 // TODO: remove this code and implement key press map instead
85 if (event.key.keysym.sym == SDLK_ESCAPE)
86 return false;
87 break;
88 case SDL_KEYUP:
89 // TODO: implement key press map
90 break;
91 }
92 }
94 // sync the windows
95 for (unsigned int i = 0 ; i < ASC_MAX_WINDOWS ; i++) {
96 if (asc_context.windows[i].id > 0) {
97 asc_window_sync(&asc_context.windows[i]);
98 }
99 }
101 return !asc_test_flag(asc_context.flags, ASC_FLAG_QUIT);
102 }
104 void asc_window_settings_init_defaults(AscWindowSettings* settings) {
105 settings->depth_size = 24;
106 settings->vsync = 1;
107 settings->dimensions.width = 800;
108 settings->dimensions.height = 600;
109 settings->fullscreen = 0;
110 settings->gl_major_version = 3;
111 settings->gl_minor_version = 3;
112 settings->title = "Ascended Window";
113 }
115 AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
116 if (index >= ASC_MAX_WINDOWS) {
117 asc_error("Maximum number of windows exceeded.");
118 return NULL;
119 }
120 AscWindow *window = &asc_context.windows[index];
121 if (window->id > 0) {
122 asc_error("Cannot create window - slot already occupied.");
123 asc_dprintf("Tried to create window with index %u", index);
124 return NULL;
125 }
127 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
128 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
130 window->window = SDL_CreateWindow(
131 settings->title,
132 SDL_WINDOWPOS_CENTERED,
133 SDL_WINDOWPOS_CENTERED,
134 settings->dimensions.width,
135 settings->dimensions.height,
136 flags
137 );
138 if (window->window == NULL) {
139 asc_error(SDL_GetError());
140 return NULL;
141 }
143 window->id = SDL_GetWindowID(window->window);
144 SDL_GetWindowSize(window->window,
145 &window->dimensions.width,
146 &window->dimensions.height
147 );
148 asc_mat4f_ortho(
149 window->projection,
150 0,
151 (float) window->dimensions.width,
152 (float) window->dimensions.height,
153 0
154 );
156 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
157 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);
158 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version);
159 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size);
160 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
161 window->glctx = SDL_GL_CreateContext(window->window);
162 if (window->glctx == NULL) {
163 asc_dprintf("Creating GL context failed for window %u", window->id);
164 } else {
165 glewExperimental = GL_TRUE;
166 GLenum err = glewInit();
167 if (err == GLEW_OK) {
168 SDL_GL_SetSwapInterval(settings->vsync);
169 glEnable(GL_DEPTH_TEST);
170 glEnable(GL_BLEND);
171 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
172 glEnable(GL_DEBUG_OUTPUT);
173 glDebugMessageCallback(asc_gl_debug_callback, NULL);
174 asc_dprintf("Window %u initialized", window->id);
175 return window;
176 } else {
177 asc_error(glewGetErrorString(err));
178 }
179 }
181 // cleanup on error
182 if (window->glctx != NULL) {
183 SDL_GL_DeleteContext(window->glctx);
184 }
185 window->glctx = NULL;
186 SDL_DestroyWindow(window->window);
187 window->window = NULL;
188 window->id = 0;
189 }
191 void asc_window_destroy(AscWindow* window) {
192 // safeguard
193 if (window->id == 0) return;
195 // destroy the GL context and the window
196 if (window->glctx != NULL) {
197 SDL_GL_DeleteContext(window->glctx);
198 }
199 if (window->window != NULL) {
200 SDL_DestroyWindow(window->window);
201 }
203 // clean the data
204 asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
205 memset(window, 0, sizeof(AscWindow));
206 }
208 void asc_window_sync(AscWindow const* window) {
209 SDL_GL_MakeCurrent(window->window, window->glctx);
210 SDL_GL_SwapWindow(window->window);
211 glViewport(0, 0, window->dimensions.width, window->dimensions.height);
212 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
213 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
214 }