From: Daniel Karbach Date: Mon, 6 Aug 2012 13:34:05 +0000 (+0200) Subject: added Input class for handling user input X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=867fd5d9b79c3b9c1d0fb17ba9f55cfe971b93d5;hp=2955f56fb11fab871a34db5700befe55737d81d7;p=l2e.git added Input class for handling user input also changed application states to utilize the new Input class rather than handling events themselves and mapped all keys in main() for testing --- diff --git a/Debug/makefile b/Debug/makefile index 48bc75f..507437a 100644 --- a/Debug/makefile +++ b/Debug/makefile @@ -10,7 +10,6 @@ RM := rm -rf -include sources.mk -include src/sdl/subdir.mk -include src/graphics/subdir.mk --include src/geometry/subdir.mk -include src/battle/subdir.mk -include src/app/subdir.mk -include src/subdir.mk diff --git a/Debug/sources.mk b/Debug/sources.mk index 732350c..0fdadd3 100644 --- a/Debug/sources.mk +++ b/Debug/sources.mk @@ -28,5 +28,4 @@ src \ src/graphics \ src/battle \ src/app \ -src/geometry \ diff --git a/Debug/src/app/subdir.mk b/Debug/src/app/subdir.mk index 76160a8..ae7ec03 100644 --- a/Debug/src/app/subdir.mk +++ b/Debug/src/app/subdir.mk @@ -4,13 +4,16 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ -../src/app/Application.cpp +../src/app/Application.cpp \ +../src/app/Input.cpp OBJS += \ -./src/app/Application.o +./src/app/Application.o \ +./src/app/Input.o CPP_DEPS += \ -./src/app/Application.d +./src/app/Application.d \ +./src/app/Input.d # Each subdirectory must supply rules for building sources it contributes diff --git a/Release/makefile b/Release/makefile index 48bc75f..507437a 100644 --- a/Release/makefile +++ b/Release/makefile @@ -10,7 +10,6 @@ RM := rm -rf -include sources.mk -include src/sdl/subdir.mk -include src/graphics/subdir.mk --include src/geometry/subdir.mk -include src/battle/subdir.mk -include src/app/subdir.mk -include src/subdir.mk diff --git a/Release/sources.mk b/Release/sources.mk index 732350c..0fdadd3 100644 --- a/Release/sources.mk +++ b/Release/sources.mk @@ -28,5 +28,4 @@ src \ src/graphics \ src/battle \ src/app \ -src/geometry \ diff --git a/Release/src/app/subdir.mk b/Release/src/app/subdir.mk index 555eb00..e0bc8c4 100644 --- a/Release/src/app/subdir.mk +++ b/Release/src/app/subdir.mk @@ -4,13 +4,16 @@ # Add inputs and outputs from these tool invocations to the build variables CPP_SRCS += \ -../src/app/Application.cpp +../src/app/Application.cpp \ +../src/app/Input.cpp OBJS += \ -./src/app/Application.o +./src/app/Application.o \ +./src/app/Input.o CPP_DEPS += \ -./src/app/Application.d +./src/app/Application.d \ +./src/app/Input.d # Each subdirectory must supply rules for building sources it contributes diff --git a/src/app/Application.cpp b/src/app/Application.cpp index e06e231..d3b00a1 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -88,6 +88,7 @@ void Application::Loop(void) { void Application::HandleEvents(void) { if (!CurrentState()) return; + input.ResetInteractiveState(); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { @@ -98,11 +99,16 @@ void Application::HandleEvents(void) { screen->Resize(event.resize.w, event.resize.h); CurrentState()->Resize(event.resize.w, event.resize.h); break; + case SDL_KEYDOWN: + case SDL_KEYUP: + input.HandleKeyboardEvent(event.key); + break; default: - CurrentState()->HandleEvent(event); + // skip event break; } } + CurrentState()->HandleInput(input); } void Application::UpdateWorld(Uint32 deltaT) { diff --git a/src/app/Application.h b/src/app/Application.h index 909942b..f70dd69 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -8,6 +8,7 @@ #ifndef APP_APPLICATION_H_ #define APP_APPLICATION_H_ +#include "Input.h" #include "../sdl/InitScreen.h" #include @@ -36,6 +37,8 @@ public: void PushState(State *); void PopState(void); void Quit(void); + Input &Buttons() { return input; } + const Input &Buttons() const { return input; } private: State *CurrentState(void); @@ -51,6 +54,7 @@ private: private: sdl::InitScreen *screen; std::stack states; + Input input; Uint32 last; }; diff --git a/src/app/Input.cpp b/src/app/Input.cpp new file mode 100644 index 0000000..e810e00 --- /dev/null +++ b/src/app/Input.cpp @@ -0,0 +1,36 @@ +/* + * Input.cpp + * + * Created on: Aug 6, 2012 + * Author: holy + */ + +#include "Input.h" + +using std::map; + +namespace app { + +Input::Input() +: down(0) +, pressed(0) +, released(0) { + +} + + +void Input::HandleKeyboardEvent(const SDL_KeyboardEvent &e) { + map::const_iterator key(mapping.find(e.keysym.sym)); + if (key == mapping.end()) return; + + Button button(key->second); + if (e.state == SDL_PRESSED) { + down |= button; + pressed |= button; + } else { + down &= ~button; + released |= button; + } +} + +} diff --git a/src/app/Input.h b/src/app/Input.h new file mode 100644 index 0000000..ad50869 --- /dev/null +++ b/src/app/Input.h @@ -0,0 +1,70 @@ +/* + * Input.h + * + * Created on: Aug 6, 2012 + * Author: holy + */ + +#ifndef APP_INPUT_H_ +#define APP_INPUT_H_ + +#include +#include + +namespace app { + +class Input { + +public: + enum Button { + PAD_UP = 1, + PAD_RIGHT = 2, + PAD_DOWN = 4, + PAD_LEFT = 8, + ACTION_A = 16, + ACTION_B = 32, + ACTION_X = 64, + ACTION_Y = 128, + START = 256, + SELECT = 512, + SHOULDER_RIGHT = 1024, + SHOULDER_LEFT = 2048 + }; + +public: + Input(); + +public: + bool IsDown(Button b) const { + return down & b; + } + bool JustPressed(Button b) const { + return pressed & b; + } + bool JustReleased(Button b) const { + return released & b; + } + +public: + void ResetInteractiveState() { + pressed = 0; + released = 0; + } + void HandleKeyboardEvent(const SDL_KeyboardEvent &); + +public: + void MapKey(SDLKey k, Button b) { + mapping[k] = b; + } + +private: + std::map mapping; + Uint16 down; + Uint16 pressed; + Uint16 released; + +}; + +} + +#endif /* APP_INPUT_H_ */ diff --git a/src/app/State.h b/src/app/State.h index efee32b..7f46c57 100644 --- a/src/app/State.h +++ b/src/app/State.h @@ -13,6 +13,7 @@ namespace app { class Application; +class Input; class State { @@ -28,7 +29,7 @@ public: /// adapt the state's graphics to given dimensions virtual void Resize(int width, int height) = 0; - virtual void HandleEvent(const SDL_Event &) = 0; + virtual void HandleInput(const Input &) = 0; virtual void UpdateWorld(float deltaT) = 0; virtual void Render(SDL_Surface *) = 0; diff --git a/src/battle/BattleState.cpp b/src/battle/BattleState.cpp index c31786e..9f1acac 100644 --- a/src/battle/BattleState.cpp +++ b/src/battle/BattleState.cpp @@ -8,12 +8,15 @@ #include "BattleState.h" #include "PartyLayout.h" +#include "../app/Application.h" +#include "../app/Input.h" #include "../geometry/operators.h" #include "../graphics/Sprite.h" #include using app::Application; +using app::Input; using geometry::Point; using geometry::Vector; @@ -51,7 +54,7 @@ void BattleState::ExitState() { } -void BattleState::HandleEvent(const SDL_Event &) { +void BattleState::HandleInput(const Input &input) { } diff --git a/src/battle/BattleState.h b/src/battle/BattleState.h index 67e0b78..397e5e3 100644 --- a/src/battle/BattleState.h +++ b/src/battle/BattleState.h @@ -17,6 +17,8 @@ #include #include +namespace app { class Input; } + namespace battle { class PartyLayout; @@ -40,7 +42,7 @@ public: virtual void Resize(int width, int height); - virtual void HandleEvent(const SDL_Event &); + virtual void HandleInput(const app::Input &); virtual void UpdateWorld(float deltaT); virtual void Render(SDL_Surface *); diff --git a/src/main.cpp b/src/main.cpp index 3ec9945..6bce28f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ */ #include "app/Application.h" +#include "app/Input.h" #include "battle/BattleState.h" #include "battle/Hero.h" #include "battle/Monster.h" @@ -19,6 +20,7 @@ #include using app::Application; +using app::Input; using battle::BattleState; using battle::Hero; using battle::Monster; @@ -78,6 +80,18 @@ int main(int argc, char **argv) { battleState->AddHero(hero); battleState->AddHero(hero); Application app(&screen, battleState); + app.Buttons().MapKey(SDLK_w, Input::PAD_UP); + app.Buttons().MapKey(SDLK_d, Input::PAD_RIGHT); + app.Buttons().MapKey(SDLK_s, Input::PAD_DOWN); + app.Buttons().MapKey(SDLK_a, Input::PAD_LEFT); + app.Buttons().MapKey(SDLK_RIGHT, Input::ACTION_A); + app.Buttons().MapKey(SDLK_DOWN, Input::ACTION_B); + app.Buttons().MapKey(SDLK_UP, Input::ACTION_X); + app.Buttons().MapKey(SDLK_LEFT, Input::ACTION_Y); + app.Buttons().MapKey(SDLK_RETURN, Input::START); + app.Buttons().MapKey(SDLK_SPACE, Input::SELECT); + app.Buttons().MapKey(SDLK_RSHIFT, Input::SHOULDER_RIGHT); + app.Buttons().MapKey(SDLK_LSHIFT, Input::SHOULDER_LEFT); app.Run(); return 0;