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