also changed application states to utilize the new Input class rather than handling events themselves and mapped all keys in main() for testing
-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
src/graphics \
src/battle \
src/app \
-src/geometry \
# 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
-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
src/graphics \
src/battle \
src/app \
-src/geometry \
# 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
void Application::HandleEvents(void) {
if (!CurrentState()) return;
+ input.ResetInteractiveState();
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
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) {
#ifndef APP_APPLICATION_H_
#define APP_APPLICATION_H_
+#include "Input.h"
#include "../sdl/InitScreen.h"
#include <stack>
void PushState(State *);
void PopState(void);
void Quit(void);
+ Input &Buttons() { return input; }
+ const Input &Buttons() const { return input; }
private:
State *CurrentState(void);
private:
sdl::InitScreen *screen;
std::stack<State *> states;
+ Input input;
Uint32 last;
};
--- /dev/null
+/*
+ * 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;
+ }
+}
+
+}
--- /dev/null
+/*
+ * 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_ */
namespace app {
class Application;
+class Input;
class State {
/// 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;
#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;
+using app::Input;
using geometry::Point;
using geometry::Vector;
}
-void BattleState::HandleEvent(const SDL_Event &) {
+void BattleState::HandleInput(const Input &input) {
}
#include <vector>
#include <SDL.h>
+namespace app { class Input; }
+
namespace battle {
class PartyLayout;
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 *);
*/
#include "app/Application.h"
+#include "app/Input.h"
#include "battle/BattleState.h"
#include "battle/Hero.h"
#include "battle/Monster.h"
#include <iostream>
using app::Application;
+using app::Input;
using battle::BattleState;
using battle::Hero;
using battle::Monster;
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;