src/window.c

Wed, 06 Mar 2024 23:08:03 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 06 Mar 2024 23:08:03 +0100
changeset 33
e7ddb52facd3
parent 29
1d001eb694dc
child 34
45d29d7221cc
permissions
-rw-r--r--

add behavior nodes + restructure test program

Also, the test program will now officially be a game of snake.

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"
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
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
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 {
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
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 void asc_window_settings_init_defaults(AscWindowSettings* settings) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 settings->depth_size = 24;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55 settings->vsync = 1;
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
56 settings->dimensions.width = 800;
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
57 settings->dimensions.height = 600;
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
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
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 settings->title = "Ascended Window";
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
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
64 static void asc_window_init_scenes(AscWindow *window) {
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
65 asc_scene_init(&window->ui);
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
66 }
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
67
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
68 AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) {
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
69 if (index >= ASC_MAX_WINDOWS) {
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
70 asc_error("Maximum number of windows exceeded.");
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
71 return NULL;
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
72 }
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
73 AscWindow *window = &asc_context.windows[index];
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
74 if (window->id > 0) {
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
75 asc_error("Cannot create window - slot already occupied.");
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
76 asc_dprintf("Tried to create window with index %u", index);
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
77 return NULL;
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
78 }
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
79
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 window->window = SDL_CreateWindow(
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 settings->title,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 SDL_WINDOWPOS_CENTERED,
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86 SDL_WINDOWPOS_CENTERED,
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
87 settings->dimensions.width,
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
88 settings->dimensions.height,
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 flags
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 if (window->window == NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 asc_error(SDL_GetError());
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
93 return NULL;
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 }
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 window->id = SDL_GetWindowID(window->window);
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
97 SDL_GetWindowSize(window->window,
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
98 &window->dimensions.width,
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
99 &window->dimensions.height
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
100 );
12
d89e0ebc76d2 add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
101 asc_mat4f_ortho(
d89e0ebc76d2 add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
102 window->projection,
d89e0ebc76d2 add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
103 0,
d89e0ebc76d2 add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
104 (float) window->dimensions.width,
d89e0ebc76d2 add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
105 (float) window->dimensions.height,
d89e0ebc76d2 add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
106 0
d89e0ebc76d2 add projection matrix to AscWindow
Mike Becker <universe@uap-core.de>
parents: 9
diff changeset
107 );
0
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 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
110 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, settings->gl_major_version);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, settings->gl_minor_version);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, settings->depth_size);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 window->glctx = SDL_GL_CreateContext(window->window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 if (window->glctx == NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 asc_dprintf("Creating GL context failed for window %u", window->id);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 } else {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 glewExperimental = GL_TRUE;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 GLenum err = glewInit();
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 if (err == GLEW_OK) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 SDL_GL_SetSwapInterval(settings->vsync);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 glEnable(GL_DEPTH_TEST);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 glEnable(GL_BLEND);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 glEnable(GL_DEBUG_OUTPUT);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 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
127
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 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
129 if (asc_primitives_init(&window->primitives)) {
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
130 asc_window_init_scenes(window);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
131 asc_context.active_window = window;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
132 return window;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
133 } else {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
134 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
135 }
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 } else {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137 asc_error(glewGetErrorString(err));
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 }
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 // cleanup on error
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 if (window->glctx != NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 SDL_GL_DeleteContext(window->glctx);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 window->glctx = NULL;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 SDL_DestroyWindow(window->window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 window->window = NULL;
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 window->id = 0;
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
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
151 void asc_window_destroy(AscWindow* window) {
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
152 // safeguard
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
153 if (window->id == 0) return;
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
154
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
155 // this window cannot be active anymore
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
156 if (asc_context.active_window == window) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
157 asc_context.active_window = NULL;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
158 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
159
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
160 // destroy all scenes
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
161 asc_scene_destroy(&window->ui);
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
162
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
163 // 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
164 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
165 asc_primitives_destroy(&window->primitives);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
166
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
167 // destroy the GL context and the window
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 if (window->glctx != NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 SDL_GL_DeleteContext(window->glctx);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 if (window->window != NULL) {
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 SDL_DestroyWindow(window->window);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
175 // 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
176 if (asc_context.active_window != NULL) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
177 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
178 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
179 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
180
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 // clean the data
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183 memset(window, 0, sizeof(AscWindow));
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_sync(AscWindow const* window) {
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
187 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
188 if (window != active_window) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
189 asc_window_activate(window);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
190 }
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
191
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
192 // Clear viewport for new frame
3
1efd6da2ad53 add datatypes.h
Mike Becker <universe@uap-core.de>
parents: 2
diff changeset
193 glViewport(0, 0, window->dimensions.width, window->dimensions.height);
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
196
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
197 // Draw the UI
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
198 asc_scene_draw(&window->ui);
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
199
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
200 // Swap Buffers
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
201 SDL_GL_SwapWindow(window->window);
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
202
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
203 if (window != active_window) {
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
204 asc_window_activate(active_window);
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
205 }
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 }
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
207
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
208 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
209 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
210 asc_context.active_window = window;
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
211 }

mercurial