src/window.c

Sun, 25 Jan 2026 19:45:24 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 25 Jan 2026 19:45:24 +0100
changeset 297
02ce7c6251a9
parent 295
c72df1f06671
permissions
-rw-r--r--

implement that your own trace kills you

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
106
895f92cff6b8 bring back error.h to reduce compile dependencies
Mike Becker <universe@uap-core.de>
parents: 105
diff changeset
28 #include "ascension/error.h"
6
302971e8599b move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
29 #include "ascension/window.h"
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
30 #include "ascension/context.h"
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31
253
6ab35fcb8676 upgrade to SDL 3
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
32 #include <GL/glew.h>
6ab35fcb8676 upgrade to SDL 3
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
33
231
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
34 #include <assert.h>
253
6ab35fcb8676 upgrade to SDL 3
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
35 #include <stdio.h>
6
302971e8599b move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents: 3
diff changeset
36
257
67d7b79997df remove AscWindowSettings struct
Mike Becker <universe@uap-core.de>
parents: 253
diff changeset
37 void asc_window_initialize(unsigned int index, AscGLContextSettings settings) {
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
38 if (index >= ASC_MAX_WINDOWS) {
92
78ce93fb46e5 apply new logging macros
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
39 asc_error("Maximum number of windows exceeded (%u/%u).", index, ASC_MAX_WINDOWS);
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
40 return;
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
41 }
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
42 AscWindow *window = &asc_context.windows[index];
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
43 if (window->id > 0) {
92
78ce93fb46e5 apply new logging macros
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
44 asc_error("Cannot create window - slot %u already occupied.", index);
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
45 return;
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
46 }
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
47
253
6ab35fcb8676 upgrade to SDL 3
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
48 SDL_WindowFlags flags = SDL_WINDOW_OPENGL;
257
67d7b79997df remove AscWindowSettings struct
Mike Becker <universe@uap-core.de>
parents: 253
diff changeset
49 flags |= settings.fullscreen ? SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE;
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
50 window->sdl = SDL_CreateWindow("Ascension",800, 600, flags);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
51 if (window->sdl == NULL) {
92
78ce93fb46e5 apply new logging macros
Mike Becker <universe@uap-core.de>
parents: 75
diff changeset
52 asc_error("Creating Window failed: %s", SDL_GetError());
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
53 return;
0
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
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
56 window->id = SDL_GetWindowID(window->sdl);
105
2b7f92ff2c15 improve datatypes
Mike Becker <universe@uap-core.de>
parents: 101
diff changeset
57 {
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
58 int x,y, w, h;
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
59 SDL_GetWindowPosition(window->sdl, &x, &y);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
60 SDL_GetWindowSize(window->sdl, &w, &h);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
61 window->rect = ASC_RECT(x, y, w, h);
105
2b7f92ff2c15 improve datatypes
Mike Becker <universe@uap-core.de>
parents: 101
diff changeset
62 }
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
63 window->resized = true; // count initial sizing as resize
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64
154
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
65 // default UI scale
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
66 window->ui_scale = 1.0f;
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
67
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
68 if (asc_gl_context_initialize(&window->glctx, window->sdl, &settings)) {
220
6b266e907f89 resolve several minor TODOs
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
69 char ui_scene_name[16];
6b266e907f89 resolve several minor TODOs
Mike Becker <universe@uap-core.de>
parents: 213
diff changeset
70 snprintf(ui_scene_name, sizeof(ui_scene_name), "Window %u UI", index);
287
359eaf2a8bd2 make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents: 262
diff changeset
71 asc_camera_init(&window->ui_camera, .type = ASC_CAMERA_ORTHO,
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
72 .ortho.rect = ASC_RECTV(ASC_VEC2I_0, window->rect.size),
288
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
73 .viewport_update_func = asc_camera_ortho_update_size);
287
359eaf2a8bd2 make camera independent of the scene
Mike Becker <universe@uap-core.de>
parents: 262
diff changeset
74 asc_scene_init(&window->ui, ui_scene_name, &window->ui_camera);
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
75 asc_dprintf("Window %u initialized at index %u", window->id, index);
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
76 asc_context.active_window = index;
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 } else {
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
78 asc_error("Creating GL context failed for window %u at index %u", window->id, index);
44
b3da4096c607 create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
79 // cleanup on error
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
80 SDL_DestroyWindow(window->sdl);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
81 window->sdl = NULL;
44
b3da4096c607 create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
82 window->id = 0;
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
86 void asc_window_destroy(unsigned int index) {
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
87 // safeguard
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
88 if (asc_context.windows[index].id == 0) return;
7
9dd76cbd6c90 improve code structure
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
89
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
90 // this window cannot be active anymore
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
91 if (asc_context.active_window == index) {
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
92 asc_context.active_window = ASC_MAX_WINDOWS;
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
93 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
94
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
95 // pointer to the window
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
96 AscWindow *window = &asc_context.windows[index];
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
97
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
98 // for releasing OpenGL resources, we need to make the context current
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
99 asc_gl_context_activate(&window->glctx);
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
100
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
101 // destroy all scenes
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
102 for (unsigned int i = 0; i < ASC_MAX_SCENES; i++) {
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
103 asc_scene_destroy(&window->scenes[i]);
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
104 }
95
622887f7e954 in preparation of more scenes, bring back AscScene struct
Mike Becker <universe@uap-core.de>
parents: 92
diff changeset
105 asc_scene_destroy(&window->ui);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
106
44
b3da4096c607 create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
107 // release context related data
b3da4096c607 create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
108 asc_gl_context_destroy(&window->glctx);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
109
44
b3da4096c607 create own compilation unit for GL context - fixes shader not being created per context
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
110 // destroy window
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
111 if (window->sdl != NULL) {
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
112 SDL_DestroyWindow(window->sdl);
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 }
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
115 // if another window was active, make the other context current again
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
116 if (asc_context.active_window < ASC_MAX_WINDOWS) {
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
117 asc_gl_context_activate(&asc_active_window->glctx);
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
118 }
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
119
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 // clean the data
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 asc_dprintf("Window %u and its OpenGL context destroyed.", window->id);
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 memset(window, 0, sizeof(AscWindow));
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
288
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
125 static void asc_window_update_viewport(AscCamera *camera, asc_vec2u window_size) {
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
126 if (camera->viewport_update_func == NULL) {
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
127 // this assumes the viewport was initialized with zeros!
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
128 camera->viewport.size = window_size;
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
129 } else {
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
130 camera->viewport_update_func(camera, window_size);
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
131 }
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
132 }
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
133
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
134 static void asc_window_update_camera(AscCamera *camera) {
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
135 if (camera->update_func != NULL) {
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
136 camera->update_func(camera);
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
137 }
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
138 }
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
139
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
140 void asc_window_sync(unsigned int index) {
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
141 if (index > ASC_MAX_WINDOWS) {
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
142 asc_error("Index for syncing window out of bounds (%u/%u).", index, ASC_MAX_WINDOWS);
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
143 return;
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
144 }
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
145 AscWindow *window = &asc_context.windows[index];
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
146 // necessary safeguard
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
147 if (window->id == 0) return;
65
9c44c55d327a consistently refer to windows by ID - fixes #381
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
148
295
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
149 // activate the window that shall be synced temporarily
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
150 unsigned int active_index = asc_window_activate(index);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
151
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
152 // Clear the color buffer for the window frame
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
153 glViewport(0, 0,
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
154 (GLsizei) window->rect.size.width,
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
155 (GLsizei) window->rect.size.height);
47
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
156 glClear(GL_COLOR_BUFFER_BIT);
44457f6cb0a2 remove unnecessary scene container
Mike Becker <universe@uap-core.de>
parents: 44
diff changeset
157
262
b47de42f4598 update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents: 257
diff changeset
158 // Update the viewports when the window resized
b47de42f4598 update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents: 257
diff changeset
159 if (window->resized) {
b47de42f4598 update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents: 257
diff changeset
160 for (unsigned int i = 0; i < ASC_MAX_SCENES; i++) {
b47de42f4598 update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents: 257
diff changeset
161 if (asc_scene_active(&window->scenes[i])) {
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
162 asc_window_update_viewport(window->scenes[i].camera, window->rect.size);
262
b47de42f4598 update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents: 257
diff changeset
163 }
b47de42f4598 update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents: 257
diff changeset
164 }
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
165 asc_window_update_viewport(window->ui.camera, window->rect.size);
262
b47de42f4598 update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents: 257
diff changeset
166 }
b47de42f4598 update viewports of cameras before executing behaviors, so that the new viewport information is available in the behavior functions
Mike Becker <universe@uap-core.de>
parents: 257
diff changeset
167
288
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
168 // Execute camera updates
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
169 for (unsigned int i = 0; i < ASC_MAX_SCENES; i++) {
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
170 if (asc_scene_active(&window->scenes[i])) {
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
171 asc_window_update_camera(window->scenes[i].camera);
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
172 }
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
173 }
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
174 asc_window_update_camera(window->ui.camera);
8796f03aac26 make the camera follow the player + add limited vision
Mike Becker <universe@uap-core.de>
parents: 287
diff changeset
175
112
3e956c96dd6c extract execution of behaviors from draw function
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
176 // Execute all behaviors
3e956c96dd6c extract execution of behaviors from draw function
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
177 // TODO: this can eventually be parallelized
3e956c96dd6c extract execution of behaviors from draw function
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
178 for (unsigned int i = 0; i < ASC_MAX_SCENES; i++) {
3e956c96dd6c extract execution of behaviors from draw function
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
179 asc_scene_execute_behaviors(&window->scenes[i]);
3e956c96dd6c extract execution of behaviors from draw function
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
180 }
3e956c96dd6c extract execution of behaviors from draw function
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
181 asc_scene_execute_behaviors(&window->ui);
3e956c96dd6c extract execution of behaviors from draw function
Mike Becker <universe@uap-core.de>
parents: 106
diff changeset
182
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
183 // Draw all scenes
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
184 for (unsigned int i = 0; i < ASC_MAX_SCENES; i++) {
99
ac143db979dc add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents: 96
diff changeset
185 asc_scene_draw(&window->scenes[i]);
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
186 }
99
ac143db979dc add aspect-ration independent rendering
Mike Becker <universe@uap-core.de>
parents: 96
diff changeset
187 asc_scene_draw(&window->ui);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
188
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
189 // Swap Buffers
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
190 SDL_GL_SwapWindow(window->sdl);
29
1d001eb694dc bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
191
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
192 // Clear Flags
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
193 window->resized = false;
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
194 window->moved = false;
37
8a8cc6725b48 add camera and render groups
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
195
295
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
196 asc_window_activate(active_index);
0
593b60458157 initial commit
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 }
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
198
295
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
199 unsigned int asc_window_activate(unsigned int index) {
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
200 unsigned int active_index = asc_context.active_window;
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
201 if (active_index != index) {
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
202 asc_context.active_window = index;
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
203 asc_gl_context_activate(&asc_active_window->glctx);
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
204 return active_index;
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
205 }
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
206 return index;
16
c5dde81b6fb2 add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
207 }
68
823c03733e42 add mouse and window focus - resolves #382
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
208
823c03733e42 add mouse and window focus - resolves #382
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
209 unsigned int asc_window_index(Uint32 id) {
823c03733e42 add mouse and window focus - resolves #382
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
210 unsigned int i = 0;
823c03733e42 add mouse and window focus - resolves #382
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
211 for (; i < ASC_MAX_WINDOWS ; i++) {
823c03733e42 add mouse and window focus - resolves #382
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
212 if (asc_context.windows[i].id == id) break;
823c03733e42 add mouse and window focus - resolves #382
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
213 }
823c03733e42 add mouse and window focus - resolves #382
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
214 return i;
823c03733e42 add mouse and window focus - resolves #382
Mike Becker <universe@uap-core.de>
parents: 65
diff changeset
215 }
95
622887f7e954 in preparation of more scenes, bring back AscScene struct
Mike Becker <universe@uap-core.de>
parents: 92
diff changeset
216
96
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
217 AscScene *asc_window_scene(unsigned int index) {
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
218 return &asc_active_window->scenes[index];
9e25f080a33e add scenes, but they don't draw yet
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
219 }
154
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
220
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
221 asc_vec2u asc_window_display_resolution(void) {
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
222 const AscWindow *window = asc_active_window;
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
223 SDL_DisplayID display = SDL_GetDisplayForWindow(window->sdl);
253
6ab35fcb8676 upgrade to SDL 3
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
224 const SDL_DisplayMode *dm = SDL_GetDesktopDisplayMode(display);
6ab35fcb8676 upgrade to SDL 3
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
225 if (dm == NULL) {
154
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
226 asc_error("Failed to get display mode for window %u on display %d: %s",
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
227 window->id, display, SDL_GetError());
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
228 }
253
6ab35fcb8676 upgrade to SDL 3
Mike Becker <universe@uap-core.de>
parents: 237
diff changeset
229 return ASC_VEC2U(dm->w, dm->h);
154
4dff9cc488fe add auto-scaling of UI depending on screen resolution - resolves #682
Mike Becker <universe@uap-core.de>
parents: 149
diff changeset
230 }
231
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
231
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
232 float asc_window_ui_scale(unsigned int index) {
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
233 assert(asc_context.windows[index].sdl != NULL);
231
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
234 return asc_context.windows[index].ui_scale;
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
235 }
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
236
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
237 // TODO: error handling for all SDL_SetStuff() functions
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
238
231
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
239 void asc_window_set_title(unsigned index, const char *title) {
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
240 assert(asc_context.windows[index].sdl != NULL);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
241 SDL_SetWindowTitle(asc_context.windows[index].sdl, title);
231
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
242 }
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
243
237
2ea35c5f4760 add functions to change the window position
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
244 void asc_window_set_position(unsigned index, asc_vec2i pos) {
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
245 assert(asc_context.windows[index].sdl != NULL);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
246 SDL_SetWindowPosition(asc_context.windows[index].sdl, pos.x, pos.y);
237
2ea35c5f4760 add functions to change the window position
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
247 }
2ea35c5f4760 add functions to change the window position
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
248
2ea35c5f4760 add functions to change the window position
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
249 void asc_window_center(unsigned index) {
2ea35c5f4760 add functions to change the window position
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
250 // TODO: add support for multiple displays
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
251 const AscWindow *window = asc_context.windows + index;
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
252 assert(window->sdl != NULL);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
253 SDL_SetWindowPosition(window->sdl, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
254 }
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
255
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
256 void asc_window_hcenter(unsigned index) {
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
257 // TODO: add support for multiple displays
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
258 const AscWindow *window = asc_context.windows + index;
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
259 assert(window->sdl != NULL);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
260 SDL_SetWindowPosition(window->sdl, SDL_WINDOWPOS_CENTERED, window->rect.pos.y);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
261 }
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
262
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
263 void asc_window_vcenter(unsigned index) {
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
264 // TODO: add support for multiple displays
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
265 const AscWindow *window = asc_context.windows + index;
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
266 assert(window->sdl != NULL);
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
267 SDL_SetWindowPosition(window->sdl, window->rect.pos.x, SDL_WINDOWPOS_CENTERED);
237
2ea35c5f4760 add functions to change the window position
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
268 }
2ea35c5f4760 add functions to change the window position
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
269
231
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
270 void asc_window_set_size(unsigned index, asc_vec2u size) {
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
271 const AscWindow *window = asc_context.windows + index;
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
272 assert(window->sdl != NULL);
237
2ea35c5f4760 add functions to change the window position
Mike Becker <universe@uap-core.de>
parents: 231
diff changeset
273 // TODO: check what needs to be changed when SDL_WINDOW_ALLOW_HIGHDPI is supported by the engine
294
4df350dac84f add functions for centering a window on only one axis
Mike Becker <universe@uap-core.de>
parents: 288
diff changeset
274 SDL_SetWindowSize(window->sdl, (int)size.width, (int)size.height);
231
0da563c4e39c make initial window size depend on UI scaling factor
Mike Becker <universe@uap-core.de>
parents: 220
diff changeset
275 }
295
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
276
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
277 void asc_window_fullscreen(unsigned index, bool fullscreen) {
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
278 const AscWindow *window = asc_context.windows + index;
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
279 assert(window->sdl != NULL);
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
280 SDL_SetWindowFullscreen(window->sdl, fullscreen);
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
281 }
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
282
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
283 void asc_window_vsync(unsigned index, bool vsync) {
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
284 unsigned int active_index = asc_window_activate(index);
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
285 SDL_GL_SetSwapInterval(vsync ? 1 : 0);
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
286 asc_window_activate(active_index);
c72df1f06671 add functions to change fullscreen and vsync during runtime
Mike Becker <universe@uap-core.de>
parents: 294
diff changeset
287 }

mercurial