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
54 #include "vg_config.h"
56 #pragma GCC diagnostic push
57 #pragma GCC diagnostic ignored "-Wreturn-type"
59 static inline float vg_get_axis( const char *axis
)
61 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
63 if( !strcmp( axis
, vg_axis_binds
[i
].name
) )
65 return vg_axis_binds
[i
].value
;
70 static inline struct button_binding
*vg_get_button_ptr( const char *button
)
72 for( int i
=0; i
<vg_list_size(vg_button_binds
); i
++ )
74 if( !strcmp(button
,vg_button_binds
[i
].name
) )
76 return vg_button_binds
+ i
;
80 #pragma GCC diagnostic pop
82 static int vg_console_enabled(void);
84 static inline int vg_get_button( const char *button
)
86 return vg_get_button_ptr( button
)->value
&& !vg_console_enabled();
89 static inline int vg_get_button_down( const char *button
)
91 struct button_binding
*bind
= vg_get_button_ptr( button
);
92 return bind
->value
& (bind
->value
^ bind
->prev
) && !vg_console_enabled();
95 static inline int vg_get_button_up( const char *button
)
97 struct button_binding
*bind
= vg_get_button_ptr( button
);
98 return bind
->prev
& (bind
->value
^ bind
->prev
) && !vg_console_enabled();
101 static inline enum vg_button_state
vg_get_button_state( const char *button
)
103 if(vg_get_button_down( button
)) return k_button_state_down
;
104 if(vg_get_button_up( button
)) return k_button_state_up
;
105 if(vg_get_button( button
)) return k_button_state_pressed
;
106 return k_button_state_none
;
109 static inline int key_is_keyboard( int const id
)
111 vg_static_assert( GLFW_MOUSE_BUTTON_LAST
< GLFW_KEY_SPACE
,
112 "GLFW: Mouse has too many buttons" );
113 return id
> GLFW_MOUSE_BUTTON_LAST
;
116 int get_button_cross_device( int const id
)
118 if( key_is_keyboard( id
) )
119 return glfwGetKey( vg_window
, id
);
121 return glfwGetMouseButton( vg_window
, id
) == GLFW_PRESS
;
124 void vg_update_inputs(void)
126 if( !glfwGetGamepadState( GLFW_JOYSTICK_1
, &vg_gamepad
) )
128 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( binding
->controller
)
138 binding
->value
= vg_gamepad
.buttons
[ binding
->controller
];
140 binding
->value
= get_button_cross_device( binding
->bind
);
143 /* Update axis inputs */
144 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
146 struct axis_binding
*binding
= vg_axis_binds
+ i
;
147 binding
->value
= vg_gamepad
.axes
[ binding
->bind
];
151 static void vg_gamepad_init(void)
153 for( int id
= 0; id
<= GLFW_JOYSTICK_LAST
; id
++ )
155 if( glfwJoystickPresent( id
) )
157 vg_info( "Joystick found: '%s'\n", glfwGetJoystickName(id
) );
160 if( glfwJoystickIsGamepad( id
) )
162 vg_gamepad_name
= glfwGetGamepadName( id
);
163 vg_success( "Gamepad with mapping registered: %s\n", vg_gamepad_name
);
165 vg_gamepad_ready
= 1;