1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-03 17:19:46 +02:00

Extract game input state handling from IngameState

Prevents input getting "stuck" when input is removed in the pause menu
This commit is contained in:
Daniel Evans 2016-11-16 23:34:28 +00:00
parent 40f18dabb0
commit f0e0e6e747
6 changed files with 96 additions and 81 deletions

View File

@ -22,6 +22,9 @@ set(RWGAME_SOURCES
MenuSystem.hpp
MenuSystem.cpp
GameInput.hpp
GameInput.cpp
states/LoadingState.hpp
states/LoadingState.cpp
states/IngameState.hpp

80
rwgame/GameInput.cpp Normal file
View File

@ -0,0 +1,80 @@
#include "GameInput.hpp"
#include <unordered_map>
// Hardcoded Controls SDLK_* -> GameInputState::Control
const std::unordered_multimap<int, GameInputState::Control> kDefaultControls = {
/* On Foot */
{SDLK_LCTRL, GameInputState::FireWeapon},
{SDLK_KP_0, GameInputState::FireWeapon},
{SDLK_KP_ENTER, GameInputState::NextWeapon},
{SDLK_KP_PERIOD, GameInputState::LastWeapon},
{SDLK_w, GameInputState::GoForward},
{SDLK_UP, GameInputState::GoForward},
{SDLK_s, GameInputState::GoBackwards},
{SDLK_DOWN, GameInputState::GoBackwards},
{SDLK_a, GameInputState::GoLeft},
{SDLK_LEFT, GameInputState::GoLeft},
{SDLK_d, GameInputState::GoRight},
{SDLK_RIGHT, GameInputState::GoRight},
{SDLK_PAGEUP, GameInputState::ZoomIn},
{SDLK_z, GameInputState::ZoomIn},
{SDLK_PAGEDOWN, GameInputState::ZoomOut},
{SDLK_x, GameInputState::ZoomOut},
{SDLK_f, GameInputState::EnterExitVehicle},
{SDLK_RETURN, GameInputState::EnterExitVehicle},
{SDLK_c, GameInputState::ChangeCamera},
{SDLK_HOME, GameInputState::ChangeCamera},
{SDLK_RCTRL, GameInputState::Jump},
{SDLK_SPACE, GameInputState::Jump},
{SDLK_LSHIFT, GameInputState::Sprint},
{SDLK_RSHIFT, GameInputState::Sprint},
{SDLK_LALT, GameInputState::Walk},
{SDLK_DELETE, GameInputState::AimWeapon},
{SDLK_CAPSLOCK, GameInputState::LookBehind},
/* In Vehicle */
{SDLK_LCTRL, GameInputState::VehicleFireWeapon},
{SDLK_a, GameInputState::VehicleLeft},
{SDLK_LEFT, GameInputState::VehicleLeft},
{SDLK_d, GameInputState::VehicleRight},
{SDLK_RIGHT, GameInputState::VehicleRight},
{SDLK_w, GameInputState::VehicleAccelerate},
{SDLK_UP, GameInputState::VehicleAccelerate},
{SDLK_d, GameInputState::VehicleBrake},
{SDLK_DOWN, GameInputState::VehicleBrake},
{SDLK_INSERT, GameInputState::ChangeRadio},
{SDLK_r, GameInputState::ChangeRadio},
{SDLK_LSHIFT, GameInputState::Horn},
{SDLK_RSHIFT, GameInputState::Horn},
{SDLK_KP_PLUS, GameInputState::Submission},
{SDLK_CAPSLOCK, GameInputState::Submission},
{SDLK_RCTRL, GameInputState::Handbrake},
{SDLK_SPACE, GameInputState::Handbrake},
{SDLK_KP_9, GameInputState::VehicleAimUp},
{SDLK_KP_2, GameInputState::VehicleAimDown},
{SDLK_KP_4, GameInputState::VehicleAimLeft},
{SDLK_KP_6, GameInputState::VehicleAimRight},
{SDLK_KP_9, GameInputState::VehicleDown},
{SDLK_KP_2, GameInputState::VehicleUp},
{SDLK_KP_1, GameInputState::LookLeft},
{SDLK_q, GameInputState::LookLeft},
{SDLK_KP_2, GameInputState::LookRight},
{SDLK_e, GameInputState::LookRight},
};
void GameInput::updateGameInputState(GameInputState *state,
const SDL_Event &event) {
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP: {
auto sym = event.key.keysym.sym;
auto level = event.type == SDL_KEYDOWN ? 1.f : 0.f;
auto& levels = state->levels;
auto range = kDefaultControls.equal_range(sym);
for (auto it = range.first; it != range.second; ++it) {
levels[it->second] = level;
}
} break;
}
}

10
rwgame/GameInput.hpp Normal file
View File

@ -0,0 +1,10 @@
#ifndef RWGAME_GAMEINPUT_HPP
#define RWGAME_GAMEINPUT_HPP
#include "engine/GameState.hpp"
#include "SDL2/SDL.h"
namespace GameInput {
void updateGameInputState(GameInputState* state, const SDL_Event& event);
}
#endif

View File

@ -1,5 +1,6 @@
#include "RWGame.hpp"
#include "DrawUI.hpp"
#include "GameInput.hpp"
#include "State.hpp"
#include "states/BenchmarkState.hpp"
#include "states/IngameState.hpp"
@ -394,6 +395,8 @@ int RWGame::run() {
break;
}
GameInput::updateGameInputState(&getState()->input[0], event);
RW_PROFILE_BEGIN("State");
state->handleEvent(event);
RW_PROFILE_END()

