src/core.c

Mon, 30 Oct 2023 17:40:34 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 30 Oct 2023 17:40:34 +0100
changeset 1
e3113d4e2fc0
parent 0
593b60458157
child 2
bb2bfff31f1d
permissions
-rw-r--r--

update uwproj files

0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 * Copyright 2023 Mike Becker. All rights reserved.
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 *
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * modification, are permitted provided that the following conditions are met:
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 *
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 *
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 *
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * POSSIBILITY OF SUCH DAMAGE.
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 */
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 #include "ascension/core.h"
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "ascension/utils.h"
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <cx/linked_list.h>
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <cx/printf.h>
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 static void asc_gl_debug_callback(
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 GLenum source, GLenum type, GLuint id, GLenum severity,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 GLsizei length, const GLchar* message,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 const void* userParam
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 ) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 cxmutstr buf = cx_asprintf(
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 "source = %d, id = %u, type = %d, severity= %d, message = %.*s",
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 source, id, type, severity, length, message);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 if (type == GL_DEBUG_TYPE_ERROR) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 asc_error(buf.ptr);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 } else {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 asc_dprintf("GL debug: %*.s", (int)buf.length, buf.ptr);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 cx_strfree(&buf);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 AscContext asc_context;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 // forward declarations
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 static void asc_window_destroy_impl(AscWindow* window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 void asc_context_initialize(void) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 if (asc_test_flag(asc_context.flags, ASC_FLAG_INITILIZED))
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 return;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 // initialize error buffer
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 cxBufferInit(
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 &asc_context.error_buffer,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 NULL,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 256,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 NULL,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 CX_BUFFER_AUTO_EXTEND
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 );
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 // initialize lists
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 asc_context.windows = cxLinkedListCreateSimple(CX_STORE_POINTERS);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 asc_context.windows->simple_destructor =
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 (cx_destructor_func)asc_window_destroy_impl;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 // initialize SDL
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 asc_error(SDL_GetError());
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 } else {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 if (TTF_Init() < 0) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 asc_error(TTF_GetError());
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 SDL_ClearError();
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 asc_set_flag(&asc_context.flags, ASC_FLAG_INITILIZED);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 asc_dprintf("Ascension context initialized.");
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 void asc_context_destroy(void) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 // destroy lists
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 cxListDestroy(asc_context.windows);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 // quit SDL
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 if (TTF_WasInit())
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 TTF_Quit();
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 SDL_Quit();
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 // destroy the error buffer
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 cxBufferDestroy(&asc_context.error_buffer);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 asc_context.flags = 0;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 asc_dprintf("Ascension context destroyed.");
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 void asc_error_cchar(char const* text) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 asc_error_cxstr(cx_str(text));
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 void asc_error_cuchar(unsigned char const* text) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 asc_error_cxstr(cx_str((char const*)text));
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 void asc_error_cxstr(cxstring text) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 if (text.length == 0) return;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 // write error to debug output
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 asc_dprintf("ERROR: %*.s", (int)text.length, text.ptr);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 // write error to buffer
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 CxBuffer* buf = &asc_context.error_buffer;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 cxBufferWrite(text.ptr, 1, text.length, buf);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 cxBufferPut(buf, '\n');
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 asc_set_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 bool asc_has_error(void) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 return asc_test_flag(asc_context.flags, ASC_FLAG_HAS_ERROR);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 char const* asc_get_error(void) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 // we zero-terminate the current buffer contents before providing them
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 cxBufferPut(&asc_context.error_buffer, 0);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 --asc_context.error_buffer.pos;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132 --asc_context.error_buffer.size;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 return asc_context.error_buffer.space;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 void asc_clear_error(void) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 cxBufferClear(&asc_context.error_buffer);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 asc_clear_flag(&asc_context.flags, ASC_FLAG_HAS_ERROR);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 static void asc_event_window_resized(Uint32 id, Sint32 width, Sint32 height) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 CxIterator iter = cxListIterator(asc_context.windows);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 cx_foreach(AscWindow*, w, iter) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 if (w->id == id) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 w->width = width;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 w->height = height;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 return;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 bool asc_loop_next(void) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 // dispatch SDL events
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 SDL_Event event;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 while (SDL_PollEvent(&event)) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 switch (event.type) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 case SDL_QUIT:return false;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 case SDL_WINDOWEVENT: {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 if (event.window.type == SDL_WINDOWEVENT_RESIZED)
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 asc_event_window_resized(
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 event.window.windowID,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 event.window.data1,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 event.window.data2
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 );
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 break;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167 case SDL_KEYDOWN:
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 // TODO: remove this code and implement key press map instead
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 if (event.key.keysym.sym == SDLK_ESCAPE)
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 return false;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 break;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 case SDL_KEYUP:
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 // TODO: implement key press map
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 break;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
176 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 // sync the windows
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 CxMutIterator windows = cxListMutIterator(asc_context.windows);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 cx_foreach(AscWindow*, w, windows) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 asc_window_sync(w);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 return true;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 void asc_window_settings_init_defaults(AscWindowSettings* settings) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 settings->depth_size = 24;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 settings->vsync = 1;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 settings->width = 800;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 settings->height = 600;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 settings->fullscreen = 0;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192 settings->gl_major_version = 3;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 settings->gl_minor_version = 3;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 settings->title = "Ascended Window";
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 void asc_window_initialize(AscWindow* window, AscWindowSettings const* settings) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 window->window = SDL_CreateWindow(
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 settings->title,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 SDL_WINDOWPOS_CENTERED,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204 SDL_WINDOWPOS_CENTERED,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 settings->width,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 settings->height,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 flags
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 );
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 if (window->window == NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 asc_error(SDL_GetError());
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 return;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 window->id = SDL_GetWindowID(window->window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 SDL_GetWindowSize(window->window, &window->width, &window->height);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 window->glctx = SDL_GL_CreateContext(window->window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223 if (window->glctx == NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 asc_dprintf("Creating GL context failed for window %u", window->id);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 } else {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 glewExperimental = GL_TRUE;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 GLenum err = glewInit();
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 if (err == GLEW_OK) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 SDL_GL_SetSwapInterval(settings->vsync);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 glEnable(GL_DEPTH_TEST);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 glEnable(GL_BLEND);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 glEnable(GL_DEBUG_OUTPUT);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 glDebugMessageCallback(asc_gl_debug_callback, NULL);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 asc_dprintf("Window %u initialized", window->id);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 cxListAdd(asc_context.windows, window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 return;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 } else {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 asc_error(glewGetErrorString(err));
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 // cleanup on error
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 if (window->glctx != NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 SDL_GL_DeleteContext(window->glctx);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 window->glctx = NULL;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 SDL_DestroyWindow(window->window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 window->window = NULL;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 window->id = 0;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 void asc_window_destroy_impl(AscWindow* window) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 // destory the GL context and the window
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 if (window->glctx != NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 SDL_GL_DeleteContext(window->glctx);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 if (window->window != NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 SDL_DestroyWindow(window->window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262 // clean the data
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264 memset(window, 0, sizeof(AscWindow));
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 void asc_window_destroy(AscWindow* window) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 // find the window in the context and remove it
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 CxMutIterator iter = cxListMutIterator(asc_context.windows);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 cx_foreach(AscWindow*, w, iter) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 if (w == window) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 asc_dprintf("Window %u removed from context.", window->id);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273 cxIteratorFlagRemoval(iter);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 asc_window_destroy_impl(window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
277 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 void asc_window_sync(AscWindow const* window) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 SDL_GL_MakeCurrent(window->window, window->glctx);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 SDL_GL_SwapWindow(window->window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 glViewport(0, 0, window->width, window->height);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285 }

mercurial