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
);
27 #include "vg/vg_audio.h"
28 #include "vg/vg_shader.h"
29 #include "vg/vg_lines.h"
30 #include "vg/vg_tex.h"
32 #include "steam/steamworks_thin.h"
34 static inline float vg_get_axis( const char *axis
) __attribute__((unused
));
35 static inline int vg_get_button( const char *button
) __attribute__((unused
));
36 static inline int vg_get_button_down( const char *button
) __attribute__((unused
));
37 static inline int vg_get_button_up( const char *button
) __attribute__((unused
));
40 GLFWwindow
* vg_window
;
41 int vg_window_x
= 1280;
42 int vg_window_y
= 720;
52 // ===========================================================================================================
53 GLFWgamepadstate vg_gamepad
;
54 int vg_gamepad_ready
= 0;
55 const char *vg_gamepad_name
= NULL
;
65 static struct axis_binding
79 static struct button_binding
88 #include "vg/config.h"
90 #pragma GCC diagnostic push
91 #pragma GCC diagnostic ignored "-Wreturn-type"
93 static inline float vg_get_axis( const char *axis
)
95 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
97 if( !strcmp( axis
, vg_axis_binds
[i
].name
) )
99 return vg_axis_binds
[i
].value
;
104 static inline struct button_binding
*vg_get_button_ptr( const char *button
)
106 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
108 if( !strcmp( button
, vg_button_binds
[i
].name
) )
110 return vg_button_binds
+ i
;
114 #pragma GCC diagnostic pop
116 static inline int vg_get_button( const char *button
)
118 return vg_get_button_ptr( button
)->value
;
121 static inline int vg_get_button_down( const char *button
)
123 struct button_binding
*bind
= vg_get_button_ptr( button
);
124 return bind
->value
& (bind
->value
^ bind
->prev
);
127 static inline int vg_get_button_up( const char *button
)
129 struct button_binding
*bind
= vg_get_button_ptr( button
);
130 return bind
->prev
& (bind
->value
^ bind
->prev
);
133 static inline int key_is_keyboard( int const id
)
135 vg_static_assert( GLFW_MOUSE_BUTTON_LAST
< GLFW_KEY_SPACE
, "GLFW: Mouse has too many buttons" );
136 return id
> GLFW_MOUSE_BUTTON_LAST
;
139 // Mouse AND Keyboard get button press
140 int get_button_cross_device( int const id
)
142 if( key_is_keyboard( id
) )
144 return glfwGetKey( vg_window
, id
);
148 return glfwGetMouseButton( vg_window
, id
) == GLFW_PRESS
;
152 void vg_update_inputs(void)
154 // Update button inputs
155 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
157 struct button_binding
*binding
= vg_button_binds
+ i
;
158 binding
->prev
= binding
->value
;
160 if( vg_input_mode
== k_EInputMode_pc
)
162 binding
->value
= get_button_cross_device( binding
->bind
);
166 binding
->value
= vg_gamepad
.buttons
[ binding
->bind
];
170 // Update axis inputs
171 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
173 struct axis_binding
*binding
= vg_axis_binds
+ i
;
175 if( vg_input_mode
== k_EInputMode_pc
)
177 binding
->value
= get_button_cross_device( binding
->positive
);
178 binding
->value
-= get_button_cross_device( binding
->negative
);
182 binding
->value
= vg_gamepad
.axes
[ binding
->bind
];
188 // ===========================================================================================================
191 void vg_checkgl( const char *src_info
)
194 while( (err
= glGetError()) != GL_NO_ERROR
)
196 vg_error( "(%s) OpenGL Error: #%d\n", src_info
, err
);
200 #define VG_STRINGIT( X ) #X
201 #define VG_CHECK_GL() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
203 #define VG_CHECK_GL()
209 void( *vg_on_exit
[16] )(void);
210 u32 vg_exit_count
= 0;
212 // Add a shutdown step
213 void vg_register_exit( void( *funcptr
)(void), const char *name
)
215 vg_info( "exit registered: (%u)'%s'\n", vg_exit_count
, name
);
216 vg_on_exit
[ vg_exit_count
++ ] = funcptr
;
221 for( int i
= vg_exit_count
-1; i
>= 0; i
-- )
223 vg_info( "engine_exit[%d]()\n", i
);
230 // Forcefully exit program after error
231 void vg_exiterr( const char *strErr
)
233 vg_error( "Engine Fatal: %s\n", strErr
);
241 void vg_mouse_callback( GLFWwindow
* ptrW
, double xpos
, double ypos
)
247 void vg_scroll_callback( GLFWwindow
* ptrW
, double xoffset
, double yoffset
)
252 void vg_framebuffer_resize_callback( GLFWwindow
*ptrW
, int w
, int h
)
258 static void vg_register(void) VG_GAMELOOP
;
259 static void vg_start(void) VG_GAMELOOP
;
260 static void vg_update(void) VG_GAMELOOP
;
261 static void vg_render(void) VG_GAMELOOP
;
262 static void vg_ui(void) VG_GAMELOOP
;
263 static void vg_free(void) VG_GAMELOOP
;
265 static void vg_init( int argc
, char *argv
[], const char *window_name
)
268 // Initialize steamworks
269 if( !sw_init( 1218140U ) )
271 vg_exiterr( "Steamworks failed to initialize" );
275 vg_register_exit( &sw_SteamAPI_Shutdown
, "SteamAPI" );
280 // ==========================================================================================================================
282 glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR
, 3 );
283 glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR
, 3 );
284 glfwWindowHint( GLFW_OPENGL_PROFILE
, GLFW_OPENGL_CORE_PROFILE
);
285 glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT
, GL_TRUE
);
287 glfwWindowHint( GLFW_SAMPLES
, 1 );
289 GLFWmonitor
*monitor_primary
= glfwGetPrimaryMonitor();
291 const GLFWvidmode
*mode
= glfwGetVideoMode( monitor_primary
);
292 glfwWindowHint( GLFW_RED_BITS
, mode
->redBits
);
293 glfwWindowHint( GLFW_GREEN_BITS
, mode
->greenBits
);
294 glfwWindowHint( GLFW_BLUE_BITS
, mode
->blueBits
);
295 glfwWindowHint( GLFW_REFRESH_RATE
, mode
->refreshRate
);
297 if( !(vg_window
= glfwCreateWindow( vg_window_x
, vg_window_y
, window_name
, NULL
, NULL
)) )
299 vg_exiterr( "GLFW Failed to initialize" );
303 vg_register_exit( &glfwTerminate
, "glfwTerminate" );
306 glfwMakeContextCurrent( vg_window
);
307 glfwSwapInterval( 1 );
310 glfwSetFramebufferSizeCallback( vg_window
, vg_framebuffer_resize_callback
);
312 glfwSetCursorPosCallback( vg_window
, vg_mouse_callback
);
313 glfwSetScrollCallback( vg_window
, vg_scroll_callback
);
315 //glfwSetCharCallback( vg_window, console_proc_wchar );
316 //glfwSetKeyCallback( vg_window, console_proc_key );
317 //glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
319 if( !gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
) )
321 vg_exiterr( "Glad failed to initialize" );
324 const unsigned char* glver
= glGetString( GL_VERSION
);
325 vg_success( "Load setup complete, OpenGL version: %s\n", glver
);
327 for( int id
= 0; id
<= GLFW_JOYSTICK_LAST
; id
++ )
329 if( glfwJoystickIsGamepad( id
) )
331 vg_gamepad_name
= glfwGetGamepadName( id
);
332 vg_success( "Gamepad with mapping registered: %s\n", vg_gamepad_name
);
334 vg_gamepad_ready
= 1;
342 vg_register_exit( &vg_audio_free
, "vg_audio_free" );
344 vg_register_exit( &vg_lines_free
, "vg_lines_free" );
347 vg_register_exit( &vg_free
, "vg_free" );
349 if( vg_shaders_compile() )
354 while( !glfwWindowShouldClose( vg_window
) )
359 sw_RunSteamEventLoop();
362 vg_time_last
= vg_time
;
363 vg_time
= glfwGetTime();
364 vg_time_delta
= vg_min( vg_time
- vg_time_last
, 0.1f
);
374 glfwSwapBuffers( vg_window
);
383 // Screen projections
384 // ============================================================================================
386 void vg_projection_update(void)
388 // Do transform local->world
389 vg_mouse_ws
[0] = vg_mouse
[0];
390 vg_mouse_ws
[1] = vg_mouse
[1];
391 vg_mouse_ws
[2] = 1.0f
;
393 vg_mouse_ws
[0] = (2.0f
* vg_mouse_ws
[0]) / ((float)vg_window_x
) - 1.0f
;
394 vg_mouse_ws
[1] = -((2.0f
* vg_mouse_ws
[1]) / ((float)vg_window_y
) - 1.0f
);
397 m3x3_inv( vg_pv
, inverse
);
398 m3x3_mulv( inverse
, vg_mouse_ws
, vg_mouse_ws
);
401 u32 NvOptimusEnablement
= 0x00000001;
402 int AmdPowerXpressRequestHighPerformance
= 1;