Fri, 29 Mar 2024 00:03:25 +0100
make the timer have nanoseconds precision
0 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * Copyright 2023 Mike Becker. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
25 | * POSSIBILITY OF SUCH DAMAGE. | |
26 | */ | |
27 | ||
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 | 29 | #include "ascension/context.h" |
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 | 32 | |
6
302971e8599b
move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
33 | #include <GL/glew.h> |
302971e8599b
move window related stuff to its own unit
Mike Becker <universe@uap-core.de>
parents:
3
diff
changeset
|
34 | |
0 | 35 | void asc_window_settings_init_defaults(AscWindowSettings* settings) { |
3 | 36 | settings->dimensions.width = 800; |
37 | settings->dimensions.height = 600; | |
0 | 38 | settings->fullscreen = 0; |
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
|
39 | settings->glsettings.depth_size = 24; |
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
|
40 | settings->glsettings.vsync = 1; |
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
|
41 | settings->glsettings.gl_major_version = 4; |
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
|
42 | settings->glsettings.gl_minor_version = 0; |
0 | 43 | settings->title = "Ascended Window"; |
44 | } | |
45 | ||
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
46 | 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
|
47 | asc_scene_init(&window->ui); |
37
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
48 | asc_camera_ortho(&window->ui.cameras[0], (asc_recti){ |
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
49 | 0, 0, window->dimensions |
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
50 | }); |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
51 | } |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
52 | |
7 | 53 | AscWindow *asc_window_initialize(unsigned int index, AscWindowSettings const *settings) { |
54 | if (index >= ASC_MAX_WINDOWS) { | |
55 | asc_error("Maximum number of windows exceeded."); | |
56 | return NULL; | |
57 | } | |
58 | AscWindow *window = &asc_context.windows[index]; | |
59 | if (window->id > 0) { | |
60 | asc_error("Cannot create window - slot already occupied."); | |
61 | asc_dprintf("Tried to create window with index %u", index); | |
62 | return NULL; | |
63 | } | |
64 | ||
0 | 65 | Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; |
66 | flags |= settings->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_RESIZABLE; | |
67 | ||
68 | window->window = SDL_CreateWindow( | |
69 | settings->title, | |
70 | SDL_WINDOWPOS_CENTERED, | |
71 | SDL_WINDOWPOS_CENTERED, | |
3 | 72 | settings->dimensions.width, |
73 | settings->dimensions.height, | |
0 | 74 | flags |
75 | ); | |
76 | if (window->window == NULL) { | |
77 | asc_error(SDL_GetError()); | |
7 | 78 | return NULL; |
0 | 79 | } |
80 | ||
81 | window->id = SDL_GetWindowID(window->window); | |
3 | 82 | SDL_GetWindowSize(window->window, |
83 | &window->dimensions.width, | |
84 | &window->dimensions.height | |
85 | ); | |
37
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
86 | window->resized = true; // count initial sizing as resize |
0 | 87 | |
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
|
88 | if (asc_gl_context_initialize(&window->glctx, window->window, &settings->glsettings)) { |
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
|
89 | asc_dprintf("Window %u initialized", window->id); |
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
|
90 | asc_context.active_window = window; |
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
|
91 | asc_window_init_scenes(window); |
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
|
92 | return window; |
0 | 93 | } else { |
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
|
94 | asc_dprintf("Creating GL context failed for window %u", window->id); |
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
|
95 | // cleanup on error |
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
|
96 | SDL_DestroyWindow(window->window); |
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
|
97 | window->window = NULL; |
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
|
98 | window->id = 0; |
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
|
99 | return NULL; |
0 | 100 | } |
101 | } | |
102 | ||
7 | 103 | void asc_window_destroy(AscWindow* window) { |
104 | // safeguard | |
105 | if (window->id == 0) return; | |
106 | ||
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
107 | // this window cannot be active anymore |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
108 | if (asc_context.active_window == window) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
109 | asc_context.active_window = NULL; |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
110 | } |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
111 | |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
112 | // destroy all scenes |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
113 | asc_scene_destroy(&window->ui); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
114 | |
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
|
115 | // 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
|
116 | 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
|
117 | |
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
|
118 | // destroy window |
0 | 119 | if (window->window != NULL) { |
120 | SDL_DestroyWindow(window->window); | |
121 | } | |
122 | ||
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
123 | // 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
|
124 | if (asc_context.active_window != 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
|
125 | asc_gl_context_activate(&asc_context.active_window->glctx); |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
126 | } |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
127 | |
0 | 128 | // clean the data |
129 | asc_dprintf("Window %u and its OpenGL context destroyed.", window->id); | |
130 | memset(window, 0, sizeof(AscWindow)); | |
131 | } | |
132 | ||
37
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
133 | void asc_window_sync(AscWindow* window) { |
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
|
134 | AscWindow *active_window = asc_context.active_window; |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
135 | if (window != active_window) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
136 | asc_window_activate(window); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
137 | } |
29
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
138 | |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
139 | // Draw the UI |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
140 | asc_scene_draw(&window->ui); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
141 | |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
142 | // Swap Buffers |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
143 | SDL_GL_SwapWindow(window->window); |
1d001eb694dc
bring first scene graph to live
Mike Becker <universe@uap-core.de>
parents:
16
diff
changeset
|
144 | |
37
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
145 | // Clear Flags |
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
146 | window->resized = false; |
8a8cc6725b48
add camera and render groups
Mike Becker <universe@uap-core.de>
parents:
34
diff
changeset
|
147 | |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
148 | if (window != active_window) { |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
149 | asc_window_activate(active_window); |
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
150 | } |
0 | 151 | } |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
152 | |
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
|
153 | void asc_window_activate(AscWindow *window) { |
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
|
154 | asc_gl_context_activate(&window->glctx); |
34 | 155 | asc_context.active_window = (AscWindow *)window; |
16
c5dde81b6fb2
add text rendering and demo FPS counter
Mike Becker <universe@uap-core.de>
parents:
13
diff
changeset
|
156 | } |