summaryrefslogtreecommitdiff
path: root/source/presentation/KeyboardHandler.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/presentation/KeyboardHandler.cpp')
-rw-r--r--source/presentation/KeyboardHandler.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/source/presentation/KeyboardHandler.cpp b/source/presentation/KeyboardHandler.cpp
new file mode 100644
index 0000000..b588cbe
--- /dev/null
+++ b/source/presentation/KeyboardHandler.cpp
@@ -0,0 +1,114 @@
+#include "KeyboardHandler.h"
+
+KeyboardHandler::KeyboardHandler(Maze::Direction currentFacing)
+ :_up(false),
+ _down(false),
+ _left(false),
+ _right(false),
+ _smokescreen(false),
+ _previousFacing(currentFacing)
+{
+ _keyboardEvents = al_create_event_queue();
+ al_register_event_source(_keyboardEvents, al_get_keyboard_event_source());
+}
+
+KeyboardHandler::KeyboardHandler(const KeyboardHandler& ref)
+ :_up(ref._up),
+ _down(ref._down),
+ _left(ref._left),
+ _right(ref._right),
+ _smokescreen(ref._smokescreen),
+ _previousFacing(ref._previousFacing)
+{
+ _keyboardEvents = al_create_event_queue();
+ al_register_event_source(_keyboardEvents, al_get_keyboard_event_source());
+}
+
+KeyboardHandler& KeyboardHandler::operator=(const KeyboardHandler& rhs)
+{
+ _up = rhs._up;
+ _down = rhs._down;
+ _left = rhs._left;
+ _right = rhs._right;
+ _smokescreen = rhs._smokescreen;
+ _previousFacing = rhs._previousFacing;
+
+ if (_keyboardEvents!=rhs._keyboardEvents) al_destroy_event_queue(_keyboardEvents);
+
+ _keyboardEvents = al_create_event_queue();
+ al_register_event_source(_keyboardEvents, al_get_keyboard_event_source());
+
+ return *this;
+}
+
+KeyboardHandler::~KeyboardHandler()
+{
+ al_destroy_event_queue(_keyboardEvents);
+}
+
+void KeyboardHandler::updateFlags()
+{
+ ALLEGRO_EVENT event;
+ while (al_get_next_event(_keyboardEvents, &event))
+ {
+ if (event.type==ALLEGRO_EVENT_KEY_DOWN)
+ {
+ switch (event.keyboard.keycode)
+ {
+ case UP_KEY:
+ _up = true;
+ break;
+ case DOWN_KEY:
+ _down = true;
+ break;
+ case LEFT_KEY:
+ _left = true;
+ break;
+ case RIGHT_KEY:
+ _right = true;
+ break;
+ case SMOKESCREEN_KEY:
+ _smokescreen = true;
+ break;
+ }
+ }
+ else if (event.type==ALLEGRO_EVENT_KEY_UP)
+ {
+ switch (event.keyboard.keycode)
+ {
+ case UP_KEY:
+ _up = false;
+ break;
+ case DOWN_KEY:
+ _down = false;
+ break;
+ case LEFT_KEY:
+ _left = false;
+ break;
+ case RIGHT_KEY:
+ _right = false;
+ break;
+ case SMOKESCREEN_KEY:
+ _smokescreen = false;
+ break;
+ }
+ }
+ }
+}
+
+Maze::Direction KeyboardHandler::getFacing()
+{
+ updateFlags();
+
+ if (_up) _previousFacing = Maze::UP;
+ else if (_down) _previousFacing = Maze::DOWN;
+ else if (_left) _previousFacing = Maze::LEFT;
+ else if (_right) _previousFacing = Maze::RIGHT;
+
+ return _previousFacing;
+}
+
+bool KeyboardHandler::getSmokescreen()
+{
+ return _smokescreen;
+}