bad char
[vg.git] / vg_engine.h
1 #pragma once
2
3 /* Copyright (C) 2021-2024 Mt.Zero Software - All Rights Reserved */
4 /*
5 .-. VG Event loop
6 | 0 |
7 | | .---------------------------------------------------------.
8 |API| | vg_enter( int argc, char *argv[], const char *window_name |
9 | | '---------------------------------------------------------'
10 | | |
11 | | v
12 |IMP| vg_launch_opt(void) <--.
13 | | | |
14 | | |'---------------'
15 | | | .-.
16 | | |'-----------------------------------| 1 |------.
17 | | | | | |
18 | | | | | v
19 | | | |IMP| vg_preload(void)
20 | | | | | |
21 | | .-----+. | | v
22 | | | | |IMP| vg_load(void)
23 | | | v '___' |
24 |IMP| | vg_framebuffer_resize(void) |
25 | | | | |
26 |IMP| | |.--------------------------------------------'
27 | | | |
28 | | | v
29 |IMP| | vg_pre_update(void)
30 | | | |
31 | | | .-----+.
32 | | | | | called 0x to 8x
33 | | | | v
34 |IMP| | '- vg_fixed_update(void)
35 | | | |
36 | | | .-'
37 | | | |
38 | | | v
39 |IMP| | vg_post_update(void)
40 | | | |
41 | | | v
42 |IMP| | vg_render(void)
43 | | | |
44 | | | v
45 |IMP| | vg_gui(void)
46 | | | |
47 | | | v
48 |IMP| | vg_game_settings_init(void)
49 |IMP| | vg_game_settings_gui( ui_rect panel )
50 | | | | (optional: #define VG_GAME_SETTINGS)
51 | | | |
52 | | '----'
53 '___'
54
55 */
56
57 /* configuration warnings */
58
59 #ifndef VG_TIMESTEP_FIXED
60 #warning VG_TIMESTEP_FIXED not defined; setting to 1/60
61 #define VG_TIMESTEP_FIXED (1.0/60.0)
62 #endif
63
64 #ifndef VG_3D
65 #ifndef VG_2D
66 #warning VG_3D or VG_2D not defined; defining VG_3D
67 #define VG_3D
68 #endif
69 #endif
70
71 #define VG_ENGINE
72 #define SDL_MAIN_HANDLED
73
74 #include "dep/glad/glad.h"
75 #include "dep/sdl/include/SDL.h"
76
77 #include "vg_platform.h"
78 #include "vg_mem.h"
79 #include "vg_m.h"
80 #include "vg_imgui.h"
81
82 #include <setjmp.h>
83
84 /* API */
85 void vg_enter( int argc, char *argv[], const char *window_name );
86
87 /* Thread 1 */
88 void vg_bake_shaders(void);
89
90 extern void vg_preload(void);
91 extern void vg_load(void);
92
93 /* Main thread */
94 extern void vg_launch_opt(void);
95 extern void vg_framebuffer_resize(int w, int h);
96 extern void vg_pre_update(void);
97 extern void vg_fixed_update(void);
98 extern void vg_post_update(void);
99
100 extern void vg_render(void);
101 extern void vg_gui(void);
102
103 void vg_settings_open(void);
104 void vg_settings_close(void);
105
106 enum quality_profile{
107 k_quality_profile_high = 0,
108 k_quality_profile_low = 1,
109 k_quality_profile_min = 2
110 };
111
112 enum vg_thread_purpose
113 {
114 k_thread_purpose_nothing,
115 k_thread_purpose_main,
116 k_thread_purpose_loader
117 };
118
119 struct vg_engine {
120 /* Engine sync */
121 SDL_Window *window;
122 SDL_GLContext gl_context;
123
124 SDL_sem *sem_loader; /* allows only one loader at a time */
125 jmp_buf env_loader_exit;
126
127 SDL_threadID thread_id_main,
128 thread_id_loader;
129 void *thread_data;
130
131 SDL_SpinLock sl_status;
132 enum engine_status{
133 k_engine_status_none,
134 k_engine_status_load_internal,
135 k_engine_status_running,
136 k_engine_status_crashed
137 }
138 engine_status;
139
140 /* Window information */
141 int window_x,
142 window_y,
143 samples,
144 window_should_close;
145 const char *base_path;
146
147 int display_refresh_rate,
148 fps_limit,
149 vsync,
150 screen_mode,
151 display_index;
152
153 int settings_open;
154
155 enum vsync_feature{
156 k_vsync_feature_disabled=0,
157 k_vsync_feature_enabled=1,
158 k_vsync_feature_enabled_adaptive=2,
159 k_vsync_feature_error=3
160 }
161 vsync_feature;
162
163 i32 mouse_pos[2];
164 v2f mouse_delta,
165 mouse_wheel;
166
167 /* Runtime */
168 double time,
169 time_real,
170 time_delta,
171 time_rate,
172
173 time_fixed_accumulator,
174 time_fixed_extrapolate,
175 time_frame_delta;
176
177 f32 time_fixed_delta;
178
179 u64 time_hp, time_hp_last, time_spinning;
180
181 int fixed_iterations;
182
183 enum engine_stage{
184 k_engine_stage_none,
185 k_engine_stage_update,
186 k_engine_stage_update_fixed,
187 k_engine_stage_rendering,
188 k_engine_stage_ui
189 }
190 engine_stage;
191
192 /* graphics */
193 #ifdef VG_3D
194 m4x4f pv;
195 #else
196 m3x3f pv;
197 #endif
198
199 i32 quality_profile;
200
201 float loader_ring;
202 GLuint tex_missing;
203
204 vg_rand rand;
205 }
206 extern vg;
207
208 struct vg_setting_enum
209 {
210 i32 new_value, *actual_value;
211
212 struct ui_enum_opt *options;
213 u32 option_count;
214 const char *label;
215 };
216
217 struct vg_setting_ranged_i32
218 {
219 i32 new_value, *actual_value, min, max;
220 char buf[10];
221 const char *label;
222 };
223
224 void ui_settings_ranged_i32_init( struct vg_setting_ranged_i32 *prop );
225 void ui_settings_enum_init( struct vg_setting_enum *prop );
226 bool vg_settings_enum( struct vg_setting_enum *prop, ui_rect rect );
227 bool vg_settings_enum_diff( struct vg_setting_enum *prop );
228 void vg_settings_ui_header( ui_rect inout_panel, const char *name );
229 bool vg_settings_apply_button( ui_rect inout_panel, bool validated );
230
231 enum engine_status _vg_engine_status(void);
232 enum vg_thread_purpose vg_thread_purpose(void);
233
234 void vg_checkgl( const char *src_info );
235 #define VG_STRINGIT( X ) #X
236 #define VG_CHECK_GL_ERR() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
237
238 /* the few includes we always want. */
239 #include "vg_log.h"
240 #include "vg_shader.h"
241 #include "vg_console.h"