1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
10 #include "gl/glad/glad.h"
13 #define STB_DS_IMPLEMENTATION
14 #define STB_IMAGE_IMPLEMENTATION
15 #include "stb/stb_ds.h"
16 #include "stb/stb_image.h"
18 #include "vg/vg_platform.h"
20 void vg_register_exit( void( *funcptr
)(void), const char *name
);
21 void vg_exiterr( const char *strErr
);
30 #include "vg/vg_audio.h"
31 #include "vg/vg_shader.h"
32 #include "vg/vg_lines.h"
33 #include "vg/vg_tex.h"
35 #include "steam/steamworks_thin.h"
37 static inline float vg_get_axis( const char *axis
) __attribute__((unused
));
38 static inline int vg_get_button( const char *button
) __attribute__((unused
));
39 static inline int vg_get_button_down( const char *button
) __attribute__((unused
));
40 static inline int vg_get_button_up( const char *button
) __attribute__((unused
));
43 GLFWwindow
* vg_window
;
44 int vg_window_x
= 1280;
45 int vg_window_y
= 720;
55 // ===========================================================================================================
56 GLFWgamepadstate vg_gamepad
;
57 int vg_gamepad_ready
= 0;
58 const char *vg_gamepad_name
= NULL
;
68 static struct axis_binding
82 static struct button_binding
91 #include "vg/config.h"
93 #pragma GCC diagnostic push
94 #pragma GCC diagnostic ignored "-Wreturn-type"
96 static inline float vg_get_axis( const char *axis
)
98 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
100 if( !strcmp( axis
, vg_axis_binds
[i
].name
) )
102 return vg_axis_binds
[i
].value
;
107 static inline struct button_binding
*vg_get_button_ptr( const char *button
)
109 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
111 if( !strcmp( button
, vg_button_binds
[i
].name
) )
113 return vg_button_binds
+ i
;
117 #pragma GCC diagnostic pop
119 static inline int vg_get_button( const char *button
)
121 return vg_get_button_ptr( button
)->value
;
124 static inline int vg_get_button_down( const char *button
)
126 struct button_binding
*bind
= vg_get_button_ptr( button
);
127 return bind
->value
& (bind
->value
^ bind
->prev
);
130 static inline int vg_get_button_up( const char *button
)
132 struct button_binding
*bind
= vg_get_button_ptr( button
);
133 return bind
->prev
& (bind
->value
^ bind
->prev
);
136 static inline int key_is_keyboard( int const id
)
138 vg_static_assert( GLFW_MOUSE_BUTTON_LAST
< GLFW_KEY_SPACE
, "GLFW: Mouse has too many buttons" );
139 return id
> GLFW_MOUSE_BUTTON_LAST
;
142 // Mouse AND Keyboard get button press
143 int get_button_cross_device( int const id
)
145 if( key_is_keyboard( id
) )
147 return glfwGetKey( vg_window
, id
);
151 return glfwGetMouseButton( vg_window
, id
) == GLFW_PRESS
;
155 void vg_update_inputs(void)
157 // Update button inputs
158 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
160 struct button_binding
*binding
= vg_button_binds
+ i
;
161 binding
->prev
= binding
->value
;
163 if( vg_input_mode
== k_EInputMode_pc
)
165 binding
->value
= get_button_cross_device( binding
->bind
);
169 binding
->value
= vg_gamepad
.buttons
[ binding
->bind
];
173 // Update axis inputs
174 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
176 struct axis_binding
*binding
= vg_axis_binds
+ i
;
178 if( vg_input_mode
== k_EInputMode_pc
)
180 binding
->value
= get_button_cross_device( binding
->positive
);
181 binding
->value
-= get_button_cross_device( binding
->negative
);
185 binding
->value
= vg_gamepad
.axes
[ binding
->bind
];
191 // ===========================================================================================================
194 void vg_checkgl( const char *src_info
)
197 while( (err
= glGetError()) != GL_NO_ERROR
)
199 vg_error( "(%s) OpenGL Error: #%d\n", src_info
, err
);
203 #define VG_STRINGIT( X ) #X
204 #define VG_CHECK_GL() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
206 #define VG_CHECK_GL()
212 void( *vg_on_exit
[16] )(void);
213 u32 vg_exit_count
= 0;
215 // Add a shutdown step
216 void vg_register_exit( void( *funcptr
)(void), const char *name
)
218 vg_info( "exit registered: (%u)'%s'\n", vg_exit_count
, name
);
219 vg_on_exit
[ vg_exit_count
++ ] = funcptr
;
224 for( int i
= vg_exit_count
-1; i
>= 0; i
-- )
226 vg_info( "engine_exit[%d]()\n", i
);
233 // Forcefully exit program after error
234 void vg_exiterr( const char *strErr
)
236 vg_error( "Engine Fatal: %s\n", strErr
);
244 void vg_mouse_callback( GLFWwindow
* ptrW
, double xpos
, double ypos
)
250 void vg_scroll_callback( GLFWwindow
* ptrW
, double xoffset
, double yoffset
)
255 void vg_framebuffer_resize_callback( GLFWwindow
*ptrW
, int w
, int h
)
261 static void vg_register(void) VG_GAMELOOP
;
262 static void vg_start(void) VG_GAMELOOP
;
263 static void vg_update(void) VG_GAMELOOP
;
264 static void vg_render(void) VG_GAMELOOP
;
265 static void vg_ui(void) VG_GAMELOOP
;
266 static void vg_free(void) VG_GAMELOOP
;
268 static void vg_init( int argc
, char *argv
[], const char *window_name
)
271 // Initialize steamworks
272 if( !sw_init( 1218140U ) )
274 vg_exiterr( "Steamworks failed to initialize" );
278 vg_register_exit( &sw_SteamAPI_Shutdown
, "SteamAPI" );
283 // ==========================================================================================================================
285 glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR
, 3 );
286 glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR
, 3 );
287 glfwWindowHint( GLFW_OPENGL_PROFILE
, GLFW_OPENGL_CORE_PROFILE
);
288 glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT
, GL_TRUE
);
290 glfwWindowHint( GLFW_SAMPLES
, 4 );
292 GLFWmonitor
*monitor_primary
= glfwGetPrimaryMonitor();
294 const GLFWvidmode
*mode
= glfwGetVideoMode( monitor_primary
);
295 glfwWindowHint( GLFW_RED_BITS
, mode
->redBits
);
296 glfwWindowHint( GLFW_GREEN_BITS
, mode
->greenBits
);
297 glfwWindowHint( GLFW_BLUE_BITS
, mode
->blueBits
);
298 glfwWindowHint( GLFW_REFRESH_RATE
, mode
->refreshRate
);
300 if( !(vg_window
= glfwCreateWindow( vg_window_x
, vg_window_y
, window_name
, NULL
, NULL
)) )
302 vg_exiterr( "GLFW Failed to initialize" );
306 vg_register_exit( &glfwTerminate
, "glfwTerminate" );
309 glfwMakeContextCurrent( vg_window
);
310 glfwSwapInterval( 1 );
313 glfwSetFramebufferSizeCallback( vg_window
, vg_framebuffer_resize_callback
);
315 glfwSetCursorPosCallback( vg_window
, vg_mouse_callback
);
316 glfwSetScrollCallback( vg_window
, vg_scroll_callback
);
318 //glfwSetCharCallback( vg_window, console_proc_wchar );
319 //glfwSetKeyCallback( vg_window, console_proc_key );
320 //glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
322 if( !gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
) )
324 vg_exiterr( "Glad failed to initialize" );
327 const unsigned char* glver
= glGetString( GL_VERSION
);
328 vg_success( "Load setup complete, OpenGL version: %s\n", glver
);
330 for( int id
= 0; id
<= GLFW_JOYSTICK_LAST
; id
++ )
332 if( glfwJoystickIsGamepad( id
) )
334 vg_gamepad_name
= glfwGetGamepadName( id
);
335 vg_success( "Gamepad with mapping registered: %s\n", vg_gamepad_name
);
337 vg_gamepad_ready
= 1;
345 vg_register_exit( &vg_audio_free
, "vg_audio_free" );
347 vg_register_exit( &vg_lines_free
, "vg_lines_free" );
350 vg_register_exit( &vg_free
, "vg_free" );
352 if( vg_shaders_compile() )
357 while( !glfwWindowShouldClose( vg_window
) )
362 sw_RunSteamEventLoop();
365 vg_time_last
= vg_time
;
366 vg_time
= glfwGetTime();
367 vg_time_delta
= vg_min( vg_time
- vg_time_last
, 0.1f
);
377 glfwSwapBuffers( vg_window
);
386 // Screen projections
387 // ============================================================================================
389 void vg_projection_update(void)
391 // Do transform local->world
392 vg_mouse_ws
[0] = vg_mouse
[0];
393 vg_mouse_ws
[1] = vg_mouse
[1];
394 vg_mouse_ws
[2] = 1.0f
;
396 vg_mouse_ws
[0] = (2.0f
* vg_mouse_ws
[0]) / ((float)vg_window_x
) - 1.0f
;
397 vg_mouse_ws
[1] = -((2.0f
* vg_mouse_ws
[1]) / ((float)vg_window_y
) - 1.0f
);
400 m3x3_inv( vg_pv
, inverse
);
401 m3x3_mulv( inverse
, vg_mouse_ws
, vg_mouse_ws
);
406 u32 NvOptimusEnablement
= 0x00000001;
407 int AmdPowerXpressRequestHighPerformance
= 1;