src/window.c

Tue, 07 Nov 2023 20:24:08 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 07 Nov 2023 20:24:08 +0100
changeset 8
756b49205a29
parent 7
9dd76cbd6c90
child 9
6ad1a4213954
permissions
-rw-r--r--

add script to update make rules

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

mercurial