1
0
Fork 0

Merge branch 'maint-platform' into dev

This commit is contained in:
Henrik Hautakoski 2020-09-24 17:17:12 +02:00
commit f86a1ae90c
45 changed files with 1091 additions and 1001 deletions

View file

@ -5,7 +5,6 @@
#include "DisplayMode.h"
#include "DisplayDescription.h"
#include <Spectre/Display/GLContext.h>
#include <Spectre/System/SystemEvent.h>
#include <string>
namespace sp {

View file

@ -3,7 +3,6 @@
#define SPECTRE_GAME_H
#include <Spectre/Graphics.h>
#include <Spectre/Input/InputEvent.h>
#include <Spectre/Game/FPSCounter.h>
class InputModule;
@ -37,9 +36,9 @@ protected :
FPSCounter& getFpsCounter();
private :
MessageHandler* getMessageHandler() const;
void setup();
private :
void gameLoop();

View file

@ -2,10 +2,10 @@
#ifndef SPECTRE_INTPUT_DEVICE_H
#define SPECTRE_INTPUT_DEVICE_H
#include "InputModule.h"
namespace sp {
class InputMudule;
class InputDevice
{
friend class InputModule;

View file

@ -1,157 +0,0 @@
#ifndef SPECTRE_INPUT_EVENT_H
#define SPECTRE_INPUT_EVENT_H
#include <string>
#include <vector>
namespace sp {
namespace MouseButton {
enum Type {
Unknown,
Left,
Right,
Middle,
Button1,
Button2,
NUM_MBUTTONS,
};
};
namespace Key {
enum Type {
Unknown,
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,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Zero,
Period,
Comma,
Enter,
Backspace,
Escape,
Space,
Capslock,
Up,
Down,
Left,
Right,
NUMPAD_1,
NUMPAD_2,
NUMPAD_3,
NUMPAD_4,
NUMPAD_5,
NUMPAD_6,
NUMPAD_7,
NUMPAD_8,
NUMPAD_9,
NUMPAD_0,
NUMPAD_Enter,
Home,
End,
Insert,
Delete,
PageUp,
PageDown,
Pause,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
Tab,
LShift,
RShift,
LCtrl,
RCtrl,
LAlt,
RAlt,
NUM_KEYS,
};
}
typedef struct InputEvent
{
enum Type
{
None,
Key,
MouseButton,
MousePosition
};
struct KeyEvent {
Key::Type code;
bool pressed; /* true if pressed, false if released. */
std::string getKeyName() const; /* Get the key name */
};
struct MouseButtonEvent {
MouseButton::Type button;
bool pressed; /* true if pressed, false if released. */
std::string getName() const;
};
struct MouseEvent {
unsigned int x;
unsigned int y;
};
Type type;
union {
struct KeyEvent key;
struct MouseEvent mouse;
struct MouseButtonEvent mouseButton;
};
InputEvent(Type type = None);
} InputEvent;
} // namespace sp
#endif /* SPECTRE_INPUT_EVENT_H */

View file

@ -1,17 +0,0 @@
#ifndef SPECTRE_INPUT_LISTENER_H
#define SPECTRE_INPUT_LISTENER_H
#include "InputEvent.h"
namespace sp {
class InputListener
{
public :
virtual void onInputEvent(const InputEvent& event);
};
} // namespace sp
#endif /* SPECTRE_INPUT_LISTENER_H */

View file

@ -5,9 +5,6 @@
#include <deque>
#include <vector>
#include "InputListener.h"
#include "InputEvent.h"
namespace sp {
class Mouse;
@ -28,12 +25,6 @@ public :
void addInputDevice(InputDevice *device);
void registerListener(InputListener* listener);
void removeListener(InputListener* listener);
void postInputEvent(const InputEvent& event);
// NOTE: Update devices here! (for winapi, process keyboard/mouse messages)
void update();
@ -48,11 +39,6 @@ protected :
Keyboard *m_keyboard;
// Buffered input queue.
std::deque<InputEvent> m_buffer;
std::vector<InputListener*> m_listeners;
PlatformInput *m_platform;
};

View file

@ -3,19 +3,107 @@
#define SPECTRE_INPUT_KEYBOARD_H
#include <string>
#include "InputEvent.h"
#include "InputDevice.h"
namespace sp {
class Keyboard : public InputDevice
{
public :
enum Key {
Unknown,
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,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Zero,
Period,
Comma,
Enter,
Backspace,
Escape,
Space,
Capslock,
Up,
Down,
Left,
Right,
Numpad1,
Numpad2,
Numpad3,
Numpad4,
Numpad5,
Numpad6,
Numpad7,
Numpad8,
Numpad9,
Numpad0,
Home,
End,
Insert,
Delete,
PageUp,
PageDown,
Pause,
F1,
F2,
F3,
F4,
F5,
F6,
F7,
F8,
F9,
F10,
F11,
F12,
Tab,
LShift,
RShift,
LCtrl,
RCtrl,
LAlt,
RAlt,
NUM_KEYS,
};
public :
virtual ~Keyboard() {};
virtual bool isKeyDown(Key::Type key) = 0;
virtual bool isKeyDown(Key key) = 0;
static std::string getKeyName(Key::Type key);
static std::string getKeyName(Key key);
};
} // namespace sp

View file

@ -4,26 +4,39 @@
#include <string>
#include <Spectre/Math/Vector2.h>
#include "InputEvent.h"
#include "InputDevice.h"
namespace sp {
class Mouse : public InputDevice
{
public :
enum Button {
Unknown,
Left,
Right,
Middle,
Button1,
Button2,
NUM_MBUTTONS
};
public :
virtual ~Mouse();
// Get mouse position
// Get the position in relative (focused window) coordinates.
// coordinates are relative to the window's top-left corner.
virtual Vector2f getPosition() const = 0;
//virtual Vector2i getPositionAbs() const = 0;
// Get the position in absolute (screen) coordinates.
// 0,0 is located at the screen's top-left corner.
virtual Vector2f getAbsPosition() const = 0;
virtual bool isButtonDown(MouseButton::Type button) const = 0;
virtual bool isButtonDown(Button button) const = 0;
static std::string getButtonName(MouseButton::Type button);
static std::string getButtonName(Button button);
};
} // namespace sp
}; // namespace sp
#endif /* SPECTRE_INPUT_MOUSE_H */

View file

@ -0,0 +1,74 @@
#ifndef SPECTRE_SYSTEM_EVENT_H
#define SPECTRE_SYSTEM_EVENT_H
#include <Spectre/Input/Mouse.h>
#include <Spectre/Input/Keyboard.h>
namespace sp {
class Display;
struct Event
{
public :
enum Type {
None,
Quit,
Size,
Key,
MouseButton,
MouseMove
};
struct KeyEvent {
Keyboard::Key code;
bool pressed; /* true if pressed, false if released. */
std::string getKeyName() const; /* Get the key name */
};
struct MouseButtonEvent {
Mouse::Button button;
bool pressed; /* true if pressed, false if released. */
std::string getName() const;
};
struct MouseMoveEvent {
unsigned int x;
unsigned int y;
};
struct SizeEvent
{
Display *display;
int width;
int height;
};
Type type;
union {
struct SizeEvent size;
struct KeyEvent key;
struct MouseMoveEvent mouseMove;
struct MouseButtonEvent mouseButton;
};
Event(Type type = None);
// Helper methods
static Event createSize(Display *display, int width, int height);
static Event createKey(Keyboard::Key code, bool pressed);
static Event createMouseButton(Mouse::Button button, bool pressed);
static Event createMouseMove(unsigned int x, unsigned int y);
};
} // namespace sp
#endif /* SPECTRE_SYSTEM_EVENT_H */

View file

@ -0,0 +1,23 @@
#ifndef SYSTEM_EVENT_LISTENER_H
#define SYSTEM_EVENT_LISTENER_H
#include <Spectre/System/Event.h>
#include <Spectre/Input/Mouse.h>
#include <Spectre/Input/Keyboard.h>
namespace sp {
class Display;
class EventListener
{
public :
virtual void onSizeChanged(Display* display, int width, int height);
virtual void onEvent(const Event& event);
};
} // namespace sp
#endif /* SYSTEM_EVENT_LISTENER_H */

View file

@ -2,16 +2,25 @@
#ifndef SPECTRE_SYSTEM_MESSAGEHANDLER_H
#define SPECTRE_SYSTEM_MESSAGEHANDLER_H
#include "SystemEvent.h"
#include <vector>
#include "EventListener.h"
namespace sp {
class Display;
class MessageHandler
class MessageHandler : public EventListener
{
public :
void registerListener(EventListener *listener);
void unregisterListener(EventListener *listener);
virtual void onSizeChanged(Display* display, int width, int height);
virtual void onEvent(const Event& event);
protected :
std::vector<EventListener*> m_listeners;
};
} // namespace sp

View file

@ -2,22 +2,29 @@
#ifndef SPECTRE_MESSAGE_QUEUE_H
#define SPECTRE_MESSAGE_QUEUE_H
#include <Spectre/System/SystemEvent.h>
#include <Spectre/System/Event.h>
#include <queue>
namespace sp {
class PlatformEventQueue;
class MessageQueue
{
public :
void postEvent(SysEvent event);
MessageQueue();
~MessageQueue();
bool pollEvent(SysEvent& event);
void postEvent(Event event);
bool pollEvent(Event& event);
bool isEmpty() const;
protected :
std::deque<SysEvent> m_queue;
std::deque<Event> m_queue;
PlatformEventQueue* m_impl;
};
} // namespace sp

View file

@ -1,41 +0,0 @@
#ifndef SYSTEM_EVENT_H
#define SYSTEM_EVENT_H
namespace sp {
class Display;
struct SysEvent
{
public :
enum Type {
None,
Quit,
Size,
};
Type type;
struct Size
{
Display *display;
int width;
int height;
};
union {
struct Size size;
};
SysEvent(Type type = None);
// Helper methods
static SysEvent sizeEvent(Display *display, int width, int height);
};
} // namespace sp
#endif /* SYSTEM_EVENT_H */