]> git.localhorst.tv Git - l2e.git/commitdiff
added Input class for handling user input
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 6 Aug 2012 13:34:05 +0000 (15:34 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 6 Aug 2012 13:34:05 +0000 (15:34 +0200)
also changed application states to utilize the new Input class rather than handling events themselves and mapped all keys in main() for testing

14 files changed:
Debug/makefile
Debug/sources.mk
Debug/src/app/subdir.mk
Release/makefile
Release/sources.mk
Release/src/app/subdir.mk
src/app/Application.cpp
src/app/Application.h
src/app/Input.cpp [new file with mode: 0644]
src/app/Input.h [new file with mode: 0644]
src/app/State.h
src/battle/BattleState.cpp
src/battle/BattleState.h
src/main.cpp

index 48bc75f7d9901d9207bba013f501cc9056091d19..507437a93cbfa29ba2e0136e4c244d637cf0e2b3 100644 (file)
@@ -10,7 +10,6 @@ RM := rm -rf
 -include sources.mk
 -include src/sdl/subdir.mk
 -include src/graphics/subdir.mk
 -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
 -include src/battle/subdir.mk
 -include src/app/subdir.mk
 -include src/subdir.mk
index 732350c3e648485ed2e1ecfb38b7193815f801ef..0fdadd38f9a63f83ae891efcde4019c97aab7708 100644 (file)
@@ -28,5 +28,4 @@ src \
 src/graphics \
 src/battle \
 src/app \
 src/graphics \
 src/battle \
 src/app \
-src/geometry \
 
 
index 76160a841f1ac6d441fe4dfb8b4e940d188750ae..ae7ec036b008c71e19612c3e03aa003d4ed9601a 100644 (file)
@@ -4,13 +4,16 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
 
 # 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 += \
 
 OBJS += \
-./src/app/Application.o 
+./src/app/Application.o \
+./src/app/Input.o 
 
 CPP_DEPS += \
 
 CPP_DEPS += \
-./src/app/Application.d 
+./src/app/Application.d \
+./src/app/Input.d 
 
 
 # Each subdirectory must supply rules for building sources it contributes
 
 
 # Each subdirectory must supply rules for building sources it contributes
index 48bc75f7d9901d9207bba013f501cc9056091d19..507437a93cbfa29ba2e0136e4c244d637cf0e2b3 100644 (file)
@@ -10,7 +10,6 @@ RM := rm -rf
 -include sources.mk
 -include src/sdl/subdir.mk
 -include src/graphics/subdir.mk
 -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
 -include src/battle/subdir.mk
 -include src/app/subdir.mk
 -include src/subdir.mk
index 732350c3e648485ed2e1ecfb38b7193815f801ef..0fdadd38f9a63f83ae891efcde4019c97aab7708 100644 (file)
@@ -28,5 +28,4 @@ src \
 src/graphics \
 src/battle \
 src/app \
 src/graphics \
 src/battle \
 src/app \
-src/geometry \
 
 
index 555eb00b74789670b4fd06c7e795db1f48498979..e0bc8c4457527cddfb6a0995e74d6e0d60d4c562 100644 (file)
@@ -4,13 +4,16 @@
 
 # Add inputs and outputs from these tool invocations to the build variables 
 CPP_SRCS += \
 
 # 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 += \
 
 OBJS += \
-./src/app/Application.o 
+./src/app/Application.o \
+./src/app/Input.o 
 
 CPP_DEPS += \
 
 CPP_DEPS += \
-./src/app/Application.d 
+./src/app/Application.d \
+./src/app/Input.d 
 
 
 # Each subdirectory must supply rules for building sources it contributes
 
 
 # Each subdirectory must supply rules for building sources it contributes
index e06e231d785535488554f9b09975410f8942f667..d3b00a1efaf125aa38f62561979b5c8a0bdd038c 100644 (file)
@@ -88,6 +88,7 @@ void Application::Loop(void) {
 
 void Application::HandleEvents(void) {
        if (!CurrentState()) return;
 
 void Application::HandleEvents(void) {
        if (!CurrentState()) return;
+       input.ResetInteractiveState();
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
                switch (event.type) {
        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;
                                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:
                        default:
-                               CurrentState()->HandleEvent(event);
+                               // skip event
                                break;
                }
        }
                                break;
                }
        }
+       CurrentState()->HandleInput(input);
 }
 
 void Application::UpdateWorld(Uint32 deltaT) {
 }
 
 void Application::UpdateWorld(Uint32 deltaT) {
index 909942b829730020ecd598e368bcbaefc4d7edcb..f70dd69a29185383f395566397769e980c32aff1 100644 (file)
@@ -8,6 +8,7 @@
 #ifndef APP_APPLICATION_H_
 #define APP_APPLICATION_H_
 
 #ifndef APP_APPLICATION_H_
 #define APP_APPLICATION_H_
 
+#include "Input.h"
 #include "../sdl/InitScreen.h"
 
 #include <stack>
 #include "../sdl/InitScreen.h"
 
 #include <stack>
@@ -36,6 +37,8 @@ public:
        void PushState(State *);
        void PopState(void);
        void Quit(void);
        void PushState(State *);
        void PopState(void);
        void Quit(void);
+       Input &Buttons() { return input; }
+       const Input &Buttons() const { return input; }
 
 private:
        State *CurrentState(void);
 
 private:
        State *CurrentState(void);
@@ -51,6 +54,7 @@ private:
 private:
        sdl::InitScreen *screen;
        std::stack<State *> states;
 private:
        sdl::InitScreen *screen;
        std::stack<State *> states;
+       Input input;
        Uint32 last;
 
 };
        Uint32 last;
 
 };
