1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
3 static inline float vg_get_axis( const char *axis
);
4 static inline int vg_get_button( const char *button
);
5 static inline int vg_get_button_down( const char *button
);
6 static inline int vg_get_button_up( const char *button
);
10 k_button_state_down
= 1,
11 k_button_state_up
= 3,
12 k_button_state_pressed
= 2,
13 k_button_state_none
= 0
16 GLFWgamepadstate vg_gamepad
;
17 int vg_gamepad_ready
= 0;
18 const char *vg_gamepad_name
= NULL
;
21 /* TODO: Fix this... */
29 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
,
111 "GLFW: Mouse has too many buttons" );
112 return id
> GLFW_MOUSE_BUTTON_LAST
;
115 int get_button_cross_device( int const id
)
117 if( key_is_keyboard( id
) )
118 return glfwGetKey( vg_window
, id
);
120 return glfwGetMouseButton( vg_window
, id
) == GLFW_PRESS
;
123 void vg_update_inputs(void)
125 if( !glfwGetGamepadState( GLFW_JOYSTICK_1
, &vg_gamepad
) )
127 vg_gamepad_ready
= 0;
131 /* Update button inputs */
132 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
134 struct button_binding
*binding
= vg_button_binds
+ i
;
135 binding
->prev
= binding
->value
;
137 if( vg_input_mode
== k_EInputMode_pc
)
139 binding
->value
= get_button_cross_device( binding
->bind
);
143 binding
->value
= vg_gamepad
.buttons
[ binding
->bind
];
147 /* Update axis inputs */
148 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
150 struct axis_binding
*binding
= vg_axis_binds
+ i
;
151 binding
->value
= vg_gamepad
.axes
[ binding
->bind
];
155 static void vg_gamepad_init(void)
157 for( int id
= 0; id
<= GLFW_JOYSTICK_LAST
; id
++ )
159 if( glfwJoystickPresent( id
) )
161 vg_info( "Joystick found: '%s'\n", glfwGetJoystickName(id
) );
164 if( glfwJoystickIsGamepad( id
) )
166 vg_gamepad_name
= glfwGetGamepadName( id
);
167 vg_success( "Gamepad with mapping registered: %s\n", vg_gamepad_name
);
169 vg_gamepad_ready
= 1;