1 // Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
3 static inline float vg_get_axis( const char *axis
) __attribute__((unused
));
4 static inline int vg_get_button( const char *button
) __attribute__((unused
));
5 static inline int vg_get_button_down( const char *button
) __attribute__((unused
));
6 static inline int vg_get_button_up( const char *button
) __attribute__((unused
));
9 // ===========================================================================================================
10 GLFWgamepadstate vg_gamepad
;
11 int vg_gamepad_ready
= 0;
12 const char *vg_gamepad_name
= NULL
;
22 static struct axis_binding
36 static struct button_binding
45 #include "vg/config.h"
47 #pragma GCC diagnostic push
48 #pragma GCC diagnostic ignored "-Wreturn-type"
50 static inline float vg_get_axis( const char *axis
)
52 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
54 if( !strcmp( axis
, vg_axis_binds
[i
].name
) )
56 return vg_axis_binds
[i
].value
;
61 static inline struct button_binding
*vg_get_button_ptr( const char *button
)
63 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
65 if( !strcmp( button
, vg_button_binds
[i
].name
) )
67 return vg_button_binds
+ i
;
71 #pragma GCC diagnostic pop
73 static int vg_console_enabled(void);
75 static inline int vg_get_button( const char *button
)
77 return vg_get_button_ptr( button
)->value
&& !vg_console_enabled();
80 static inline int vg_get_button_down( const char *button
)
82 struct button_binding
*bind
= vg_get_button_ptr( button
);
83 return bind
->value
& (bind
->value
^ bind
->prev
) && !vg_console_enabled();
86 static inline int vg_get_button_up( const char *button
)
88 struct button_binding
*bind
= vg_get_button_ptr( button
);
89 return bind
->prev
& (bind
->value
^ bind
->prev
) && !vg_console_enabled();
92 static inline int vg_get_button_state( const char *button
)
94 if( vg_get_button_down( button
) ) return 1;
95 if( vg_get_button_up( button
) ) return 3;
96 if( vg_get_button( button
) ) return 2;
100 static inline int key_is_keyboard( int const id
)
102 vg_static_assert( GLFW_MOUSE_BUTTON_LAST
< GLFW_KEY_SPACE
, "GLFW: Mouse has too many buttons" );
103 return id
> GLFW_MOUSE_BUTTON_LAST
;
106 // Mouse AND Keyboard get button press
107 int get_button_cross_device( int const id
)
109 if( key_is_keyboard( id
) )
111 return glfwGetKey( vg_window
, id
);
115 return glfwGetMouseButton( vg_window
, id
) == GLFW_PRESS
;
119 void vg_update_inputs(void)
121 // Update button inputs
122 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
124 struct button_binding
*binding
= vg_button_binds
+ i
;
125 binding
->prev
= binding
->value
;
127 if( vg_input_mode
== k_EInputMode_pc
)
129 binding
->value
= get_button_cross_device( binding
->bind
);
133 binding
->value
= vg_gamepad
.buttons
[ binding
->bind
];
137 // Update axis inputs
138 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
140 struct axis_binding
*binding
= vg_axis_binds
+ i
;
142 if( vg_input_mode
== k_EInputMode_pc
)
144 binding
->value
= get_button_cross_device( binding
->positive
);
145 binding
->value
-= get_button_cross_device( binding
->negative
);
149 binding
->value
= vg_gamepad
.axes
[ binding
->bind
];