View File

@ -17,7 +17,6 @@
#include <script/ScriptMachine.hpp>
#include <glm/gtc/constants.hpp>
#include <unordered_map>
constexpr float kAutoLookTime = 2.f;
constexpr float kAutolookMinVelocity = 0.2f;
@ -26,67 +25,6 @@ const float kCameraPitchLimit = glm::quarter_pi<float>() * 0.5f;
const float kVehicleCameraPitch =
glm::half_pi<float>() - glm::quarter_pi<float>() * 0.25f;
// Hardcoded Controls SDLK_* -> GameInputState::Control
const std::unordered_multimap<int, GameInputState::Control> kDefaultControls = {
/* On Foot */
{SDLK_LCTRL, GameInputState::FireWeapon},
{SDLK_KP_0, GameInputState::FireWeapon},
{SDLK_KP_ENTER, GameInputState::NextWeapon},
{SDLK_KP_PERIOD, GameInputState::LastWeapon},
{SDLK_w, GameInputState::GoForward},
{SDLK_UP, GameInputState::GoForward},
{SDLK_s, GameInputState::GoBackwards},
{SDLK_DOWN, GameInputState::GoBackwards},
{SDLK_a, GameInputState::GoLeft},
{SDLK_LEFT, GameInputState::GoLeft},
{SDLK_d, GameInputState::GoRight},
{SDLK_RIGHT, GameInputState::GoRight},
{SDLK_PAGEUP, GameInputState::ZoomIn},
{SDLK_z, GameInputState::ZoomIn},
{SDLK_PAGEDOWN, GameInputState::ZoomOut},
{SDLK_x, GameInputState::ZoomOut},
{SDLK_f, GameInputState::EnterExitVehicle},
{SDLK_RETURN, GameInputState::EnterExitVehicle},
{SDLK_c, GameInputState::ChangeCamera},
{SDLK_HOME, GameInputState::ChangeCamera},
{SDLK_RCTRL, GameInputState::Jump},
{SDLK_SPACE, GameInputState::Jump},
{SDLK_LSHIFT, GameInputState::Sprint},
{SDLK_RSHIFT, GameInputState::Sprint},
{SDLK_LALT, GameInputState::Walk},
{SDLK_DELETE, GameInputState::AimWeapon},
{SDLK_CAPSLOCK, GameInputState::LookBehind},
/* In Vehicle */
{SDLK_LCTRL, GameInputState::VehicleFireWeapon},
{SDLK_a, GameInputState::VehicleLeft},
{SDLK_LEFT, GameInputState::VehicleLeft},
{SDLK_d, GameInputState::VehicleRight},
{SDLK_RIGHT, GameInputState::VehicleRight},
{SDLK_w, GameInputState::VehicleAccelerate},
{SDLK_UP, GameInputState::VehicleAccelerate},
{SDLK_d, GameInputState::VehicleBrake},
{SDLK_DOWN, GameInputState::VehicleBrake},
{SDLK_INSERT, GameInputState::ChangeRadio},
{SDLK_r, GameInputState::ChangeRadio},
{SDLK_LSHIFT, GameInputState::Horn},
{SDLK_RSHIFT, GameInputState::Horn},
{SDLK_KP_PLUS, GameInputState::Submission},
{SDLK_CAPSLOCK, GameInputState::Submission},
{SDLK_RCTRL, GameInputState::Handbrake},
{SDLK_SPACE, GameInputState::Handbrake},
{SDLK_KP_9, GameInputState::VehicleAimUp},
{SDLK_KP_2, GameInputState::VehicleAimDown},
{SDLK_KP_4, GameInputState::VehicleAimLeft},
{SDLK_KP_6, GameInputState::VehicleAimRight},
{SDLK_KP_9, GameInputState::VehicleDown},
{SDLK_KP_2, GameInputState::VehicleUp},
{SDLK_KP_1, GameInputState::LookLeft},
{SDLK_q, GameInputState::LookLeft},
{SDLK_KP_2, GameInputState::LookRight},
{SDLK_e, GameInputState::LookRight},
};
IngameState::IngameState(RWGame* game, bool newgame, const std::string& save)
: State(game)
, started(false)
@ -445,8 +383,6 @@ void IngameState::handleEvent(const SDL_Event& event) {
break;
}
updateInputState(event);
if (player && player->isInputEnabled()) {
handlePlayerInput(event);
}
@ -499,22 +435,6 @@ void IngameState::handlePlayerInput(const SDL_Event& event) {
}
}
void IngameState::updateInputState(const SDL_Event& event) {
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP: {
auto sym = event.key.keysym.sym;
auto level = event.type == SDL_KEYDOWN ? 1.f : 0.f;
auto& levels = getWorld()->state->input[0].levels;
auto range = kDefaultControls.equal_range(sym);
for (auto it = range.first; it != range.second; ++it) {
levels[it->second] = level;
}
} break;
}
}
bool IngameState::shouldWorldUpdate() {
return true;
}

View File

@ -53,7 +53,6 @@ public:
virtual void handleEvent(const SDL_Event& event);
virtual void handlePlayerInput(const SDL_Event& event);
void updateInputState(const SDL_Event& event);
virtual bool shouldWorldUpdate();