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