1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
10 #include "gl/glad/glad.h"
13 #define STB_DS_IMPLEMENTATION
14 #include "stb/stb_ds.h"
16 #include "cglm/include/cglm/cglm.h"
18 void vg_register_exit( void( *funcptr
)(void), const char *name
);
19 void vg_exiterr( const char *strErr
);
21 #include "vg/vg_platform.h"
23 #include "vg/vg_audio.h"
24 #include "vg/vg_shader.h"
26 #include "steam/steamworks_thin.h"
28 static inline float vg_get_axis( const char *axis
) __attribute__((unused
));
29 static inline int vg_get_button( const char *button
) __attribute__((unused
));
30 static inline int vg_get_button_down( const char *button
) __attribute__((unused
));
31 static inline int vg_get_button_up( const char *button
) __attribute__((unused
));
34 GLFWwindow
* vg_window
;
35 int vg_window_x
= 1280;
36 int vg_window_y
= 720;
46 // ===========================================================================================================
47 GLFWgamepadstate vg_gamepad
;
48 int vg_gamepad_ready
= 0;
49 const char *vg_gamepad_name
= NULL
;
59 static struct axis_binding
73 static struct button_binding
82 #include "vg/config.h"
84 #pragma GCC diagnostic push
85 #pragma GCC diagnostic ignored "-Wreturn-type"
87 static inline float vg_get_axis( const char *axis
)
89 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
91 if( !strcmp( axis
, vg_axis_binds
[i
].name
) )
93 return vg_axis_binds
[i
].value
;
98 static inline struct button_binding
*vg_get_button_ptr( const char *button
)
100 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
102 if( !strcmp( button
, vg_button_binds
[i
].name
) )
104 return vg_button_binds
+ i
;
108 #pragma GCC diagnostic pop
110 static inline int vg_get_button( const char *button
)
112 return vg_get_button_ptr( button
)->value
;
115 static inline int vg_get_button_down( const char *button
)
117 struct button_binding
*bind
= vg_get_button_ptr( button
);
118 return bind
->value
& (bind
->value
^ bind
->prev
);
121 static inline int vg_get_button_up( const char *button
)
123 struct button_binding
*bind
= vg_get_button_ptr( button
);
124 return bind
->prev
& (bind
->value
^ bind
->prev
);
127 static inline int key_is_keyboard( int const id
)
129 static_assert( GLFW_MOUSE_BUTTON_LAST
< GLFW_KEY_SPACE
, "GLFW: Mouse has too many buttons" );
130 return id
> GLFW_MOUSE_BUTTON_LAST
;
133 // Mouse AND Keyboard get button press
134 int get_button_cross_device( int const id
)
136 if( key_is_keyboard( id
) )
138 return glfwGetKey( vg_window
, id
);
142 return glfwGetMouseButton( vg_window
, id
) == GLFW_PRESS
;
146 void vg_update_inputs(void)
148 // Update button inputs
149 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
151 struct button_binding
*binding
= vg_button_binds
+ i
;
152 binding
->prev
= binding
->value
;
154 if( vg_input_mode
== k_EInputMode_pc
)
156 binding
->value
= get_button_cross_device( binding
->bind
);
160 binding
->value
= vg_gamepad
.buttons
[ binding
->bind
];
164 // Update axis inputs
165 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
167 struct axis_binding
*binding
= vg_axis_binds
+ i
;
169 if( vg_input_mode
== k_EInputMode_pc
)
171 binding
->value
= get_button_cross_device( binding
->positive
);
172 binding
->value
-= get_button_cross_device( binding
->negative
);
176 binding
->value
= vg_gamepad
.axes
[ binding
->bind
];
182 // ===========================================================================================================
185 void vg_checkgl( const char *src_info
)
188 while( (err
= glGetError()) != GL_NO_ERROR
)
190 vg_error( "(%s) OpenGL Error: #%d\n", src_info
, err
);
194 #define VG_STRINGIT( X ) #X
195 #define VG_CHECK_GL() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
197 #define VG_CHECK_GL()
203 void( *vg_on_exit
[16] )(void);
204 u32 vg_exit_count
= 0;
206 // Add a shutdown step
207 void vg_register_exit( void( *funcptr
)(void), const char *name
)
209 vg_info( "exit registered: (%u)'%s'\n", vg_exit_count
, name
);
210 vg_on_exit
[ vg_exit_count
++ ] = funcptr
;
215 for( int i
= vg_exit_count
-1; i
>= 0; i
-- )
217 vg_info( "engine_exit[%d]()\n", i
);
224 // Forcefully exit program after error
225 void vg_exiterr( const char *strErr
)
227 vg_error( "Engine Fatal: %s\n", strErr
);
235 void vg_mouse_callback( GLFWwindow
* ptrW
, double xpos
, double ypos
)
241 void vg_scroll_callback( GLFWwindow
* ptrW
, double xoffset
, double yoffset
)
246 void vg_framebuffer_resize_callback( GLFWwindow
*ptrW
, int w
, int h
)
252 static void vg_start(void) VG_GAMELOOP
;
253 static void vg_update(void) VG_GAMELOOP
;
254 static void vg_render(void) VG_GAMELOOP
;
255 static void vg_ui(void) VG_GAMELOOP
;
256 static void vg_free(void) VG_GAMELOOP
;
258 static void vg_init( int argc
, char *argv
[], const char *window_name
)
261 // Initialize steamworks
262 if( !sw_init( 1218140U ) )
264 vg_exiterr( "Steamworks failed to initialize" );
268 vg_register_exit( &sw_SteamAPI_Shutdown
, "SteamAPI" );
273 // ==========================================================================================================================
275 glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR
, 3 );
276 glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR
, 3 );
277 glfwWindowHint( GLFW_OPENGL_PROFILE
, GLFW_OPENGL_CORE_PROFILE
);
278 glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT
, GL_TRUE
);
280 glfwWindowHint( GLFW_SAMPLES
, 4 );
282 GLFWmonitor
*monitor_primary
= glfwGetPrimaryMonitor();
284 const GLFWvidmode
*mode
= glfwGetVideoMode( monitor_primary
);
285 glfwWindowHint( GLFW_RED_BITS
, mode
->redBits
);
286 glfwWindowHint( GLFW_GREEN_BITS
, mode
->greenBits
);
287 glfwWindowHint( GLFW_BLUE_BITS
, mode
->blueBits
);
288 glfwWindowHint( GLFW_REFRESH_RATE
, mode
->refreshRate
);
290 if( !(vg_window
= glfwCreateWindow( vg_window_x
, vg_window_y
, window_name
, NULL
, NULL
)) )
292 vg_exiterr( "GLFW Failed to initialize" );
296 vg_register_exit( &glfwTerminate
, "glfwTerminate" );
299 glfwMakeContextCurrent( vg_window
);
300 glfwSwapInterval( 1 );
303 glfwSetFramebufferSizeCallback( vg_window
, vg_framebuffer_resize_callback
);
305 glfwSetCursorPosCallback( vg_window
, vg_mouse_callback
);
306 glfwSetScrollCallback( vg_window
, vg_scroll_callback
);
308 //glfwSetCharCallback( vg_window, console_proc_wchar );
309 //glfwSetKeyCallback( vg_window, console_proc_key );
310 //glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
312 if( !gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
) )
314 vg_exiterr( "Glad failed to initialize" );
317 const unsigned char* glver
= glGetString( GL_VERSION
);
318 vg_success( "Load setup complete, OpenGL version: %s\n", glver
);
320 for( int id
= 0; id
<= GLFW_JOYSTICK_LAST
; id
++ )
322 if( glfwJoystickIsGamepad( id
) )
324 vg_gamepad_name
= glfwGetGamepadName( id
);
325 vg_success( "Gamepad with mapping registered: %s\n", vg_gamepad_name
);
327 vg_gamepad_ready
= 1;
335 vg_register_exit( &vg_audio_free
, "vg_audio_free" );
338 if( vg_shaders_compile() )
341 while( !glfwWindowShouldClose( vg_window
) )
346 sw_RunSteamEventLoop();
349 vg_time_last
= vg_time
;
350 vg_time
= glfwGetTime();
351 vg_time_delta
= vg_min( vg_time
- vg_time_last
, 0.1f
);
357 // Update mashed projections etc
362 glfwSwapBuffers( vg_window
);
372 u32 NvOptimusEnablement
= 0x00000001;
373 int AmdPowerXpressRequestHighPerformance
= 1;