1 /* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */
14 #if defined(VG_SERVER) || defined(VG_TOOLS)
19 #include "../../dep/glad/glad.h"
20 #include "../../dep/glfw/glfw3.h"
23 #define STB_DS_IMPLEMENTATION
24 #include "stb/stb_ds.h"
26 #define QOI_IMPLEMENTATION
27 #include "phoboslab/qoi.h"
29 #include "vg_stdint.h"
30 #include "vg_platform.h"
32 void vg_register_exit( void( *funcptr
)(void), const char *name
);
33 void vg_exiterr( const char *strErr
);
39 //#include "vg_steamworks.h"
44 #include "vg_gldiag.h"
50 GLFWwindow
* vg_window
;
58 #ifdef VG_CAPTURE_MODE
59 int vg_window_x
= 1920;
60 int vg_window_y
= 1080;
62 int vg_window_x
= 1366;
63 int vg_window_y
= 768;
75 #include "vg_shader.h"
79 #include "vg_console.h"
84 void vg_checkgl( const char *src_info
)
87 while( (err
= glGetError()) != GL_NO_ERROR
)
89 vg_error( "(%s) OpenGL Error: #%d\n", src_info
, err
);
93 #define VG_STRINGIT( X ) #X
94 #define VG_CHECK_GL() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
102 void( *vg_on_exit
[16] )(void);
103 u32 vg_exit_count
= 0;
105 void vg_register_exit( void( *funcptr
)(void), const char *name
)
107 vg_info( "exit registered: (%u)'%s'\n", vg_exit_count
, name
);
108 vg_on_exit
[ vg_exit_count
++ ] = funcptr
;
113 for( int i
= vg_exit_count
-1; i
>= 0; i
-- )
115 vg_info( "engine_exit[%d]()\n", i
);
122 void vg_exiterr( const char *strErr
)
124 vg_error( "Engine Fatal: %s\n", strErr
);
129 void vg_mouse_callback( GLFWwindow
* ptrW
, double xpos
, double ypos
)
135 void vg_scroll_callback( GLFWwindow
* ptrW
, double xoffset
, double yoffset
)
137 vg_mouse_wheel
[0] += xoffset
;
138 vg_mouse_wheel
[1] += yoffset
;
142 static void vg_register(void) VG_GAMELOOP
;
143 static void vg_start(void) VG_GAMELOOP
;
144 static void vg_update(void) VG_GAMELOOP
;
145 static void vg_framebuffer_resize(int w
, int h
) VG_GAMELOOP
;
146 static void vg_render(void) VG_GAMELOOP
;
147 static void vg_ui(void) VG_GAMELOOP
;
148 static void vg_free(void) VG_GAMELOOP
;
150 void vg_framebuffer_resize_callback( GLFWwindow
*ptrW
, int w
, int h
)
155 #ifdef VG_FRAMEBUFFER_RESIZE
156 vg_framebuffer_resize(w
,h
);
160 static void vg_init( int argc
, char *argv
[], const char *window_name
)
168 glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR
, 3 );
169 glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR
, 3 );
170 glfwWindowHint( GLFW_OPENGL_PROFILE
, GLFW_OPENGL_CORE_PROFILE
);
171 glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT
, GL_TRUE
);
173 #ifdef VG_CAPTURE_MODE
174 glfwWindowHint( GLFW_RESIZABLE
, GLFW_FALSE
);
176 glfwWindowHint( GLFW_RESIZABLE
, GLFW_TRUE
);
178 glfwWindowHint(GLFW_DOUBLEBUFFER
, GLFW_TRUE
);
181 glfwWindowHint(GLFW_SAMPLES
,4);
184 GLFWmonitor
*monitor_primary
= glfwGetPrimaryMonitor();
186 const GLFWvidmode
*mode
= glfwGetVideoMode( monitor_primary
);
187 glfwWindowHint( GLFW_RED_BITS
, mode
->redBits
);
188 glfwWindowHint( GLFW_GREEN_BITS
, mode
->greenBits
);
189 glfwWindowHint( GLFW_BLUE_BITS
, mode
->blueBits
);
191 int refresh_rate
= mode
->refreshRate
;
193 if( refresh_rate
< 28 || refresh_rate
>= 144 )
196 glfwWindowHint( GLFW_REFRESH_RATE
, refresh_rate
);
198 if( !(vg_window
= glfwCreateWindow( vg_window_x
, vg_window_y
,
199 window_name
, NULL
, NULL
)) )
200 vg_exiterr( "GLFW Failed to initialize" );
202 vg_register_exit( &glfwTerminate
, "glfwTerminate" );
204 glfwMakeContextCurrent( vg_window
);
205 glfwSwapInterval( 1 );
207 glfwSetWindowSizeLimits( vg_window
, 800, 600, GLFW_DONT_CARE
,GLFW_DONT_CARE
);
208 glfwSetFramebufferSizeCallback( vg_window
, vg_framebuffer_resize_callback
);
210 glfwSetCursorPosCallback( vg_window
, vg_mouse_callback
);
211 glfwSetScrollCallback( vg_window
, vg_scroll_callback
);
213 glfwSetCharCallback( vg_window
, console_proc_wchar
);
214 glfwSetKeyCallback( vg_window
, console_proc_key
);
216 glfwSetInputMode(vg_window
, GLFW_CURSOR
, GLFW_CURSOR_HIDDEN
);
219 if( !gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
) )
220 vg_exiterr( "Glad failed to initialize" );
222 const unsigned char* glver
= glGetString( GL_VERSION
);
223 vg_success( "Load setup complete, OpenGL version: %s\n", glver
);
225 vg_run_gfx_diagnostics();
229 vg_register_exit( &vg_lines_free
, "vg_lines_free" );
231 vg_register_exit( &ui_default_free
, "UI" );
234 vg_register_exit( &vg_free
, "vg_free" );
237 vg_shaders_recompile(0,NULL
);
238 vg_register_exit( &vg_shaders_free
, "vg_shaders_free" );
239 vg_function_push( (struct vg_cmd
){
241 .function
= vg_shaders_recompile
248 vg_register_exit( &vg_console_free
, "Console" );
251 vg_register_exit( &vg_audio_free
, "vg_audio_free" );
253 vg_debugtools_setup();
258 while( !glfwWindowShouldClose( vg_window
) )
260 v2_copy( (v2f
){ 0.0f
, 0.0f
}, vg_mouse_wheel
);
268 vg_time_last
= vg_time
;
269 vg_time
= glfwGetTime();
270 vg_time_delta
= vg_minf( vg_time
- vg_time_last
, 0.1f
);
276 vg_lines_drawall((float*)vg_pv
);
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" ) );
285 vg_debugtools_draw();
287 ui_resolve( &ui_global_ctx
);
288 ui_draw( &ui_global_ctx
, NULL
);
291 glfwSwapBuffers( vg_window
);
299 void vg_projection_update(void)
302 * Reproject screenspace mouse into world
305 vg_mouse_ws
[0] = vg_mouse
[0];
306 vg_mouse_ws
[1] = vg_mouse
[1];
307 vg_mouse_ws
[2] = 1.0f
;
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
);
313 m3x3_inv( vg_pv
, inverse
);
314 m3x3_mulv( inverse
, vg_mouse_ws
, vg_mouse_ws
);
321 * Graphic cards will check these to force it to use the GPU
323 u32 NvOptimusEnablement
= 0x00000001;
324 int AmdPowerXpressRequestHighPerformance
= 1;