diff --git a/src/app/Input.cpp b/src/app/Input.cpp
new file mode 100644 (file)
index 0000000..e810e00
--- /dev/null
@@ -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<SDLKey, Button>::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 (file)
index 0000000..ad50869
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Input.h
+ *
+ *  Created on: Aug 6, 2012
+ *      Author: holy
+ */
+
+#ifndef APP_INPUT_H_
+#define APP_INPUT_H_
+
+#include <SDL.h>
+#include <map>
+
+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<SDLKey, Button> mapping;
+       Uint16 down;
+       Uint16 pressed;
+       Uint16 released;
+
+};
+
+}
+
+#endif /* APP_INPUT_H_ */
index efee32ba2ab0f3a3001dbbe9944ab9d8f44a58c3..7f46c5746c580ddc1ea089acab018d2f3b1f5926 100644 (file)
@@ -13,6 +13,7 @@
 namespace app {
 
 class Application;
 namespace app {
 
 class Application;
+class Input;
 
 class State {
 
 
 class State {
 
@@ -28,7 +29,7 @@ public:
        /// adapt the state's graphics to given dimensions
        virtual void Resize(int width, int height) = 0;
 
        /// 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;
 
        virtual void UpdateWorld(float deltaT) = 0;
        virtual void Render(SDL_Surface *) = 0;
 
index c31786eed84aca8db1fa258bb43b8fb50738fa48..9f1acac18d859b83d378f494cc3cc380b71d2f58 100644 (file)
@@ -8,12 +8,15 @@
 #include "BattleState.h"
 
 #include "PartyLayout.h"
 #include "BattleState.h"
 
 #include "PartyLayout.h"
+#include "../app/Application.h"
+#include "../app/Input.h"
 #include "../geometry/operators.h"
 #include "../graphics/Sprite.h"
 
 #include <stdexcept>
 
 using app::Application;
 #include "../geometry/operators.h"
 #include "../graphics/Sprite.h"
 
 #include <stdexcept>
 
 using app::Application;
+using app::Input;
 using geometry::Point;
 using geometry::Vector;
 
 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) {
 
 }
 
 
 }
 
index 67e0b786beb39364b390d0978f897a5a08ac82ad..397e5e3b02a59314bd97dc7518c2746d25ab54b7 100644 (file)
@@ -17,6 +17,8 @@
 #include <vector>
 #include <SDL.h>
 
 #include <vector>
 #include <SDL.h>
 
+namespace app { class Input; }
+
 namespace battle {
 
 class PartyLayout;
 namespace battle {
 
 class PartyLayout;
@@ -40,7 +42,7 @@ public:
 
        virtual void Resize(int width, int height);
 
 
        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 *);
 
        virtual void UpdateWorld(float deltaT);
        virtual void Render(SDL_Surface *);
 
index 3ec99459014e6687127018e181b84b2c9b2bbea6..6bce28f0efe5d5e9eee4dbc044f0d6567c0139f9 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include "app/Application.h"
  */
 
 #include "app/Application.h"
+#include "app/Input.h"
 #include "battle/BattleState.h"
 #include "battle/Hero.h"
 #include "battle/Monster.h"
 #include "battle/BattleState.h"
 #include "battle/Hero.h"
 #include "battle/Monster.h"
@@ -19,6 +20,7 @@
 #include <iostream>
 
 using app::Application;
 #include <iostream>
 
 using app::Application;
+using app::Input;
 using battle::BattleState;
 using battle::Hero;
 using battle::Monster;
 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);
                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;
                app.Run();
 
                return 0;