summaryrefslogtreecommitdiffstats
path: root/engine/include
diff options
context:
space:
mode:
authorancarola <raffaele.ancarola@epfl.ch>2019-01-19 13:07:37 +0100
committerancarola <raffaele.ancarola@epfl.ch>2019-01-19 13:07:37 +0100
commit3922c797671cdc23d9233ff76909489e45fd0006 (patch)
treed385754f00159b93a8966fb5080a51dc2d0eb4a0 /engine/include
parentedas (diff)
downloadflatland-3922c797671cdc23d9233ff76909489e45fd0006.tar.gz
flatland-3922c797671cdc23d9233ff76909489e45fd0006.zip
Test two completed successfully
Diffstat (limited to 'engine/include')
-rw-r--r--engine/include/flatland.h8
-rw-r--r--engine/include/flatobject.h11
-rw-r--r--engine/include/flatserial.h38
-rw-r--r--engine/include/flatsignal.h103
-rw-r--r--engine/include/flatwindow.h6
-rw-r--r--engine/include/serial/focusable.h (renamed from engine/include/focusable.h)25
-rw-r--r--engine/include/serial/keyfocusable.h24
-rw-r--r--engine/include/types.h3
8 files changed, 156 insertions, 62 deletions
diff --git a/engine/include/flatland.h b/engine/include/flatland.h
index 18b1a89..27d2d12 100644
--- a/engine/include/flatland.h
+++ b/engine/include/flatland.h
@@ -35,9 +35,15 @@ struct flat_status
unsigned char loop:1;
};
-int init_flatland(const FlatWindow&, gameloop, const flat_status&, float fps = 60);
+int init_flatland(FlatWindow*, gameloop, const flat_status&, float fps = 60);
void quit_flatland();
+/* Core channel */
+
+class SignalChannel;
+
+SignalChannel * getCoreChannel();
+
/* Window and status accessors */
FlatWindow * getFlatWindow();
diff --git a/engine/include/flatobject.h b/engine/include/flatobject.h
index fe7b107..7ad1ddd 100644
--- a/engine/include/flatobject.h
+++ b/engine/include/flatobject.h
@@ -3,13 +3,14 @@
#include <list>
#include <vector>
+#include <string>
#include <initializer_list>
#include "types.h"
class FlatObject
{
- char id[32];
+ std::string id;
/* Common list of objects */
static std::list<FlatObject*> allObjects;
@@ -19,15 +20,17 @@ public:
FlatObject();
~FlatObject();
- void setID(const char*);
+ void setID(const std::string&);
- const char* getID() const;
+ const std::string& getID() const;
/* Static accessors to allObject */
static bool isAllocated(FlatObject*);
- static std::vector<FlatObject*>& getByID(const char *id, std::vector<FlatObject*>&);
+ static std::vector<FlatObject*>& getByID(const std::string& id, std::vector<FlatObject*>&);
+
+ static std::string randomID(Uint8 length = 8);
};
#endif
diff --git a/engine/include/flatserial.h b/engine/include/flatserial.h
index b95413d..41c6b97 100644
--- a/engine/include/flatserial.h
+++ b/engine/include/flatserial.h
@@ -1,14 +1,42 @@
#ifndef __FLATSERIAL_H__
#define __FLATSERIAL_H__
-/* SDL serial events handling */
+#include "types.h"
+#include <vector>
+#include "SDL2/SDL.h"
-class Focusable;
+union SDL_Event;
+class task_s;
-void process_events();
+struct SDL_EventCollector
+{
+ task_s * checker;
+
+ /* Keyboard event */
+ std::vector<SDL_Event> keyboard;
-void registerFocusable(Focusable*);
+ /* Window resize or iconize event */
+ std::vector<SDL_Event> window;
-void unregisterFocusable(Focusable*);
+ /* Quit notification */
+ std::vector<SDL_Event> quit;
+
+ /* Custom events: useful for signals */
+ std::vector<SDL_Event> user;
+
+ // TODO other events
+
+ SDL_EventCollector();
+ ~SDL_EventCollector();
+
+ void collect(void*);
+
+ const std::vector<SDL_Event>& getStack(Uint32 id) const;
+};
+
+namespace FlatSerial {
+
+ extern SDL_EventCollector * collector;
+}
#endif
diff --git a/engine/include/flatsignal.h b/engine/include/flatsignal.h
index 6ec837a..fa403e2 100644
--- a/engine/include/flatsignal.h
+++ b/engine/include/flatsignal.h
@@ -1,60 +1,97 @@
#ifndef __FLAT_SIGNAL_H__
#define __FLAT_SIGNAL_H__
-#include <vector>
#include <map>
+#include <list>
+#include <set>
+#include <initializer_list>
#include "flatobject.h"
+#include "types.h"
-class task_s;
+/* Post-process signal delivery when it's emitted */
+#define FSIG_PRIOR_INSTANT 0
-struct FlatSignal : virtual public FlatObject
+/* Process and emitted signal instantaneously */
+#define FSIG_PRIOR_DEFAULT 5
+
+class FlatSignal : virtual public FlatObject
{
- const int type;
+
+public:
+
+ FlatObject * sender;
void * data;
+ Uint8 priority;
- FlatSignal(int type, void *data);
+ FlatSignal(FlatObject * sender, void *data = 0, Uint8 priority = 5);
- static emit(const FlatSignal&);
- static std::vector<FlatSignal> sig_stack;
+ /* Alias to SignalChannel::emit() */
+ bool emit(const std::string& channel) const;
};
-template <class T>
-class FlatListener : virtual public FlatObject
-{
- typedef void (T::*callback_t)(void*);
+class FlatListener;
- task_s * listener;
+/* Functor for order comparison */
+struct sig_prior_cmp {
+
+ bool operator()(const FlatSignal&, const FlatSignal&) const;
+};
- void checkSignal(void*)
- {
- for (auto signal : FlatSignal::sig_stack)
- {
- callback_t cb;
+typedef std::set<FlatSignal, sig_prior_cmp> SignalStack;
- if (cb = checkmap.find(signal.type))
- cb(signal.data);
- }
- }
+/* Channel class */
+class SignalChannel : virtual public FlatObject
+{
+ /* Post processing signal stacking */
+ SignalStack stack;
- std::map<int, callback_t> checkmap;
+ /* Listeners list */
+ std::list<FlatListener*> listeners;
-public:
+ /* Synchronous task checking for signals */
+ task_s * checker;
- FlatListener()
- {
+ /* Channel mapping */
+ static std::map<std::string, SignalChannel*> channels;
+
+public:
- }
+ SignalChannel(const std::string& id = "");
+ ~SignalChannel();
- ~FlatListener()
- {
+ void emit(const FlatSignal&);
- }
+ void connect(FlatListener*);
+ void disconnect(FlatListener*);
+
+ static SignalChannel * findChannel(const std::string&);
- connect(int type, callback_t cb)
- {
- checkmap.insert(std::make_pair(type, cb));
- }
+ void post_processing(void*);
};
+/* Listener class */
+class FlatListener : virtual public FlatObject
+{
+ std::list<std::string> filters;
+
+ bool checkInFilters(const std::string&) const;
+
+protected:
+
+ virtual void callback(FlatObject *sender, void *) = 0;
+
+public:
+
+ FlatListener(const std::initializer_list<std::string>& filters = {});
+ virtual ~FlatListener() {}
+
+ void execute(const FlatSignal&);
+
+ void addFilter(const std::string&);
+ void removeFilter(const std::string&);
+
+ bool connect(const std::string&);
+ bool disconnect(const std::string&);
+};
#endif
diff --git a/engine/include/flatwindow.h b/engine/include/flatwindow.h
index 8c3d5bf..eb113c1 100644
--- a/engine/include/flatwindow.h
+++ b/engine/include/flatwindow.h
@@ -27,14 +27,14 @@ struct window_status
};
#include "flatobject.h"
-#include "focusable.h"
+#include "serial/keyfocusable.h"
class SDL_Window;
class FlatLayer;
class SDL_KeyEvent;
-class FlatWindow : public FlatObject, public Focusable
+class FlatWindow : public FlatObject, virtual public KeyFocusable
{
std::string title;
window_status status;
@@ -45,7 +45,7 @@ class FlatWindow : public FlatObject, public Focusable
FlatLayer * main_layer;
- void serial_cb(const SDL_Event&) override;
+ void key_cb(const SDL_KeyboardEvent*) override;
// TODO window calls
//protected:
diff --git a/engine/include/focusable.h b/engine/include/serial/focusable.h
index b70357b..325f84b 100644
--- a/engine/include/focusable.h
+++ b/engine/include/serial/focusable.h
@@ -1,21 +1,10 @@
#ifndef __FOCUSABLE_H__
#define __FOCUSABLE_H__
-#include <vector>
+#include "types.h"
-union SDL_Event;
class task_s;
-
-struct SDL_EventCollector
-{
- task_s * checker;
- std::vector<SDL_Event> stack;
-
- SDL_EventCollector();
- ~SDL_EventCollector();
-
- void collect(void*);
-};
+union SDL_Event;
class Focusable
{
@@ -23,7 +12,13 @@ class Focusable
task_s * event_trigger;
- static SDL_EventCollector * collector;
+protected:
+
+ /* Callback to event */
+ virtual void serial_cb(const SDL_Event*) = 0;
+
+ /* Event stack specification */
+ virtual Uint32 stackID() const = 0;
public:
@@ -31,8 +26,6 @@ public:
virtual ~Focusable();
- virtual void serial_cb(const SDL_Event&) = 0;
-
void setFocused(bool flag);
bool isFocused() const;
diff --git a/engine/include/serial/keyfocusable.h b/engine/include/serial/keyfocusable.h
index e69de29..02b3361 100644
--- a/engine/include/serial/keyfocusable.h
+++ b/engine/include/serial/keyfocusable.h
@@ -0,0 +1,24 @@
+#ifndef __KEYFOCUSABLE_H__
+#define __KEYFOCUSABLE_H__
+
+#include "focusable.h"
+
+struct SDL_KeyboardEvent;
+
+class KeyFocusable : public Focusable
+{
+
+ virtual void serial_cb(const SDL_Event*) override;
+
+ virtual Uint32 stackID() const override;
+
+protected:
+
+ virtual void key_cb(const SDL_KeyboardEvent*) = 0;
+
+public:
+
+ using Focusable::Focusable;
+};
+
+#endif
diff --git a/engine/include/types.h b/engine/include/types.h
index 3141e6c..003c114 100644
--- a/engine/include/types.h
+++ b/engine/include/types.h
@@ -22,8 +22,11 @@ class FlatWindow;
class FlatSpriter;
class FlatMultiSpriter;
+class task_s;
+
/* SDL types */
+typedef unsigned char Uint8;
typedef unsigned int Uint32;
struct SDL_Surface;