1 /* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */
12 #include "glad/glad.h"
13 #include "glfw/glfw3.h"
15 #define STB_DS_IMPLEMENTATION
16 #include "stb/stb_ds.h"
18 #define QOI_IMPLEMENTATION
19 #include "phoboslab/qoi.h"
21 #include "vg/vg_platform.h"
23 void vg_register_exit( void( *funcptr
)(void), const char *name
);
24 void vg_exiterr( const char *strErr
);
28 #include "vg/vg_gldiag.h"
33 GLFWwindow
* vg_window
;
41 #ifdef VG_CAPTURE_MODE
42 int vg_window_x
= 1920;
43 int vg_window_y
= 1080;
45 int vg_window_x
= 1366;
46 int vg_window_y
= 768;
57 #include "vg/vg_audio.h"
58 #include "vg/vg_shader.h"
59 #include "vg/vg_lines.h"
60 #include "vg/vg_tex.h"
61 #include "vg/vg_input.h"
63 #include "vg/vg_console.h"
64 #include "vg/vg_debug.h"
67 #include "vg/vg_steamworks.h"
71 void vg_checkgl( const char *src_info
)
74 while( (err
= glGetError()) != GL_NO_ERROR
)
76 vg_error( "(%s) OpenGL Error: #%d\n", src_info
, err
);
80 #define VG_STRINGIT( X ) #X
81 #define VG_CHECK_GL() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
89 void( *vg_on_exit
[16] )(void);
90 u32 vg_exit_count
= 0;
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 void vg_exiterr( const char *strErr
)
111 vg_error( "Engine Fatal: %s\n", strErr
);
116 void vg_mouse_callback( GLFWwindow
* ptrW
, double xpos
, double ypos
)
122 void vg_scroll_callback( GLFWwindow
* ptrW
, double xoffset
, double yoffset
)
124 vg_mouse_wheel
[0] += xoffset
;
125 vg_mouse_wheel
[1] += yoffset
;
129 static void vg_register(void) VG_GAMELOOP
;
130 static void vg_start(void) VG_GAMELOOP
;
131 static void vg_update(void) VG_GAMELOOP
;
132 static void vg_framebuffer_resize(int w
, int h
) VG_GAMELOOP
;
133 static void vg_render(void) VG_GAMELOOP
;
134 static void vg_ui(void) VG_GAMELOOP
;
135 static void vg_free(void) VG_GAMELOOP
;
137 void vg_framebuffer_resize_callback( GLFWwindow
*ptrW
, int w
, int h
)
142 #ifdef VG_FRAMEBUFFER_RESIZE
143 vg_framebuffer_resize(w
,h
);
147 static void vg_init( int argc
, char *argv
[], const char *window_name
)
155 glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR
, 3 );
156 glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR
, 3 );
157 glfwWindowHint( GLFW_OPENGL_PROFILE
, GLFW_OPENGL_CORE_PROFILE
);
158 glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT
, GL_TRUE
);
160 #ifdef VG_CAPTURE_MODE
161 glfwWindowHint( GLFW_RESIZABLE
, GLFW_FALSE
);
163 glfwWindowHint( GLFW_RESIZABLE
, GLFW_TRUE
);
165 glfwWindowHint(GLFW_DOUBLEBUFFER
, GLFW_TRUE
);
168 glfwWindowHint(GLFW_SAMPLES
,4);
171 GLFWmonitor
*monitor_primary
= glfwGetPrimaryMonitor();
173 const GLFWvidmode
*mode
= glfwGetVideoMode( monitor_primary
);
174 glfwWindowHint( GLFW_RED_BITS
, mode
->redBits
);
175 glfwWindowHint( GLFW_GREEN_BITS
, mode
->greenBits
);
176 glfwWindowHint( GLFW_BLUE_BITS
, mode
->blueBits
);
178 int refresh_rate
= mode
->refreshRate
;
180 if( refresh_rate
< 28 || refresh_rate
>= 144 )
183 glfwWindowHint( GLFW_REFRESH_RATE
, refresh_rate
);
185 if( !(vg_window
= glfwCreateWindow( vg_window_x
, vg_window_y
,
186 window_name
, NULL
, NULL
)) )
187 vg_exiterr( "GLFW Failed to initialize" );
189 vg_register_exit( &glfwTerminate
, "glfwTerminate" );
191 glfwMakeContextCurrent( vg_window
);
192 glfwSwapInterval( 1 );
194 glfwSetWindowSizeLimits( vg_window
, 800, 600, GLFW_DONT_CARE
,GLFW_DONT_CARE
);
195 glfwSetFramebufferSizeCallback( vg_window
, vg_framebuffer_resize_callback
);
197 glfwSetCursorPosCallback( vg_window
, vg_mouse_callback
);
198 glfwSetScrollCallback( vg_window
, vg_scroll_callback
);
200 glfwSetCharCallback( vg_window
, console_proc_wchar
);
201 glfwSetKeyCallback( vg_window
, console_proc_key
);
203 glfwSetInputMode(vg_window
, GLFW_CURSOR
, GLFW_CURSOR_HIDDEN
);
206 if( !gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
) )
207 vg_exiterr( "Glad failed to initialize" );
209 const unsigned char* glver
= glGetString( GL_VERSION
);
210 vg_success( "Load setup complete, OpenGL version: %s\n", glver
);
212 vg_run_gfx_diagnostics();
216 vg_register_exit( &vg_lines_free
, "vg_lines_free" );
218 vg_register_exit( &ui_default_free
, "UI" );
221 vg_register_exit( &vg_free
, "vg_free" );
223 if( vg_shaders_compile() )
228 vg_register_exit( &vg_console_free
, "Console" );
231 vg_register_exit( &vg_audio_free
, "vg_audio_free" );
233 vg_debugtools_setup();
238 while( !glfwWindowShouldClose( vg_window
) )
240 v2_copy( (v2f
){ 0.0f
, 0.0f
}, vg_mouse_wheel
);
248 vg_time_last
= vg_time
;
249 vg_time
= glfwGetTime();
250 vg_time_delta
= vg_minf( vg_time
- vg_time_last
, 0.1f
);
256 vg_lines_drawall((float*)vg_pv
);
259 ui_begin( &ui_global_ctx
, vg_window_x
, vg_window_y
);
260 ui_set_mouse( &ui_global_ctx
, vg_mouse
[0], vg_mouse
[1],
261 vg_get_button_state( "primary" ) );
265 vg_debugtools_draw();
267 ui_resolve( &ui_global_ctx
);
268 ui_draw( &ui_global_ctx
, NULL
);
271 glfwSwapBuffers( vg_window
);
280 void vg_projection_update(void)
283 * Reproject screenspace mouse into world
286 vg_mouse_ws
[0] = vg_mouse
[0];
287 vg_mouse_ws
[1] = vg_mouse
[1];
288 vg_mouse_ws
[2] = 1.0f
;
290 vg_mouse_ws
[0] = (2.0f
* vg_mouse_ws
[0]) / ((float)vg_window_x
) - 1.0f
;
291 vg_mouse_ws
[1] = -((2.0f
* vg_mouse_ws
[1]) / ((float)vg_window_y
) - 1.0f
);
294 m3x3_inv( vg_pv
, inverse
);
295 m3x3_mulv( inverse
, vg_mouse_ws
, vg_mouse_ws
);
302 * Graphic cards will check these to force it to use the GPU
304 u32 NvOptimusEnablement
= 0x00000001;
305 int AmdPowerXpressRequestHighPerformance
= 1;