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
));
10 k_button_state_down
= 1,
11 k_button_state_up
= 3,
12 k_button_state_pressed
= 2,
13 k_button_state_none
= 0
17 // ===========================================================================================================
18 GLFWgamepadstate vg_gamepad
;
19 int vg_gamepad_ready
= 0;
20 const char *vg_gamepad_name
= NULL
;
30 static struct axis_binding
44 static struct button_binding
53 #include "vg_config.h"
55 #pragma GCC diagnostic push
56 #pragma GCC diagnostic ignored "-Wreturn-type"
58 static inline float vg_get_axis( const char *axis
)
60 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
62 if( !strcmp( axis
, vg_axis_binds
[i
].name
) )
64 return vg_axis_binds
[i
].value
;
69 static inline struct button_binding
*vg_get_button_ptr( const char *button
)
71 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
73 if( !strcmp( button
, vg_button_binds
[i
].name
) )
75 return vg_button_binds
+ i
;
79 #pragma GCC diagnostic pop
81 static int vg_console_enabled(void);
83 static inline int vg_get_button( const char *button
)
85 return vg_get_button_ptr( button
)->value
&& !vg_console_enabled();
88 static inline int vg_get_button_down( const char *button
)
90 struct button_binding
*bind
= vg_get_button_ptr( button
);
91 return bind
->value
& (bind
->value
^ bind
->prev
) && !vg_console_enabled();
94 static inline int vg_get_button_up( const char *button
)
96 struct button_binding
*bind
= vg_get_button_ptr( button
);
97 return bind
->prev
& (bind
->value
^ bind
->prev
) && !vg_console_enabled();
100 static inline enum vg_button_state
vg_get_button_state( const char *button
)
102 if( vg_get_button_down( button
) ) return k_button_state_down
;
103 if( vg_get_button_up( button
) ) return k_button_state_up
;
104 if( vg_get_button( button
) ) return k_button_state_pressed
;
105 return k_button_state_none
;
108 static inline int key_is_keyboard( int const id
)
110 vg_static_assert( GLFW_MOUSE_BUTTON_LAST
< GLFW_KEY_SPACE
, "GLFW: Mouse has too many buttons" );
111 return id
> GLFW_MOUSE_BUTTON_LAST
;
114 // Mouse AND Keyboard get button press
115 int get_button_cross_device( int const id
)
117 if( key_is_keyboard( id
) )
119 return glfwGetKey( vg_window
, id
);
123 return glfwGetMouseButton( vg_window
, id
) == GLFW_PRESS
;
127 void vg_update_inputs(void)
129 // Update button inputs
130 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
132 struct button_binding
*binding
= vg_button_binds
+ i
;
133 binding
->prev
= binding
->value
;
135 if( vg_input_mode
== k_EInputMode_pc
)
137 binding
->value
= get_button_cross_device( binding
->bind
);
141 binding
->value
= vg_gamepad
.buttons
[ binding
->bind
];
145 // Update axis inputs
146 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
148 struct axis_binding
*binding
= vg_axis_binds
+ i
;
150 if( vg_input_mode
== k_EInputMode_pc
)
152 binding
->value
= get_button_cross_device( binding
->positive
);
153 binding
->value
-= get_button_cross_device( binding
->negative
);
157 binding
->value
= vg_gamepad
.axes
[ binding
->bind
];