Skip to content

Commit

Permalink
add own (minimalistic) eventinterface to abstract the engine from the…
Browse files Browse the repository at this point in the history
… underlaying framework
  • Loading branch information
skaupper committed Feb 22, 2017
1 parent b1d4490 commit 0b957e5
Show file tree
Hide file tree
Showing 15 changed files with 567 additions and 23 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ SET(SOURCES src/Core.cpp
src/Logger.cpp
src/Misc.cpp
src/Fonts.cpp
src/Entity.cpp)
src/Entity.cpp
src/SDLEventInterface.cpp)

IF(ENABLE_TESTS)
SET (SOURCES ${SOURCES} src/SDLWrapper.cpp)
Expand Down
5 changes: 2 additions & 3 deletions include/bkengine/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
#include <string>
#include <memory>

#include <SDL.h>

#include "Animation.h"
#include "Misc.h"
#include "Event.h"

namespace bkengine
{
Expand Down Expand Up @@ -45,7 +44,7 @@ namespace bkengine

virtual void onRender(const Rect &parentRect = Rect());
virtual void onLoop();
virtual bool onEvent(SDL_Event *e);
virtual bool onEvent(const Event &);

void applyCollisionBox(const Rect &);
Rect getCollisionBox() const;
Expand Down
3 changes: 2 additions & 1 deletion include/bkengine/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace bkengine
class Entity : public Element
{
public:
explicit Entity(Scene *parentScene, const std::string &descr = "", const Rect &renderBox = Rect(),
explicit Entity(Scene *parentScene, const std::string &descr = "",
const Rect &renderBox = Rect(),
int collisionLayer = -1);
virtual ~Entity();

Expand Down
90 changes: 90 additions & 0 deletions include/bkengine/Event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef EVENT_H
#define EVENT_H

#include <chrono>

#include "Keys.h"


namespace bkengine
{
enum class KeyState {
NONE,
DOWN,
UP
};

typedef KeyState ButtonState;

enum class WheelDirection {
NORMAL,
FLIPPED
};

enum class EventType {
UNKNOWN,
APPLICATION,
KEYBOARD,
MOUSE,
MOTION,
WHEEL,
QUIT
};


struct KeyboardEvent {
Keys key;
KeyState state;
bool repeat;
};

struct MouseEvent {
ButtonState state;
Buttons button;
uint8_t specialId;
uint8_t clicks;
int32_t x;
int32_t y;
};

struct MotionEvent {
int32_t x;
int32_t y;
int32_t relativeX;
int32_t relativeY;
};

struct WheelEvent {
int32_t x;
int32_t y;
WheelDirection direction;
};

struct ApplicationEvent {
// TODO: implement
};

struct WindowEvent {
// TODO: implement
};

class Event
{
public:
EventType type = EventType::UNKNOWN;
uint32_t timeStamp = std::chrono::duration_cast<std::chrono::seconds>
(std::chrono::system_clock::now().time_since_epoch()).count();
uint32_t windowId = 0;

union {
KeyboardEvent keyboard;
MouseEvent mouse;
MotionEvent motion;
WheelEvent wheel;
ApplicationEvent application;
WindowEvent window;
};
};
}

#endif
16 changes: 16 additions & 0 deletions include/bkengine/EventInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef EVENT_INTERFACE_H
#define EVENT_INTERFACE_H

#include "Event.h"

namespace bkengine
{
class EventInterface
{
public:
virtual bool ready() = 0;
virtual Event poll() = 0;
};
}

#endif
7 changes: 6 additions & 1 deletion include/bkengine/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "Core.h"
#include "Scene.h"
#include "Timer.h"
#include "SDLEventInterface.h"
#include "EventInterface.h"

namespace bkengine
{
Expand Down Expand Up @@ -40,6 +42,8 @@ namespace bkengine
template <typename T> T &getScene(unsigned int index);
template <typename T> T &getCurrentScene();

template <typename T> void setEventInterface();

void run();
void stop();

Expand All @@ -50,9 +54,10 @@ namespace bkengine
int activeScene;
bool running;
Timer timer;
std::shared_ptr<EventInterface> eventInterface;

void onLoop();
bool onEvent(SDL_Event *);
bool onEvent(const Event &);
void onRender();
};

Expand Down
155 changes: 155 additions & 0 deletions include/bkengine/Keys.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#ifndef KEYS_H
#define KEYS_H

namespace bkengine
{
enum class Keys {
UNKNOWN,

/* Control keys */
RETURN,
ESCAPE,
BACKSPACE,
TAB,
SPACE,
CAPSLOCK,

/* Number keys */
ZERO,
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,

/* Letter keys */
A,
B,
C,
D,
E,
F,
G,
H,
I,
J,
K,
L,
M,
N,
O,
P,
Q,
R,
S,
T,
U,
V,
W,
X,
Y,
Z,

/* Function keys */
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
F13,
F14,
F15,
F16,
F17,
F18,
F19,
F20,
F21,
F22,
F23,
F24,

/* Arrow keys */
RIGHT,
LEFT,
DOWN,
UP,

PRINTSCREEN,
SCROLLLOCK,
PAUSE,
INSERT,
HOME,
PAGEUP,
DELETE,
END,
PAGEDOWN,

/* Numpad keys */
NUMLOCKCLEAR,
NP_DIVIDE,
NP_MULTIPLY,
NP_MINUS,
NP_PLUS,
NP_ENTER,
NP_ONE,
NP_TWO,
NP_THREE,
NP_FOUR,
NP_FIVE,
NP_SIX,
NP_SEVEN,
NP_EIGHT,
NP_NINE,
NP_ZERO,
NP_SEPARATOR,

/* Modifier keys */
LCTRL,
LSHIFT,
LALT,
LGUI,
RCTRL,
RSHIFT,
RALT,
RGUI,

/* Media keys */
MUTE,
VOLUMEUP,
VOLUMEDOWN,
AUDIONEXT,
AUDIOPREV,
AUDIOSTOP,
AUDIOPLAY,
AUDIOMUTE,

/* Special keys */
APPLICATION,
MENU,
BRIGHTNESSDOWN,
BRIGHTNESSUP,
SLEEP,
POWER
};

enum class Buttons {
LEFT,
MIDDLE,
RIGHT,
SPECIAL
};
}

#endif
9 changes: 5 additions & 4 deletions include/bkengine/Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

namespace bkengine
{
template <typename T> int sgn(T val) {
template <typename T> int sgn(T val)
{
return (T(0) < val) - (val < T(0));
}

struct Location {
float x;
float y;
Expand Down Expand Up @@ -98,8 +99,8 @@ namespace bkengine

class RelativeCoordinates
{
public:
static Rect apply(const Rect &rect, const Rect &srcRect);
public:
static Rect apply(const Rect &rect, const Rect &srcRect);
};
}

Expand Down
20 changes: 20 additions & 0 deletions include/bkengine/SDLEventInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef SDL_EVENT_INTERFACE_H
#define SDL_EVENT_INTERFACE_H

#include <map>

#include "SDLWrapper.h"
#include "EventInterface.h"
#include "Logger.h"

namespace bkengine
{
class SDLEventInterface : public EventInterface
{
public:
virtual bool ready();
virtual Event poll();
};
}

#endif
Loading

0 comments on commit 0b957e5

Please sign in to comment.