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 inline int vg_get_button( const char *button
)
75 return vg_get_button_ptr( button
)->value
;
78 static inline int vg_get_button_down( const char *button
)
80 struct button_binding
*bind
= vg_get_button_ptr( button
);
81 return bind
->value
& (bind
->value
^ bind
->prev
);
84 static inline int vg_get_button_up( const char *button
)
86 struct button_binding
*bind
= vg_get_button_ptr( button
);
87 return bind
->prev
& (bind
->value
^ bind
->prev
);
90 static inline int key_is_keyboard( int const id
)
92 vg_static_assert( GLFW_MOUSE_BUTTON_LAST
< GLFW_KEY_SPACE
, "GLFW: Mouse has too many buttons" );
93 return id
> GLFW_MOUSE_BUTTON_LAST
;
96 // Mouse AND Keyboard get button press
97 int get_button_cross_device( int const id
)
99 if( key_is_keyboard( id
) )
101 return glfwGetKey( vg_window
, id
);
105 return glfwGetMouseButton( vg_window
, id
) == GLFW_PRESS
;
109 void vg_update_inputs(void)
111 // Update button inputs
112 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
114 struct button_binding
*binding
= vg_button_binds
+ i
;
115 binding
->prev
= binding
->value
;
117 if( vg_input_mode
== k_EInputMode_pc
)
119 binding
->value
= get_button_cross_device( binding
->bind
);
123 binding
->value
= vg_gamepad
.buttons
[ binding
->bind
];
127 // Update axis inputs
128 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
130 struct axis_binding
*binding
= vg_axis_binds
+ i
;
132 if( vg_input_mode
== k_EInputMode_pc
)
134 binding
->value
= get_button_cross_device( binding
->positive
);
135 binding
->value
-= get_button_cross_device( binding
->negative
);
139 binding
->value
= vg_gamepad
.axes
[ binding
->bind
];