summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorancarola <raffaele.ancarola@epfl.ch>2019-01-20 01:19:26 +0100
committerancarola <raffaele.ancarola@epfl.ch>2019-01-20 01:19:26 +0100
commit6c0c2f6f10e86986087c71a2964974ebcd939a3f (patch)
tree0a16aff4380188c6ee222579dbb55392a230dbc0
parentIgnore swap files (diff)
downloadflatland-6c0c2f6f10e86986087c71a2964974ebcd939a3f.tar.gz
flatland-6c0c2f6f10e86986087c71a2964974ebcd939a3f.zip
Test 3 partially executed
-rw-r--r--engine/flatevolvable.cpp6
-rw-r--r--engine/flatland.cpp15
-rw-r--r--engine/flatserial.cpp15
-rw-r--r--engine/flatsignal.cpp17
-rw-r--r--engine/flattask.cpp42
-rw-r--r--engine/flatwindow.cpp2
-rw-r--r--engine/include/flatevolvable.h4
-rw-r--r--engine/include/flatserial.h2
-rw-r--r--engine/include/flatsignal.h5
-rw-r--r--engine/include/flattask.h43
-rw-r--r--engine/include/flatwindow.h2
-rw-r--r--engine/include/serial/focusable.h3
-rw-r--r--engine/include/serial/keyfocusable.h2
-rw-r--r--goals8
-rw-r--r--test/main.cpp105
-rwxr-xr-xtest/testbin14656 -> 37408 bytes
-rw-r--r--test/test2.cpp31
17 files changed, 251 insertions, 51 deletions
diff --git a/engine/flatevolvable.cpp b/engine/flatevolvable.cpp
index 20dd6dd..c9eccf6 100644
--- a/engine/flatevolvable.cpp
+++ b/engine/flatevolvable.cpp
@@ -15,9 +15,9 @@ FlatEvolvable::~FlatEvolvable()
delete task;
}
-void FlatEvolvable::evolve_task(void *data)
+void FlatEvolvable::evolve_task(float *data)
{
- evolve(*(float*)data);
+ evolve(*data);
}
void FlatEvolvable::setEvolving(bool flag)
@@ -25,7 +25,7 @@ void FlatEvolvable::setEvolving(bool flag)
if (isEvolving() != flag)
{
if (flag)
- task = new FlatTask<FlatEvolvable>(this, &FlatEvolvable::evolve_task, (void*)&flatland_dt);
+ task = new FlatTask<FlatEvolvable, float*>(this, &FlatEvolvable::evolve_task, &flatland_dt);
else {
delete task;
diff --git a/engine/flatland.cpp b/engine/flatland.cpp
index cbb9842..2f1c920 100644
--- a/engine/flatland.cpp
+++ b/engine/flatland.cpp
@@ -130,6 +130,17 @@ int init_flatland(FlatWindow* w, gameloop loop, const flat_status& s, float _fps
try {
try {
+
+ /* Execute tasks */
+ task_s::executePreProcess();
+
+ } catch (const exception &e) {
+
+ cerr << "Flatland: exception thrown while executing pre-process tasks" << endl;
+ cerr << e.what() << endl;
+ }
+
+ try {
/* Execute loop function */
loop_function(flatland_dt);
@@ -143,11 +154,11 @@ int init_flatland(FlatWindow* w, gameloop loop, const flat_status& s, float _fps
try {
/* Execute tasks */
- task_s::executeAll();
+ task_s::executePostProcess();
} catch (const exception &e) {
- cerr << "Flatland: exception thrown while executing tasks" << endl;
+ cerr << "Flatland: exception thrown while executing post-process tasks" << endl;
cerr << e.what() << endl;
}
diff --git a/engine/flatserial.cpp b/engine/flatserial.cpp
index 25eafbe..9c49787 100644
--- a/engine/flatserial.cpp
+++ b/engine/flatserial.cpp
@@ -3,12 +3,17 @@
SDL_EventCollector::SDL_EventCollector()
{
- checker = new FlatTask<SDL_EventCollector>(this, &SDL_EventCollector::collect, 0);
+ /* Checker task, pre-process, maximum priority */
+ checker = new FlatTask<SDL_EventCollector>(this, &SDL_EventCollector::collect, 0, true, 0);
+
+ /* Eraser task, post-process, minimum priority */
+ eraser = new FlatTask<SDL_EventCollector>(this, &SDL_EventCollector::erase, 0, false, 0xff);
}
SDL_EventCollector::~SDL_EventCollector()
{
delete checker;
+ delete eraser;
}
void SDL_EventCollector::collect(void*)
@@ -49,6 +54,14 @@ void SDL_EventCollector::collect(void*)
}
}
+void SDL_EventCollector::erase(void*)
+{
+ keyboard.clear();
+ window.clear();
+ quit.clear();
+ user.clear();
+}
+
const std::vector<SDL_Event>& SDL_EventCollector::getStack(Uint32 id) const
{
switch(id)
diff --git a/engine/flatsignal.cpp b/engine/flatsignal.cpp
index 8d66ae1..cdbc800 100644
--- a/engine/flatsignal.cpp
+++ b/engine/flatsignal.cpp
@@ -18,8 +18,8 @@ SignalChannel::SignalChannel(const string& id)
//TODO throw exception
;
- /* Initialize task */
- checker = new FlatTask<SignalChannel>(this, &SignalChannel::post_processing, 0);
+ /* Initialize task, post-process, fifth priority */
+ checker = new FlatTask<SignalChannel>(this, &SignalChannel::post_processing, 0, false, 4);
string ID = (id.empty()) ? FlatObject::randomID() : id;
@@ -96,9 +96,10 @@ void SignalChannel::post_processing(void*)
/* FlatSignal class */
-FlatSignal::FlatSignal(FlatObject *sender, void *data, Uint8 priority)
+FlatSignal::FlatSignal(FlatObject *sender, const string& id, void *data, Uint8 priority)
: sender(sender), data(data), priority(priority)
{
+ setID(id);
}
bool FlatSignal::emit(const string& channel) const
@@ -134,13 +135,9 @@ bool FlatListener::checkInFilters(const std::string& filter) const
void FlatListener::execute(const FlatSignal& sig)
{
- if (!sig.getID().empty())
- {
- if (!checkInFilters(sig.getID()))
- return;
- }
-
- callback(sig.sender, sig.data);
+ if ((!sig.getID().empty() && checkInFilters(sig.getID())) ||
+ (sig.getID().empty() && filters.empty()))
+ callback(sig.sender, sig.data);
}
void FlatListener::addFilter(const std::string& f)
diff --git a/engine/flattask.cpp b/engine/flattask.cpp
index b71f9d7..02e584e 100644
--- a/engine/flattask.cpp
+++ b/engine/flattask.cpp
@@ -1,23 +1,53 @@
#include "flattask.h"
+bool task_prior::operator()(task_s* s, task_s* g) const
+{
+ return s->getPriority() <= g->getPriority();
+}
+
/* Static variable definition */
-std::list<task_s*> task_s::tasks;
+task_set task_s::pre_process_tasks;
+task_set task_s::post_process_tasks;
+
+task_s::task_s(bool pre_process, Uint8 priority)
-task_s::task_s()
+ : pre_process(pre_process), priority(priority)
{
/* Push into the public callback list */
- tasks.push_back(this);
+ if (pre_process)
+ pre_process_tasks.insert(this);
+ else
+ post_process_tasks.insert(this);
}
task_s::~task_s()
{
/* Remove from the public callback list */
- tasks.remove(this);
+ if (pre_process)
+ pre_process_tasks.erase(this);
+ else
+ post_process_tasks.erase(this);
+}
+
+Uint8 task_s::getPriority() const
+{
+ return priority;
+}
+
+void task_s::setPriority(Uint8 priority)
+{
+ this->priority = priority;
+}
+
+void task_s::executePreProcess()
+{
+ for (task_s * task : task_s::pre_process_tasks)
+ task->exec();
}
-void task_s::executeAll()
+void task_s::executePostProcess()
{
- for (task_s * task : task_s::tasks)
+ for (task_s * task : task_s::post_process_tasks)
task->exec();
}
diff --git a/engine/flatwindow.cpp b/engine/flatwindow.cpp
index cbff905..ff17274 100644
--- a/engine/flatwindow.cpp
+++ b/engine/flatwindow.cpp
@@ -166,7 +166,7 @@ void FlatWindow::key_cb(const SDL_KeyboardEvent *event)
close();
/* Say flatland to quit */
- FlatSignal quit(this, 0, 0xff);
+ FlatSignal quit(this, "quit", 0, 0xff);
quit.emit("core");
}
}
diff --git a/engine/include/flatevolvable.h b/engine/include/flatevolvable.h
index 526599a..71aaf0f 100644
--- a/engine/include/flatevolvable.h
+++ b/engine/include/flatevolvable.h
@@ -3,7 +3,7 @@
#include "flatobject.h"
-struct task_s;
+class task_s;
class FlatEvolvable : virtual public FlatObject
{
@@ -17,7 +17,7 @@ public:
/* Evolution fields */
- void evolve_task(void*);
+ void evolve_task(float*);
virtual void evolve(float dt) = 0;
diff --git a/engine/include/flatserial.h b/engine/include/flatserial.h
index 41c6b97..f034690 100644
--- a/engine/include/flatserial.h
+++ b/engine/include/flatserial.h
@@ -11,6 +11,7 @@ class task_s;
struct SDL_EventCollector
{
task_s * checker;
+ task_s * eraser;
/* Keyboard event */
std::vector<SDL_Event> keyboard;
@@ -30,6 +31,7 @@ struct SDL_EventCollector
~SDL_EventCollector();
void collect(void*);
+ void erase(void*);
const std::vector<SDL_Event>& getStack(Uint32 id) const;
};
diff --git a/engine/include/flatsignal.h b/engine/include/flatsignal.h
index fa403e2..a1d7ec3 100644
--- a/engine/include/flatsignal.h
+++ b/engine/include/flatsignal.h
@@ -23,7 +23,10 @@ public:
void * data;
Uint8 priority;
- FlatSignal(FlatObject * sender, void *data = 0, Uint8 priority = 5);
+ FlatSignal( FlatObject * sender,
+ const std::string& id = "",
+ void *data = 0,
+ Uint8 priority = 5);
/* Alias to SignalChannel::emit() */
bool emit(const std::string& channel) const;
diff --git a/engine/include/flattask.h b/engine/include/flattask.h
index b470737..3f5c475 100644
--- a/engine/include/flattask.h
+++ b/engine/include/flattask.h
@@ -1,41 +1,64 @@
#ifndef __FLAT_TASK_H__
#define __FLAT_TASK_H__
-#include <list>
+#include <set>
+#include "types.h"
+
+struct task_prior
+{
+ bool operator()(task_s* s, task_s* g) const;
+};
+
+typedef std::set<task_s*, task_prior> task_set;
class task_s
{
+ /* Call priority of a task */
+ /* 0 = maximum priority */
+ /* 5 = default priority */
+ Uint8 priority;
+ bool pre_process;
/* Callback list */
- static std::list<task_s*> tasks;
+ static task_set pre_process_tasks;
+ static task_set post_process_tasks;
public:
- task_s();
+ task_s(bool pre_process = false, Uint8 priority = 5);
virtual ~task_s();
virtual void exec() = 0;
- /* Execute all tasks */
- static void executeAll();
+ Uint8 getPriority() const;
+
+ void setPriority(Uint8 priority);
+
+ /* Execute tasks */
+ static void executePreProcess();
+ static void executePostProcess();
};
-template<class T>
+template<class T, typename arg_t = void*>
class FlatTask : public task_s
{
- typedef void (T::*callback_t)(void*);
+ typedef void (T::*callback_t)(arg_t);
T * object;
callback_t callback;
- void * args;
+ arg_t args;
public:
- FlatTask(T *object, callback_t callback, void* args)
+ FlatTask( T *object,
+ callback_t callback,
+ arg_t args,
+ bool pre_process = false,
+ Uint8 priority = 5)
- : task_s(), object(object), callback(callback), args(args) {}
+ : task_s(pre_process, priority), object(object), callback(callback), args(args) {}
virtual void exec() override
{
diff --git a/engine/include/flatwindow.h b/engine/include/flatwindow.h
index eb113c1..11d7134 100644
--- a/engine/include/flatwindow.h
+++ b/engine/include/flatwindow.h
@@ -34,7 +34,7 @@ class FlatLayer;
class SDL_KeyEvent;
-class FlatWindow : public FlatObject, virtual public KeyFocusable
+class FlatWindow : virtual public FlatObject, public KeyFocusable
{
std::string title;
window_status status;
diff --git a/engine/include/serial/focusable.h b/engine/include/serial/focusable.h
index 325f84b..6478ed4 100644
--- a/engine/include/serial/focusable.h
+++ b/engine/include/serial/focusable.h
@@ -1,12 +1,13 @@
#ifndef __FOCUSABLE_H__
#define __FOCUSABLE_H__
+#include "flatobject.h"
#include "types.h"
class task_s;
union SDL_Event;
-class Focusable
+class Focusable : virtual public FlatObject
{
bool focused;
diff --git a/engine/include/serial/keyfocusable.h b/engine/include/serial/keyfocusable.h
index 02b3361..c97c791 100644
--- a/engine/include/serial/keyfocusable.h
+++ b/engine/include/serial/keyfocusable.h
@@ -5,7 +5,7 @@
struct SDL_KeyboardEvent;
-class KeyFocusable : public Focusable
+class KeyFocusable : virtual public Focusable
{
virtual void serial_cb(const SDL_Event*) override;
diff --git a/goals b/goals
index 84cc196..a3c754f 100644
--- a/goals
+++ b/goals
@@ -9,15 +9,15 @@
- Test two: open window and close with keyboard *
-- Exceptions
-- Priorities on FlatTask
-- FlatEvent (all)
+- Exceptions *
+- Priorities on FlatTask *
-- Test three: manage multiple events and possible vulnerability
+- Test three: manage multiple events and test possible vulnerability *
- Fixes
- FlatLayer
- FlatSprite
+- FlatEvent (all)
- Test four: animation test
diff --git a/test/main.cpp b/test/main.cpp
index 2b16838..0edd682 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -2,30 +2,119 @@
#include "flatwindow.h"
#include "exceptions/forcequit.h"
+#include "serial/keyfocusable.h"
+#include <SDL2/SDL.h>
+
+#include <iostream>
+#include <string>
+
+#include "flatsignal.h"
+
+using namespace std;
+
int count = 0;
void loop(float);
+class KeyPrinter : public KeyFocusable
+{
+ virtual void key_cb(const SDL_KeyboardEvent* key) override
+ {
+ string msg = (key->type == SDL_KEYDOWN) ? "pressed" : "released";
+ cout << "KeyPrinter: key " << (char)key->keysym.sym << " " << msg << endl;
+ };
+
+public:
+
+ using KeyFocusable::KeyFocusable;
+};
+
+class KeyCaller : public KeyFocusable
+{
+ int counter;
+ char last;
+
+ virtual void key_cb(const SDL_KeyboardEvent* key) override
+ {
+ if (key->type == SDL_KEYDOWN) {
+
+ char c = key->keysym.sym;
+
+ if (c == last)
+ ++counter;
+ else {
+ last = c;
+ counter = 0;
+ cout << "KeyCaller: Counter set to 0" << endl;
+ }
+ }
+
+ if (counter == 20)
+ {
+ FlatSignal signal(this, "char", (void*)&last);
+ signal.emit("alpha");
+
+ cout << "KeyCaller: 20 times reached" << endl;
+ cout << "KeyCaller: sending " << last << endl;
+ }
+
+ if (counter == 50)
+ {
+ cout << "KeyCaller: 50 times pressed " << last << endl;
+
+ /* Exit application */
+ FlatSignal signal(this, "quit");
+ signal.emit("core");
+ }
+ };
+
+public:
+
+ KeyCaller() : counter(0), last('\0')
+ {
+ setID("CALLER");
+ }
+};
+
+class TwentyListener : public FlatListener
+{
+ virtual void callback(FlatObject *sender, void *data) override
+ {
+ char c = *(char*)data;
+ cout << "TwentyListener: received " << c << " from " << sender->getID() << endl;
+ }
+
+public:
+
+ TwentyListener() : FlatListener({"char"}) {}
+};
+
+KeyPrinter printer;
+KeyCaller caller;
+TwentyListener twlistener;
+
+SignalChannel alpha("alpha");
+
int main()
{
- FlatWindow win(600, 900, "Stocazzo");
+ FlatWindow win(600, 900, "Test 3");
flat_status status;
+ /* Connect listener to alpha channel */
+ alpha.connect(&twlistener);
+
init_flatland(&win, loop, status, 60);
return 0;
}
-#include <iostream>
-
-using namespace std;
-
void loop(float dt)
{
++count;
- if (count == 1000)
- throw ForceQuit("1000 steps reached");
+ if (count == 10000)
+ throw ForceQuit("10000 steps reached");
- cout << "Loop number: " << count << endl;
+ //cout << "Loop number: " << count << endl;
}
+
diff --git a/test/test b/test/test
index 0f34b3c..364259d 100755
--- a/test/test
+++ b/test/test
Binary files differ
diff --git a/test/test2.cpp b/test/test2.cpp
new file mode 100644
index 0000000..2b16838
--- /dev/null
+++ b/test/test2.cpp
@@ -0,0 +1,31 @@
+#include "flatland.h"
+#include "flatwindow.h"
+#include "exceptions/forcequit.h"
+
+int count = 0;
+
+void loop(float);
+
+int main()
+{
+ FlatWindow win(600, 900, "Stocazzo");
+ flat_status status;
+
+ init_flatland(&win, loop, status, 60);
+
+ return 0;
+}
+
+#include <iostream>
+
+using namespace std;
+
+void loop(float dt)
+{
+ ++count;
+
+ if (count == 1000)
+ throw ForceQuit("1000 steps reached");
+
+ cout << "Loop number: " << count << endl;
+}