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