feat: added compositeaxis1d_from_keys and from_buttons

This required moving InputDelegateFn from input.h to input_axis.h
This commit is contained in:
Sara 2024-01-12 12:26:23 +01:00
parent 1dc6f8352a
commit aca01507ed
4 changed files with 21 additions and 12 deletions

View file

@ -10,8 +10,6 @@
struct PlayerInput; struct PlayerInput;
typedef void (*InputDelegateFn)(void* self, InputEvent event);
typedef struct InputListener { typedef struct InputListener {
void* self; void* self;
InputDelegateFn fn; InputDelegateFn fn;

View file

@ -225,3 +225,17 @@ void compositeaxis1d_drop(CompositeAxis1D* self) {
self->right.drop->drop(self->right.data); self->right.drop->drop(self->right.data);
free(self); free(self);
} }
CompositeAxis1D* compositeaxis1d_from_keys(SDL_Scancode negative, SDL_Scancode positive) {
return compositeaxis1d_new(
KeyBind_as_InputAxis(keybind_new(negative)),
KeyBind_as_InputAxis(keybind_new(positive)),
InputEvent_Float);
}
CompositeAxis1D* compositeaxis1d_from_buttons(int negative, int positive) {
return compositeaxis1d_new(
ControllerButton_as_InputAxis(controllerbutton_new(negative)),
ControllerButton_as_InputAxis(controllerbutton_new(positive)),
InputEvent_Float);
}

View file

@ -39,6 +39,8 @@ typedef struct {
IDrop const* drop; IDrop const* drop;
} InputAxis; } InputAxis;
typedef void (*InputDelegateFn)(void* self, InputEvent event);
#define impl_InputAxis_for(T, is_changed_by_f, evaluate_f, set_device_f)\ #define impl_InputAxis_for(T, is_changed_by_f, evaluate_f, set_device_f)\
InputAxis T##_as_InputAxis(T* x) {\ InputAxis T##_as_InputAxis(T* x) {\
TC_FN_TYPECHECK(int, is_changed_by_f, T*, SDL_Event);\ TC_FN_TYPECHECK(int, is_changed_by_f, T*, SDL_Event);\
@ -105,6 +107,9 @@ extern struct InputEvent compositeaxis1d_evaluate(CompositeAxis1D* self, SDL_Eve
extern void compositeaxis1d_set_device(CompositeAxis1D* self, struct InputDevice* device); extern void compositeaxis1d_set_device(CompositeAxis1D* self, struct InputDevice* device);
extern void compositeaxis1d_drop(CompositeAxis1D* self); extern void compositeaxis1d_drop(CompositeAxis1D* self);
extern CompositeAxis1D* compositeaxis1d_from_keys(SDL_Scancode negative, SDL_Scancode positive);
extern CompositeAxis1D* compositeaxis1d_from_buttons(int negative, int positive);
decl_typeclass_impl(InputAxis, CompositeAxis1D) decl_typeclass_impl(InputAxis, CompositeAxis1D)
decl_typeclass_impl(Drop, CompositeAxis1D) decl_typeclass_impl(Drop, CompositeAxis1D)

View file

@ -38,16 +38,8 @@ impl_PhysicsEntity_for(Player,
static inline static inline
void Internal_PlayerInitInput(Player* self) { void Internal_PlayerInitInput(Player* self) {
playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_new( playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_from_keys(SDL_SCANCODE_A, SDL_SCANCODE_D)), (InputDelegateFn)PlayerHorizontalInput);
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_A)), playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_from_keys(SDL_SCANCODE_S, SDL_SCANCODE_W)), (InputDelegateFn)PlayerVerticalInput);
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_D)),
InputEvent_Float)), (InputDelegateFn)PlayerHorizontalInput);
playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_new(
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_S)),
KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_W)),
InputEvent_Float)), (InputDelegateFn)PlayerVerticalInput);
playerinput_add(self->playerInput, KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_J)), (InputDelegateFn)PlayerAttackInput); playerinput_add(self->playerInput, KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_J)), (InputDelegateFn)PlayerAttackInput);
} }