From aca01507ed4c10e21844e009ae6be086e547c140 Mon Sep 17 00:00:00 2001 From: Sara Date: Fri, 12 Jan 2024 12:26:23 +0100 Subject: [PATCH] feat: added compositeaxis1d_from_keys and from_buttons This required moving InputDelegateFn from input.h to input_axis.h --- core/src/input.h | 2 -- core/src/input_axis.c | 14 ++++++++++++++ core/src/input_axis.h | 5 +++++ game/src/Player.c | 12 ++---------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/core/src/input.h b/core/src/input.h index fd4ed4c..8d4b026 100644 --- a/core/src/input.h +++ b/core/src/input.h @@ -10,8 +10,6 @@ struct PlayerInput; -typedef void (*InputDelegateFn)(void* self, InputEvent event); - typedef struct InputListener { void* self; InputDelegateFn fn; diff --git a/core/src/input_axis.c b/core/src/input_axis.c index 1dbaae0..b299db4 100644 --- a/core/src/input_axis.c +++ b/core/src/input_axis.c @@ -225,3 +225,17 @@ void compositeaxis1d_drop(CompositeAxis1D* self) { self->right.drop->drop(self->right.data); 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); +} diff --git a/core/src/input_axis.h b/core/src/input_axis.h index fed369c..6665e07 100644 --- a/core/src/input_axis.h +++ b/core/src/input_axis.h @@ -39,6 +39,8 @@ typedef struct { IDrop const* drop; } InputAxis; +typedef void (*InputDelegateFn)(void* self, InputEvent event); + #define impl_InputAxis_for(T, is_changed_by_f, evaluate_f, set_device_f)\ InputAxis T##_as_InputAxis(T* x) {\ 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_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(Drop, CompositeAxis1D) diff --git a/game/src/Player.c b/game/src/Player.c index cc16526..40b6599 100644 --- a/game/src/Player.c +++ b/game/src/Player.c @@ -38,16 +38,8 @@ impl_PhysicsEntity_for(Player, static inline void Internal_PlayerInitInput(Player* self) { - playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_new( - KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_A)), - 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, CompositeAxis1D_as_InputAxis(compositeaxis1d_from_keys(SDL_SCANCODE_A, SDL_SCANCODE_D)), (InputDelegateFn)PlayerHorizontalInput); + playerinput_add(self->playerInput, CompositeAxis1D_as_InputAxis(compositeaxis1d_from_keys(SDL_SCANCODE_S, SDL_SCANCODE_W)), (InputDelegateFn)PlayerVerticalInput); playerinput_add(self->playerInput, KeyBind_as_InputAxis(keybind_new(SDL_SCANCODE_J)), (InputDelegateFn)PlayerAttackInput); }