25771ff8d142fc9ff7b180a0048172691feb78d3
[vg.git] / src / vg / vg.h
1 /* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */
2
3 static void vg_exiterr( const char *err );
4 static void vg_exit(void);
5
6 #ifndef VG_HEADER_H
7 #define VG_HEADER_H
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <dirent.h>
12 #include <string.h>
13 #include <stdarg.h>
14 #include <ctype.h>
15 #include <math.h>
16
17 #if defined(VG_SERVER) || defined(VG_TOOLS)
18 #define VG_NON_CLIENT
19 #endif
20
21 #ifndef VG_SERVER
22 #include "../../dep/glad/glad.h"
23 #include "../../dep/glfw/glfw3.h"
24 #endif
25
26 #define STB_DS_IMPLEMENTATION
27 #include "stb/stb_ds.h"
28
29 #define QOI_IMPLEMENTATION
30 #include "phoboslab/qoi.h"
31
32 #include "vg_stdint.h"
33 #include "vg_platform.h"
34
35 void vg_register_exit( void( *funcptr )(void), const char *name );
36
37 #include "vg_m.h"
38 #include "vg_io.h"
39
40 #ifdef VG_STEAM
41 //#include "vg_steamworks.h"
42 #include "vg_steam.h"
43 #endif
44
45 #ifndef VG_NON_CLIENT
46 #include "vg_gldiag.h"
47 #endif
48
49 #ifndef VG_NON_CLIENT
50
51 /* Engine globals */
52 GLFWwindow* vg_window;
53
54 #ifdef VG_3D
55 m4x4f vg_pv;
56 #else
57 m3x3f vg_pv;
58 #endif
59
60 #ifdef VG_CAPTURE_MODE
61 int vg_window_x = 1920;
62 int vg_window_y = 1080;
63 #else
64 int vg_window_x = 1366;
65 int vg_window_y = 768;
66 #endif
67
68 v2f vg_mouse;
69 v2f vg_mouse_wheel;
70 v3f vg_mouse_ws;
71
72 double vg_time,
73 vg_time_last,
74 vg_time_delta;
75
76 #include "vg_audio.h"
77 #include "vg_shader.h"
78 #include "vg_tex.h"
79 #include "vg_input.h"
80 #include "vg_ui.h"
81 #include "vg_console.h"
82 #include "vg_lines.h"
83 #include "vg_debug.h"
84
85 #ifndef VG_RELEASE
86 void vg_checkgl( const char *src_info )
87 {
88 GLenum err;
89 while( (err = glGetError()) != GL_NO_ERROR )
90 {
91 vg_error( "(%s) OpenGL Error: #%d\n", src_info, err );
92 }
93 }
94
95 #define VG_STRINGIT( X ) #X
96 #define VG_CHECK_GL() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
97 #else
98 #define VG_CHECK_GL()
99 #endif
100
101
102 #define VG_GAMELOOP
103
104 void( *vg_on_exit[16] )(void);
105 u32 vg_exit_count = 0;
106
107 void vg_register_exit( void( *funcptr )(void), const char *name )
108 {
109 vg_info( "exit registered: (%u)'%s'\n", vg_exit_count, name );
110 vg_on_exit[ vg_exit_count ++ ] = funcptr;
111 }
112
113 static void vg_exit(void)
114 {
115 for( int i = vg_exit_count-1; i >= 0; i -- )
116 {
117 vg_info( "engine_exit[%d]()\n", i );
118 vg_on_exit[i]();
119 }
120
121 vg_info( "done\n" );
122 exit(0);
123 }
124
125 static void vg_exiterr( const char *err )
126 {
127 vg_error( "Engine Fatal: %s\n", err );
128 vg_exit();
129 }
130
131 void vg_mouse_callback( GLFWwindow* ptrW, double xpos, double ypos )
132 {
133 vg_mouse[0] = xpos;
134 vg_mouse[1] = ypos;
135 }
136
137 void vg_scroll_callback( GLFWwindow* ptrW, double xoffset, double yoffset )
138 {
139 vg_mouse_wheel[0] += xoffset;
140 vg_mouse_wheel[1] += yoffset;
141 }
142
143
144 static void vg_register(void) VG_GAMELOOP;
145 static void vg_start(void) VG_GAMELOOP;
146 static void vg_update(void) VG_GAMELOOP;
147 static void vg_framebuffer_resize(int w, int h) VG_GAMELOOP;
148 static void vg_render(void) VG_GAMELOOP;
149 static void vg_ui(void) VG_GAMELOOP;
150 static void vg_free(void) VG_GAMELOOP;
151
152 void vg_framebuffer_resize_callback( GLFWwindow *ptrW, int w, int h )
153 {
154 vg_window_x = w;
155 vg_window_y = h;
156
157 #ifdef VG_FRAMEBUFFER_RESIZE
158 vg_framebuffer_resize(w,h);
159 #endif
160 }
161
162 static void vg_init( int argc, char *argv[], const char *window_name )
163 {
164 #ifdef VG_STEAM
165 if( !sw_init() )
166 return;
167 #endif
168
169 glfwInit();
170 glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
171 glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
172 glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
173 glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
174
175 #ifdef VG_CAPTURE_MODE
176 glfwWindowHint( GLFW_RESIZABLE, GLFW_FALSE );
177 #else
178 glfwWindowHint( GLFW_RESIZABLE, GLFW_TRUE );
179 #endif
180 glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
181
182 #if 0
183 glfwWindowHint(GLFW_SAMPLES,4);
184 #endif
185
186 GLFWmonitor *monitor_primary = glfwGetPrimaryMonitor();
187
188 const GLFWvidmode *mode = glfwGetVideoMode( monitor_primary );
189 glfwWindowHint( GLFW_RED_BITS, mode->redBits );
190 glfwWindowHint( GLFW_GREEN_BITS, mode->greenBits );
191 glfwWindowHint( GLFW_BLUE_BITS, mode->blueBits );
192
193 int refresh_rate = mode->refreshRate;
194
195 if( refresh_rate < 28 || refresh_rate >= 144 )
196 refresh_rate = 60;
197
198 glfwWindowHint( GLFW_REFRESH_RATE, refresh_rate );
199
200 if( !(vg_window = glfwCreateWindow( vg_window_x, vg_window_y,
201 window_name, NULL, NULL)) )
202 vg_exiterr( "GLFW Failed to initialize" );
203 else
204 vg_register_exit( &glfwTerminate, "glfwTerminate" );
205
206 glfwMakeContextCurrent( vg_window );
207 glfwSwapInterval( 1 );
208
209 glfwSetWindowSizeLimits( vg_window, 800, 600, GLFW_DONT_CARE,GLFW_DONT_CARE);
210 glfwSetFramebufferSizeCallback( vg_window, vg_framebuffer_resize_callback );
211
212 glfwSetCursorPosCallback( vg_window, vg_mouse_callback );
213 glfwSetScrollCallback( vg_window, vg_scroll_callback );
214
215 glfwSetCharCallback( vg_window, console_proc_wchar );
216 glfwSetKeyCallback( vg_window, console_proc_key );
217 #if 0
218 glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
219 #endif
220
221 if( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) )
222 vg_exiterr( "Glad failed to initialize" );
223
224 const unsigned char* glver = glGetString( GL_VERSION );
225 vg_success( "Load setup complete, OpenGL version: %s\n", glver );
226
227 vg_run_gfx_diagnostics();
228
229 vg_gamepad_init();
230 vg_lines_init();
231 vg_register_exit( &vg_lines_free, "vg_lines_free" );
232 ui_default_init();
233 vg_register_exit( &ui_default_free, "UI" );
234
235 vg_register();
236 vg_register_exit( &vg_free, "vg_free" );
237
238
239 vg_shaders_recompile(0,NULL);
240 vg_register_exit( &vg_shaders_free, "vg_shaders_free" );
241 vg_function_push( (struct vg_cmd){
242 .name = "shaders",
243 .function = vg_shaders_recompile
244 });
245
246 if( !vg_audio_init() )
247 vg_exit();
248 vg_register_exit( &vg_audio_free, "vg_audio_free" );
249
250 vg_start();
251
252 vg_console_init();
253 vg_register_exit( &vg_console_free, "Console" );
254
255 /*
256 * Main gameloop
257 */
258 while( !glfwWindowShouldClose( vg_window ) )
259 {
260 v2_copy( (v2f){ 0.0f, 0.0f }, vg_mouse_wheel );
261
262 glfwPollEvents();
263
264 #ifdef VG_STEAM
265 sw_event_loop();
266 #endif
267
268 vg_time_last = vg_time;
269 vg_time = glfwGetTime();
270 vg_time_delta = vg_minf( vg_time - vg_time_last, 0.1f );
271
272 vg_update_inputs();
273 vg_update();
274 vg_render();
275
276 vg_lines_drawall((float*)vg_pv);
277
278 {
279 ui_begin( &ui_global_ctx, vg_window_x, vg_window_y );
280 ui_set_mouse( &ui_global_ctx, vg_mouse[0], vg_mouse[1],
281 vg_get_button_state( "primary" ) );
282
283 audio_debug_ui( vg_pv );
284 vg_ui();
285 vg_console_draw();
286
287 ui_resolve( &ui_global_ctx );
288 ui_draw( &ui_global_ctx, NULL );
289 }
290
291 glfwSwapBuffers( vg_window );
292 VG_CHECK_GL();
293 }
294
295 vg_exit();
296 }
297
298 #ifndef VG_3D
299 void vg_projection_update(void)
300 {
301 /*
302 * Reproject screenspace mouse into world
303 */
304
305 vg_mouse_ws[0] = vg_mouse[0];
306 vg_mouse_ws[1] = vg_mouse[1];
307 vg_mouse_ws[2] = 1.0f;
308
309 vg_mouse_ws[0] = (2.0f * vg_mouse_ws[0]) / ((float)vg_window_x) - 1.0f;
310 vg_mouse_ws[1] = -((2.0f * vg_mouse_ws[1]) / ((float)vg_window_y) - 1.0f);
311
312 m3x3f inverse;
313 m3x3_inv( vg_pv, inverse );
314 m3x3_mulv( inverse, vg_mouse_ws, vg_mouse_ws );
315 }
316 #endif
317
318 #endif
319
320 /*
321 * Graphic cards will check these to force it to use the GPU
322 */
323 u32 NvOptimusEnablement = 0x00000001;
324 int AmdPowerXpressRequestHighPerformance = 1;
325
326 #endif