From ae3f2e5cfb98277642634bd4dd8c42ed7ae6c132 Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Sun, 3 Mar 2024 13:19:58 +0100 Subject: [PATCH] Better input --- engine/input.cpp | 5 ----- engine/input.h | 20 ++++++++++++++++---- engine/mates.h | 2 +- engine/scene_manager.h | 2 +- engine/singleinstance.h | 2 +- src/input_conf.cpp | 31 ++++++++++++++++++------------- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/engine/input.cpp b/engine/input.cpp index a8bbce9f..1ee5af4a 100644 --- a/engine/input.cpp +++ b/engine/input.cpp @@ -4,11 +4,6 @@ #include //int player_to_joystick[Input::kMaxPlayers] = { nullptr }; - -KeyStates Input::action_states[Input::kMaxPlayers][magic_enum::enum_count()] = { { RELEASED } }; -float Input::action_times[Input::kMaxPlayers][magic_enum::enum_count()] = { { 0 } }; -vec Input::analog_states[Input::kMaxPlayers][magic_enum::enum_count()]; - bool ignoreInput = false; void Input::IgnoreInput(bool enable) { diff --git a/engine/input.h b/engine/input.h index e7a558d8..916a5c7b 100644 --- a/engine/input.h +++ b/engine/input.h @@ -1,6 +1,9 @@ #pragma once #include +#include + +#include "magic_enum.h" #include "raw_input.h" #include "../src/input_conf.h" @@ -8,13 +11,15 @@ // Action-based input struct Input { - static const int kMaxPlayers = 2; + static const int kMaxPlayers; static vec GetAnalog(int player, AnalogInput k) { + SDL_assert(player < kMaxPlayers); return analog_states[player][int(k)]; } static bool IsPressed(int player, GameKeys k) { + SDL_assert(player < kMaxPlayers); return (action_states[player][int(k)] == PRESSED || action_states[player][int(k)] == JUST_PRESSED); } @@ -33,37 +38,44 @@ struct Input { } static bool IsReleased(int player, GameKeys k) { + SDL_assert(player < kMaxPlayers); return (action_states[player][int(k)] == RELEASED || action_states[player][int(k)] == JUST_RELEASED); } // True for one frame after the action is pressed static bool IsJustPressed(int player, GameKeys k) { + SDL_assert(player < kMaxPlayers); return (action_states[player][int(k)] == JUST_PRESSED); } // True for `interval` time after the key is pressed static bool IsJustPressed(int player, GameKeys k, float interval) { + SDL_assert(player < kMaxPlayers); return action_states[player][int(k)] == JUST_PRESSED || (action_states[player][int(k)] == PRESSED && action_times[player][int(k)] < interval); } // True for one frame after the action is pressed static bool IsJustReleased(int player, GameKeys k) { + SDL_assert(player < kMaxPlayers); return (action_states[player][int(k)] == JUST_RELEASED); } // True for `interval` time after the key is pressed static bool IsJustReleased(int player, GameKeys k, float interval) { + SDL_assert(player < kMaxPlayers); return action_states[player][int(k)] == JUST_RELEASED || (action_states[player][int(k)] == RELEASED && action_times[player][int(k)] < interval); } // Consume a just pressed event so the next call to IsJustPressed for that action returns false static void ConsumeJustPressed(int player, GameKeys k) { + SDL_assert(player < kMaxPlayers); action_states[player][int(k)] = PRESSED; action_times[player][int(k)] += 1000.f; } // Consume a just released event so the next call to IsJustReleased for that action returns false static void ConsumeJustReleased(int player, GameKeys k) { + SDL_assert(player < kMaxPlayers); action_states[player][int(k)] = RELEASED; action_times[player][int(k)] += 1000.f; } @@ -77,9 +89,9 @@ struct Input { private: static void MapGameKeys(); static std::function action_mapping[magic_enum::enum_count()]; - static KeyStates action_states[Input::kMaxPlayers][magic_enum::enum_count()]; - static float action_times[Input::kMaxPlayers][magic_enum::enum_count()]; static std::function analog_mapping[magic_enum::enum_count()]; - static vec analog_states[Input::kMaxPlayers][magic_enum::enum_count()]; + static KeyStates action_states[][magic_enum::enum_count()]; + static float action_times[][magic_enum::enum_count()]; + static vec analog_states[][magic_enum::enum_count()]; }; diff --git a/engine/mates.h b/engine/mates.h index 9550ede1..0003fd04 100644 --- a/engine/mates.h +++ b/engine/mates.h @@ -8,7 +8,7 @@ #include #include #include -#include "SDL_assert.h" +#include namespace Mates { diff --git a/engine/scene_manager.h b/engine/scene_manager.h index 4b6087b0..a2debfa5 100644 --- a/engine/scene_manager.h +++ b/engine/scene_manager.h @@ -1,7 +1,7 @@ #pragma once #include "scene.h" -#include "SDL_assert.h" +#include struct SceneManager { diff --git a/engine/singleinstance.h b/engine/singleinstance.h index bddf4b7b..33cbe3a9 100644 --- a/engine/singleinstance.h +++ b/engine/singleinstance.h @@ -1,6 +1,6 @@ #pragma once -#include "SDL_assert.h" +#include template class SingleInstance diff --git a/src/input_conf.cpp b/src/input_conf.cpp index 2b723a70..8e92219c 100644 --- a/src/input_conf.cpp +++ b/src/input_conf.cpp @@ -3,16 +3,19 @@ #include "input.h" #include "player.h" -#include "magic_enum.h" #include +const int Input::kMaxPlayers = 2; + static int keyboard_player_id = 0; // Keyboard controls player one +static bool aimingWithMouse = true; +static vec lastAnalogAim; std::function Input::action_mapping[magic_enum::enum_count()]; std::function Input::analog_mapping[magic_enum::enum_count()]; - -static bool aimingWithMouse = true; -static vec lastAnalogAim; +KeyStates Input::action_states[Input::kMaxPlayers][magic_enum::enum_count()] = { { RELEASED } }; +float Input::action_times[Input::kMaxPlayers][magic_enum::enum_count()] = { { 0 } }; +vec Input::analog_states[Input::kMaxPlayers][magic_enum::enum_count()]; void Input::MapGameKeys() { @@ -135,7 +138,6 @@ void Input::MapGameKeys() }; - // Analog analog_mapping[(int)AnalogInput::MOVE] = [](int p) @@ -165,18 +167,21 @@ void Input::MapGameKeys() analog_mapping[(int)AnalogInput::AIM] = [](int p) { vec joystick = GamePad::AnalogStick::Right.get(p); - if (joystick != vec::Zero) { - aimingWithMouse = false; - } - if (Mouse::IsPressed()) { - aimingWithMouse = true; - } - if (p == keyboard_player_id && aimingWithMouse) { - return Mouse::GetPositionInWorld(); + if (p == keyboard_player_id) { + if (joystick != vec::Zero) { + aimingWithMouse = false; + } + if (Mouse::IsPressed()) { + aimingWithMouse = true; + } + if (aimingWithMouse) { + return Mouse::GetPositionInWorld(); + } } if (joystick != vec::Zero) { lastAnalogAim = joystick; } + // For 2 players, lastAnalogAim needs to be turned into an array. return Player::instance()->pos + lastAnalogAim; }; }