|
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/window.h" |
|
29 |
|
30 #include <cx/linked_list.h> |
|
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 |
|
52 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) { |
|
53 CxIterator iter = cxListIterator(asc_context.windows); |
|
54 cx_foreach(AscWindow*, w, iter) { |
|
55 if (w->id == id) { |
|
56 w->dimensions.width = width; |
|
57 w->dimensions.height = height; |
|
58 return; |
|
59 } |
|
60 } |
|
61 } |
|
62 |
|
63 bool asc_loop_next(void) { |
|
64 // dispatch SDL events |
|
65 SDL_Event event; |
|
66 while (SDL_PollEvent(&event)) { |
|
67 switch (event.type) { |
|
68 case SDL_QUIT:return false; |
|
69 case SDL_WINDOWEVENT: { |
|
70 if (event.window.type == SDL_WINDOWEVENT_RESIZED) |
|
71 asc_event_window_resized( |
|
72 event.window.windowID, |
|
73 event.window.data1, |
|
74 event.window.data2 |
|
75 ); |
|
76 break; |
|
77 } |
|
78 case SDL_KEYDOWN: |
|
79 // TODO: remove this code and implement key press map instead |
|
80 if (event.key.keysym.sym == SDLK_ESCAPE) |
|
81 return false; |
|
82 break; |
|
83 case SDL_KEYUP: |
|
84 // TODO: implement key press map |
|
85 break; |
|
86 } |
|
87 } |
|
88 |
|
89 // sync the windows |
|
90 CxMutIterator windows = cxListMutIterator(asc_context.windows); |
|
91 cx_foreach(AscWindow*, w, windows) { |
|
92 asc_window_sync(w); |
|
93 } |
|
94 return true; |
|
95 } |
|
96 |
|
97 void asc_window_settings_init_defaults(AscWindowSettings* settings) { |
|
98 settings->depth_size = 24; |
|
99 settings->vsync = 1; |
|
100 settings->dimensions.width = 800; |
|
101 settings->dimensions.height = 600; |
|
102 settings->fullscreen = 0; |
|
103 settings->gl_major_version = 3; |
|
104 settings->gl_minor_version = 3; |
|
105 settings->title = "Ascended Window"; |
|
106 } |
|
107 |
|
108 void asc_window_initialize(AscWindow* window, AscWindowSettings const* settings) { |
|
109 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; |
|
110 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; |
|
111 |
|
112 window->window = SDL_CreateWindow( |
|
113 settings->title, |
|
114 SDL_WINDOWPOS_CENTERED, |
|
115 SDL_WINDOWPOS_CENTERED, |
|
116 settings->dimensions.width, |
|
117 settings->dimensions.height, |
|
118 flags |
|
119 ); |
|
120 if (window->window == NULL) { |
|
121 asc_error(SDL_GetError()); |
|
122 return; |
|
123 } |
|
124 |
|
125 window->id = SDL_GetWindowID(window->window); |
|
126 SDL_GetWindowSize(window->window, |
|
127 &window->dimensions.width, |
|
128 &window->dimensions.height |
|
129 ); |
|
130 |
|
131 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
|
132 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version); |
|
133 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version); |
|
134 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size); |
|
135 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
|
136 window->glctx = SDL_GL_CreateContext(window->window); |
|
137 if (window->glctx == NULL) { |
|
138 asc_dprintf("Creating GL context failed for window %u", window->id); |
|
139 } else { |
|
140 glewExperimental = GL_TRUE; |
|
141 GLenum err = glewInit(); |
|
142 if (err == GLEW_OK) { |
|
143 SDL_GL_SetSwapInterval(settings->vsync); |
|
144 glEnable(GL_DEPTH_TEST); |
|
145 glEnable(GL_BLEND); |
|
146 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
|
147 glEnable(GL_DEBUG_OUTPUT); |
|
148 glDebugMessageCallback(asc_gl_debug_callback, NULL); |
|
149 asc_dprintf("Window %u initialized", window->id); |
|
150 cxListAdd(asc_context.windows, window); |
|
151 return; |
|
152 } else { |
|
153 asc_error(glewGetErrorString(err)); |
|
154 } |
|
155 } |
|
156 |
|
157 // cleanup on error |
|
158 if (window->glctx != NULL) { |
|
159 SDL_GL_DeleteContext(window->glctx); |
|
160 } |
|
161 window->glctx = NULL; |
|
162 SDL_DestroyWindow(window->window); |
|
163 window->window = NULL; |
|
164 window->id = 0; |
|
165 } |
|
166 |
|
167 void asc_window_destroy_impl(AscWindow* window) { |
|
168 // destory the GL context and the window |
|
169 if (window->glctx != NULL) { |
|
170 SDL_GL_DeleteContext(window->glctx); |
|
171 } |
|
172 if (window->window != NULL) { |
|
173 SDL_DestroyWindow(window->window); |
|
174 } |
|
175 |
|
176 // clean the data |
|
177 asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); |
|
178 memset(window, 0, sizeof(AscWindow)); |
|
179 } |
|
180 |
|
181 void asc_window_destroy(AscWindow* window) { |
|
182 // find the window in the context and remove it |
|
183 bool found = false; |
|
184 CxMutIterator iter = cxListMutIterator(asc_context.windows); |
|
185 cx_foreach(AscWindow*, w, iter) { |
|
186 if (w == window) { |
|
187 found = true; |
|
188 cxIteratorFlagRemoval(iter); |
|
189 } |
|
190 } |
|
191 if (!found) asc_window_destroy_impl(window); |
|
192 } |
|
193 |
|
194 void asc_window_sync(AscWindow const* window) { |
|
195 SDL_GL_MakeCurrent(window->window, window->glctx); |
|
196 SDL_GL_SwapWindow(window->window); |
|
197 glViewport(0, 0, window->dimensions.width, window->dimensions.height); |
|
198 glClearColor(0.0f, 0.0f, 0.0f, 1.0f); |
|
199 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
|
200 } |