3985f3c26975dcc89acb5dcfeb1776e078a58898
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
6 #include "vg/vg_loader.h"
8 static inline float vg_get_axis( const char *axis
);
9 static inline int vg_get_button( const char *button
);
10 static inline int vg_get_button_down( const char *button
);
11 static inline int vg_get_button_up( const char *button
);
15 k_button_state_down
= 1,
16 k_button_state_up
= 3,
17 k_button_state_pressed
= 2,
18 k_button_state_none
= 0
21 /* TODO: Fix this... */
29 static struct axis_binding
44 static struct button_binding
52 vg_controller_binds
[];
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
++ )
62 if( !strcmp( axis
, vg_axis_binds
[i
].name
) )
63 return vg_axis_binds
[i
].value
;
66 static inline struct button_binding
*vg_get_button_ptr( const char *button
)
68 for( int i
=0; i
<vg_list_size(vg_button_binds
); i
++ )
69 if( !strcmp(button
,vg_button_binds
[i
].name
) )
70 return vg_button_binds
+ i
;
74 static inline struct button_binding
*vg_get_button_ptr_c( const char *button
)
76 for( int i
=0; i
<vg_list_size(vg_controller_binds
); i
++ )
77 if( !strcmp(button
,vg_controller_binds
[i
].name
) )
78 return vg_controller_binds
+ i
;
82 #pragma GCC diagnostic pop
84 static int vg_console_enabled(void);
86 static inline void vg_get_button_states( const char *name
, int *cur
, int *prev
)
88 struct button_binding
*bind
= vg_get_button_ptr( name
),
89 *bindc
= vg_get_button_ptr_c( name
);
101 *cur
|= bindc
->value
;
102 *prev
|= bindc
->prev
;
106 static inline int vg_get_button( const char *button
)
109 vg_get_button_states( button
, &cur
, &prev
);
111 return cur
&& !vg_console_enabled();
114 static inline int vg_get_button_down( const char *button
)
117 vg_get_button_states( button
, &cur
, &prev
);
119 return cur
& (cur
^ prev
) && !vg_console_enabled();
122 static inline int vg_get_button_up( const char *button
)
125 vg_get_button_states( button
, &cur
, &prev
);
127 return prev
& (cur
^ prev
) && !vg_console_enabled();
130 static inline enum vg_button_state
vg_get_button_state( const char *button
)
132 if(vg_get_button_down( button
)) return k_button_state_down
;
133 if(vg_get_button_up( button
)) return k_button_state_up
;
134 if(vg_get_button( button
)) return k_button_state_pressed
;
135 return k_button_state_none
;
138 static inline int key_is_keyboard( int const id
)
140 vg_static_assert( GLFW_MOUSE_BUTTON_LAST
< GLFW_KEY_SPACE
,
141 "GLFW: Mouse has too many buttons" );
142 return id
> GLFW_MOUSE_BUTTON_LAST
;
145 int get_button_cross_device( int const id
)
147 if( key_is_keyboard( id
) )
148 return glfwGetKey( vg
.window
, id
);
150 return glfwGetMouseButton( vg
.window
, id
) == GLFW_PRESS
;
153 void vg_update_inputs(void)
155 if( !glfwGetGamepadState( GLFW_JOYSTICK_1
, &vg
.gamepad
) )
156 vg
.gamepad_ready
= 0;
158 /* Update button inputs */
159 for( int i
= 0; i
< vg_list_size( vg_button_binds
); i
++ )
161 struct button_binding
*binding
= vg_button_binds
+ i
;
162 binding
->prev
= binding
->value
;
163 binding
->value
= get_button_cross_device( binding
->bind
);
166 for( int i
=0; i
<vg_list_size( vg_controller_binds
); i
++ )
168 struct button_binding
*binding
= vg_controller_binds
+ i
;
169 binding
->prev
= binding
->value
;
170 binding
->value
= vg
.gamepad
.buttons
[ binding
->bind
];
173 /* Update axis inputs */
174 for( int i
= 0; i
< vg_list_size( vg_axis_binds
); i
++ )
176 struct axis_binding
*binding
= vg_axis_binds
+ i
;
177 binding
->value
= vg
.gamepad
.axes
[ binding
->bind
];
181 static int vg_gamepad_init(void)
183 if( vg_acquire_thread_sync(1) )
185 for( int id
=0; id
<=GLFW_JOYSTICK_LAST
; id
++ )
187 if( glfwJoystickPresent( id
) )
189 vg_info( "Joystick found: '%s'\n", glfwGetJoystickName(id
) );
192 if( glfwJoystickIsGamepad( id
) )
194 vg
.gamepad_name
= glfwGetGamepadName( id
);
195 vg_success( "Gamepad mapping registered: %s\n", vg
.gamepad_name
);
197 vg
.gamepad_ready
= 1;
203 vg_release_thread_sync(1);