1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 // TODO: replace as much of this as possible
11 #include "glad/glad.h"
12 #include "glfw/glfw3.h"
14 #define STB_DS_IMPLEMENTATION
15 #include "stb/stb_ds.h"
17 #define QOI_IMPLEMENTATION
18 #include "phoboslab/qoi.h"
20 #include "vg/vg_platform.h"
22 void vg_register_exit( void( *funcptr
)(void), const char *name
);
23 void vg_exiterr( const char *strErr
);
29 #include "vg/vg_gldiag.h"
34 GLFWwindow
* vg_window
;
39 #ifdef VG_CAPTURE_MODE
40 int vg_window_x
= 1920;
41 int vg_window_y
= 1080;
43 int vg_window_x
= 1366;
44 int vg_window_y
= 768;
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"
62 #include "vg/vg_console.h"
63 #include "vg/vg_debug.h"
64 #include "vg/vg_steamworks.h"
67 // ===========================================================================================================
70 void vg_checkgl( const char *src_info
)
73 while( (err
= glGetError()) != GL_NO_ERROR
)
75 vg_error( "(%s) OpenGL Error: #%d\n", src_info
, err
);
79 #define VG_STRINGIT( X ) #X
80 #define VG_CHECK_GL() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
88 void( *vg_on_exit
[16] )(void);
89 u32 vg_exit_count
= 0;
91 // Add a shutdown step
92 void vg_register_exit( void( *funcptr
)(void), const char *name
)
94 vg_info( "exit registered: (%u)'%s'\n", vg_exit_count
, name
);
95 vg_on_exit
[ vg_exit_count
++ ] = funcptr
;
100 for( int i
= vg_exit_count
-1; i
>= 0; i
-- )
102 vg_info( "engine_exit[%d]()\n", i
);
109 // Forcefully exit program after error
110 void vg_exiterr( const char *strErr
)
112 vg_error( "Engine Fatal: %s\n", strErr
);
120 void vg_mouse_callback( GLFWwindow
* ptrW
, double xpos
, double ypos
)
126 void vg_scroll_callback( GLFWwindow
* ptrW
, double xoffset
, double yoffset
)
128 vg_mouse_wheel
[0] += xoffset
;
129 vg_mouse_wheel
[1] += yoffset
;
132 void vg_framebuffer_resize_callback( GLFWwindow
*ptrW
, int w
, int h
)
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
;
145 static void vg_init( int argc
, char *argv
[], const char *window_name
)
147 // Initialize steamworks
152 // ==========================================================================================================================
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
);
159 #ifdef VG_CAPTURE_MODE
160 glfwWindowHint( GLFW_RESIZABLE
, GLFW_FALSE
);
162 glfwWindowHint( GLFW_RESIZABLE
, GLFW_TRUE
);
165 glfwWindowHint( GLFW_SAMPLES
, 4 );
167 GLFWmonitor
*monitor_primary
= glfwGetPrimaryMonitor();
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
);
175 if( !(vg_window
= glfwCreateWindow( vg_window_x
, vg_window_y
, window_name
, NULL
, NULL
)) )
177 vg_exiterr( "GLFW Failed to initialize" );
181 vg_register_exit( &glfwTerminate
, "glfwTerminate" );
184 glfwMakeContextCurrent( vg_window
);
185 glfwSwapInterval( 1 );
188 glfwSetFramebufferSizeCallback( vg_window
, vg_framebuffer_resize_callback
);
190 glfwSetCursorPosCallback( vg_window
, vg_mouse_callback
);
191 glfwSetScrollCallback( vg_window
, vg_scroll_callback
);
193 glfwSetCharCallback( vg_window
, console_proc_wchar
);
194 glfwSetKeyCallback( vg_window
, console_proc_key
);
195 //glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
197 if( !gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
) )
199 vg_exiterr( "Glad failed to initialize" );
202 const unsigned char* glver
= glGetString( GL_VERSION
);
203 vg_success( "Load setup complete, OpenGL version: %s\n", glver
);
205 vg_run_gfx_diagnostics();
207 for( int id
= 0; id
<= GLFW_JOYSTICK_LAST
; id
++ )
209 if( glfwJoystickIsGamepad( id
) )
211 vg_gamepad_name
= glfwGetGamepadName( id
);
212 vg_success( "Gamepad with mapping registered: %s\n", vg_gamepad_name
);
214 vg_gamepad_ready
= 1;
222 vg_register_exit( &vg_lines_free
, "vg_lines_free" );
224 vg_register_exit( &ui_default_free
, "UI" );
227 vg_register_exit( &vg_free
, "vg_free" );
228 vg_register_exit( &sw_free_opengl
, "steamworks (opengl)" );
230 if( vg_shaders_compile() )
235 vg_register_exit( &vg_console_free
, "Console" );
238 vg_register_exit( &vg_audio_free
, "vg_audio_free" );
240 vg_debugtools_setup();
243 while( !glfwWindowShouldClose( vg_window
) )
245 v2_copy( (v2f
){ 0.0f
, 0.0f
}, vg_mouse_wheel
);
250 vg_time_last
= vg_time
;
251 vg_time
= glfwGetTime();
252 vg_time_delta
= vg_minf( vg_time
- vg_time_last
, 0.1f
);
258 vg_lines_drawall((float*)vg_pv
);
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" ) );
266 vg_debugtools_draw();
268 ui_resolve( &ui_global_ctx
);
269 ui_draw( &ui_global_ctx
, NULL
);
272 glfwSwapBuffers( vg_window
);
281 // Screen projections
282 // ============================================================================================
284 void vg_projection_update(void)
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
;
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
);
295 m3x3_inv( vg_pv
, inverse
);
296 m3x3_mulv( inverse
, vg_mouse_ws
, vg_mouse_ws
);
301 u32 NvOptimusEnablement
= 0x00000001;
302 int AmdPowerXpressRequestHighPerformance
= 1;