summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2019-01-26 23:32:43 +0100
committerNao Pross <naopross@thearcway.org>2019-01-26 23:32:43 +0100
commitbb6beb5feadfd3ac252fb0bd0b533ceda960c7e7 (patch)
tree614cada7598eb7219fb3bcf1a1bdf5052a3d9457
parentRestore old files to ease merge (flatland, signal, window) (diff)
parentTesting signal (diff)
downloadflatland-bb6beb5feadfd3ac252fb0bd0b533ceda960c7e7.tar.gz
flatland-bb6beb5feadfd3ac252fb0bd0b533ceda960c7e7.zip
Merge remote-tracking branch 'raffa/master'
-rw-r--r--engine/flatland.cpp110
-rw-r--r--engine/include/bound.hpp2
-rw-r--r--engine/include/collector.hpp4
-rw-r--r--engine/include/component.hpp4
-rw-r--r--engine/include/core/object.hpp12
-rw-r--r--engine/include/core/signal.hpp22
-rw-r--r--engine/include/flatland.hpp13
-rw-r--r--engine/include/object.hpp9
-rw-r--r--engine/include/serial/focusable.hpp4
-rw-r--r--engine/include/surface.hpp4
-rw-r--r--engine/include/types.hpp5
-rw-r--r--engine/include/window.hpp4
-rw-r--r--engine/serial.cpp2
-rw-r--r--engine/signal.cpp22
-rw-r--r--engine/surface.cpp5
-rw-r--r--engine/window.cpp2
m---------lib/libwrapsdl20
m---------lib/libwsdl20
-rw-r--r--ninja/rules.ninja7
-rw-r--r--test/signal_test.cpp114
20 files changed, 280 insertions, 65 deletions
diff --git a/engine/flatland.cpp b/engine/flatland.cpp
index f761473..da0f58d 100644
--- a/engine/flatland.cpp
+++ b/engine/flatland.cpp
@@ -12,23 +12,52 @@ using namespace flat;
#include "core/task.hpp"
#include "core/signal.hpp"
+#include "object.hpp"
#include "window.hpp"
#include "exception.hpp"
#include "exceptions/forcequit.hpp"
-float flatland_dt;
+/* Global fields definitions */
-set<flat::core::object*> objects;
+float flatland_dt;
FlatWindow * window = 0;
-gameloop loop_function;
-
flat_status status;
float fps;
float fps_pause = 5;
+core::job mainsync_job;
+
+core::channel core_chan("core");
+
+core::listener::ptr quit_listener;
+core::listener::ptr print_listener;
+
+/* channels listeners callback */
+
+void quit_callback(const object *sender, core::signal::package)
+{
+ cout << "Flatland: quit request attempted" << endl;
+ flat::quit();
+}
+
+void print_callback(const object *sender, core::signal::package p)
+{
+ const char * out = p.get<const char>();
+ string * sout = p.get<string>();
+
+ if (!out)
+ cout << "Flatland: " << out << endl;
+ else if (!sout)
+ cout << "Flatland: " << sout << endl;
+
+ // else fail silently
+}
+
+/* Functions implementation */
+
uint32_t status_to_flags(const flat_status& s)
{
uint32_t flags = 0;
@@ -57,16 +86,67 @@ uint32_t status_to_flags(const flat_status& s)
return flags;
}
-int init_flatland(FlatWindow* w, gameloop loop, const flat_status& s, float _fps)
+/* Accessors */
+
+core::channel& flat::core_channel()
+{
+ return core_chan;
+}
+
+core::job& flat::main_job()
+{
+ return mainsync_job;
+}
+
+/* Main loop */
+
+int flat::init_flatland(FlatWindow* w, const flat_status& s, float _fps)
{
cout << "Flatland: Initializing flatland" << endl;
+ // init core channel
+
+ cout << "Flatland: Initializing core channel" << endl;
+
+ core_chan.start(core::priority_t::max);
+
+ if (!core_chan.map()) {
+
+ cout << "Flatland: Could not map 'core' channel" << endl;
+ cout << "Flatland: Do not call another channel 'core' before flatland initialization" << endl;
+ cout << "Flatland: Aborting" << endl;
+ return -1;
+ }
+
+ // bind listeners
+
+ quit_listener = core_chan.connect(quit_callback, initializer_list<string>({string("quit")}));
+
+ // control if quit was not already connected
+ if (!quit_listener) {
+
+ cout << "Flatland: Could not connect 'quit' listener" << endl;
+ cout << "Flatland: Do not connect to core channel another listener with filter name 'quit' before flatland initialization" << endl;
+ cout << "Flatland: Aborting" << endl;
+ return -1;
+ }
+
+ print_listener = core_chan.connect(print_callback, initializer_list<string>({string("print")}));
+
+ // control if print was not already connected
+ if (!print_listener) {
+
+ cout << "Flatland: Could not connect 'print' listener" << endl;
+ cout << "Flatland: Do not connect to core channel another listener with filter name 'print' before flatland initialization" << endl;
+ cout << "Flatland: Aborting" << endl;
+ return -1;
+ }
+
// init variables
cout << "Flatland: Initializing window" << endl;
window = w;
- loop_function = loop;
status = s;
fps = _fps;
@@ -106,23 +186,15 @@ int init_flatland(FlatWindow* w, gameloop loop, const flat_status& s, float _fps
try {
- try {
-
- /* Execute loop function */
- loop_function(flatland_dt);
-
- } catch (const exception &e) {
-
- cerr << "Flatland: exception thrown while executing loop" << endl;
- cerr << e.what() << endl;
- }
+ /* Invoke main sync job tasks */
+ mainsync_job();
} catch (const ForceQuit& f) {
cerr << "Flatland: a force quit call was thrown" << endl;
cerr << "Possible reason: " << f.reason << endl;
- quit_flatland();
+ quit();
}
SDL_Delay((uint32_t) (1000.0f / fps));
@@ -146,13 +218,13 @@ int init_flatland(FlatWindow* w, gameloop loop, const flat_status& s, float _fps
return status.error;
}
-void quit_flatland()
+void flat::quit()
{
status.running = 0;
status.loop = 0;
}
-flat_status flatland_status()
+flat_status flat::flatland_status()
{
return status;
}
diff --git a/engine/include/bound.hpp b/engine/include/bound.hpp
index 9b7b755..a924e90 100644
--- a/engine/include/bound.hpp
+++ b/engine/include/bound.hpp
@@ -8,7 +8,7 @@ namespace flat {
typedef SVector<int, 2> pixel;
-class FlatBound : virtual public flat::core::object
+class FlatBound : virtual public flat::object
{
pixel location;
diff --git a/engine/include/collector.hpp b/engine/include/collector.hpp
index bc50f4d..45a3e74 100644
--- a/engine/include/collector.hpp
+++ b/engine/include/collector.hpp
@@ -1,12 +1,12 @@
#ifndef __FLATCOLLECTOR_H__
#define __FLATCOLLECTOR_H__
-#include "core/object.hpp"
+#include "object.hpp"
#include <set>
namespace flat {
-class FlatCollector : virtual public flat::core::object
+class FlatCollector : virtual public flat::object
{
FlatCollector * parent;
diff --git a/engine/include/component.hpp b/engine/include/component.hpp
index 4ad99c8..fdde28d 100644
--- a/engine/include/component.hpp
+++ b/engine/include/component.hpp
@@ -1,13 +1,13 @@
#ifndef __FLAT_COMPONENT_H__
#define __FLAT_COMPONENT_H__
-#include "core/object.hpp"
+#include "object.hpp"
#include "core/labelled.hpp"
#include <string>
namespace flat {
-class component : virtual public core::object, virtual public core::labelled
+class component : virtual public object, virtual public core::labelled
{
component * m_parent;
diff --git a/engine/include/core/object.hpp b/engine/include/core/object.hpp
deleted file mode 100644
index cd5da5e..0000000
--- a/engine/include/core/object.hpp
+++ /dev/null
@@ -1,12 +0,0 @@
-#pragma once
-
-namespace flat
-{
- namespace core
- {
- struct object
- {
- // it exists
- };
- }
-}
diff --git a/engine/include/core/signal.hpp b/engine/include/core/signal.hpp
index 8a3d639..606d61a 100644
--- a/engine/include/core/signal.hpp
+++ b/engine/include/core/signal.hpp
@@ -4,9 +4,9 @@
#include <list>
#include <set>
#include <initializer_list>
-#include "object.hpp"
#include "task.hpp"
#include "types.hpp"
+#include "object.hpp"
#include <functional>
#include <memory>
#include "priority.hpp"
@@ -14,6 +14,8 @@
namespace flat
{
+ //class object;
+
namespace core
{
@@ -29,7 +31,7 @@ namespace flat
template<class T>
T * get() {
- return dynamic_cast<T>(data);
+ return reinterpret_cast<T*>(data);
}
void * data;
@@ -70,8 +72,7 @@ namespace flat
template<typename R, typename T>
static ptr create( R T::*mf,
T& obj,
- const std::initializer_list<std::string>& filters = {})
- {
+ const std::initializer_list<std::string>& filters = {}) {
return std::make_shared<listener>(std::bind(mf, obj), filters);
}
@@ -118,10 +119,21 @@ namespace flat
bool connect(listener* l);
void disconnect(listener* l);
+
+ listener::ptr connect(listener::callback f,
+ const std::initializer_list<std::string>& filters = {});
+
+ template<typename R, typename T>
+ inline listener::ptr connect(R T::*mf, T& obj,
+ const std::initializer_list<std::string>& filters = {})
+ {
+ using namespace std::placeholders;
+ return connect(std::bind(mf, obj, _1, _2), filters);
+ }
static ptr find(const std::string&);
- static ptr create(const std::string& id, priority_t prior);
+ static ptr create(const std::string& id, priority_t prior = priority_t::none);
void check_and_call();
};
diff --git a/engine/include/flatland.hpp b/engine/include/flatland.hpp
index 331093b..f12dc65 100644
--- a/engine/include/flatland.hpp
+++ b/engine/include/flatland.hpp
@@ -5,8 +5,6 @@ namespace flat {
class FlatWindow;
-typedef void (*gameloop)(float);
-
struct flat_status
{
@@ -37,8 +35,8 @@ struct flat_status
unsigned char loop:1;
};
-int init_flatland(FlatWindow*, gameloop, const flat_status&, float fps = 60);
-void quit_flatland();
+int init_flatland(FlatWindow*, const flat_status&, float fps = 60);
+void quit();
namespace core {
@@ -49,12 +47,11 @@ namespace core {
/* Engine channels */
-core::channel& core_chan();
-core::channel& error_chan();
+core::channel& core_channel();
/* Main job access */
-core::job& game_job();
+core::job& main_job();
/* Window and status accessors */
@@ -64,7 +61,7 @@ flat_status flatland_status();
/* Window and status modifiers */
-void load_flatland_status(const flat_status&);
+//void load_flatland_status(const flat_status&);
}
diff --git a/engine/include/object.hpp b/engine/include/object.hpp
new file mode 100644
index 0000000..70a7a2d
--- /dev/null
+++ b/engine/include/object.hpp
@@ -0,0 +1,9 @@
+#pragma once
+
+namespace flat
+{
+ struct object
+ {
+ // it exists
+ };
+}
diff --git a/engine/include/serial/focusable.hpp b/engine/include/serial/focusable.hpp
index f139577..8a05051 100644
--- a/engine/include/serial/focusable.hpp
+++ b/engine/include/serial/focusable.hpp
@@ -1,14 +1,14 @@
#ifndef __FOCUSABLE_H__
#define __FOCUSABLE_H__
-#include "core/object.hpp"
+#include "object.hpp"
#include "types.hpp"
union SDL_Event;
namespace flat {
-class Focusable : virtual public flat::core::object
+class Focusable : virtual public flat::object
{
bool focused;
diff --git a/engine/include/surface.hpp b/engine/include/surface.hpp
index 5364e8e..6e9026a 100644
--- a/engine/include/surface.hpp
+++ b/engine/include/surface.hpp
@@ -1,13 +1,13 @@
#ifndef __FLATSURFACE_H__
#define __FLATSURFACE_H__
-#include "core/object.hpp"
+#include "object.hpp"
#include "core/labelled.hpp"
#include <SDL2/SDL.h>
namespace flat {
-class surface : virtual public core::object, virtual public core::labelled
+class surface : virtual public object, virtual public core::labelled
{
SDL_Surface * sdl_surface;
SDL_Surface * parent;
diff --git a/engine/include/types.hpp b/engine/include/types.hpp
index e7a1d3a..31596c9 100644
--- a/engine/include/types.hpp
+++ b/engine/include/types.hpp
@@ -5,10 +5,7 @@
namespace flat
{
- namespace core
- {
- class object;
- }
+ class object;
class FlatActor;
class FlatSprite;
diff --git a/engine/include/window.hpp b/engine/include/window.hpp
index 98e8e04..48e8cb0 100644
--- a/engine/include/window.hpp
+++ b/engine/include/window.hpp
@@ -1,7 +1,7 @@
#ifndef __FLATWINDOW_H__
#define __FLATWINDOW_H__
-#include "core/object.hpp"
+#include "object.hpp"
#include "serial/keyfocusable.hpp"
#include <string>
@@ -37,7 +37,7 @@ struct window_status
class FlatLayer;
-class FlatWindow : virtual public core::object, public KeyFocusable
+class FlatWindow : virtual public object, public KeyFocusable
{
std::string title;
window_status status;
diff --git a/engine/serial.cpp b/engine/serial.cpp
index b3218be..ad29f2f 100644
--- a/engine/serial.cpp
+++ b/engine/serial.cpp
@@ -93,6 +93,8 @@ const std::vector<SDL_Event>& SDL_EventCollector::getStack(uint32_t id) const
// TODO other events
}
+
+ return user;
}
SDL_EventCollector * FlatSerial::collector = new SDL_EventCollector();
diff --git a/engine/signal.cpp b/engine/signal.cpp
index 8546094..5434469 100644
--- a/engine/signal.cpp
+++ b/engine/signal.cpp
@@ -1,4 +1,5 @@
#include "core/signal.hpp"
+#include "object.hpp"
#include <functional>
#include "flatland.hpp"
@@ -21,7 +22,7 @@ channel::~channel()
void channel::start(priority_t prior)
{
// Initialize task
- checker = flat::game_job().delegate_task(&channel::check_and_call, *this, prior);
+ checker = flat::main_job().delegate_task(&channel::check_and_call, *this, prior);
}
bool channel::map()
@@ -40,8 +41,11 @@ bool channel::map()
return mapped;
}
+#include <iostream>
+
void channel::emit(const signal& sig)
{
+ //cout << "React" << endl;
stack.insert(sig);
}
@@ -82,6 +86,16 @@ void channel::disconnect(listener* l)
disconnect(pt);
}
+listener::ptr channel::connect(listener::callback f, const std::initializer_list<std::string>& filters)
+{
+ listener::ptr lis = std::make_shared<listener>(f, filters);
+
+ if (connect(lis))
+ return lis;
+
+ return nullptr;
+}
+
channel::ptr channel::create(const string& id, priority_t prior)
{
ptr out = std::make_shared<channel>(id);
@@ -106,8 +120,14 @@ channel::ptr channel::find(const string& id)
void channel::check_and_call()
{
+ //std::cout << "Signal check" << std::endl;
+ // check
+ //std::cout << "Stack size: " << stack.size() << endl;
+
if (!stack.empty()) {
+ std::cout << "Signal detected" << std::endl;
+
vector<weak_ptr<listener>> to_erase;
// TODO, maybe it exists pop
diff --git a/engine/surface.cpp b/engine/surface.cpp
index 642b0a7..3ea38d4 100644
--- a/engine/surface.cpp
+++ b/engine/surface.cpp
@@ -32,7 +32,7 @@ surface::surface(const char *filename, uint32_t format, SDL_Surface *parent)
}
surface::surface(SDL_Surface *sdl_surface, SDL_Surface *parent)
- : core::object(), parent(parent), hide(false)
+ : flat::object(), parent(parent), hide(false)
{
this->sdl_surface = new SDL_Surface(*sdl_surface);
@@ -52,8 +52,7 @@ surface::surface(SDL_Surface *sdl_surface, SDL_Surface *parent)
}
surface::surface(const surface &sprite)
- : core::object(sprite), parent(sprite.parent),
- hide(sprite.hide)
+ : flat::object(sprite), parent(sprite.parent), hide(sprite.hide)
{
offset = new SDL_Rect(*sprite.offset);
diff --git a/engine/window.cpp b/engine/window.cpp
index 4f22310..20da675 100644
--- a/engine/window.cpp
+++ b/engine/window.cpp
@@ -46,7 +46,7 @@ FlatWindow::FlatWindow( int width, int height,
FlatWindow::FlatWindow(const FlatWindow& win)
- : flat::core::object(win),
+ : flat::object(win),
title(win.title), status(win.status),
sdl_window(0), screen(0)
{
diff --git a/lib/libwrapsdl2 b/lib/libwrapsdl2
new file mode 160000
+Subproject 736ec71fce673d5aa88228b96acfe6c6862a223
diff --git a/lib/libwsdl2 b/lib/libwsdl2
-Subproject 71b593ebd1a015a4cb9bfe0eebafc78d897ff28
+Subproject 736ec71fce673d5aa88228b96acfe6c6862a223
diff --git a/ninja/rules.ninja b/ninja/rules.ninja
index 51c3a8c..a2f3b56 100644
--- a/ninja/rules.ninja
+++ b/ninja/rules.ninja
@@ -7,7 +7,8 @@ cflags = $cflags -Wpointer-arith -Winit-self -Wshadow -Wswitch-enum
cflags = $cflags -Wredundant-decls -Wfloat-equal -Wundef -Wvla
cflags = $cflags -Wconversion
-libs = -lSDL2 lib/libmm/build/libmm.a lib/libwsdl2/build/libwsdl2.a
+libs = -lSDL2
+#lib/libmm/build/libmm.a lib/libwsdl2/build/libwsdl2.a
lflags = $libs
flags = -fdiagnostics-color
@@ -16,6 +17,10 @@ rule mkdir
command = mkdir -p $out
description = creating directory $out
+rule clean
+ command = rm -r build/*
+ description = clean up builds
+
rule cpp
command = g++ $flags $cflags -c $in -o $out
description = compiling $in
diff --git a/test/signal_test.cpp b/test/signal_test.cpp
new file mode 100644
index 0000000..db5b571
--- /dev/null
+++ b/test/signal_test.cpp
@@ -0,0 +1,114 @@
+#include "core/signal.hpp"
+#include "core/task.hpp"
+#include "object.hpp"
+#include "window.hpp"
+#include "flatland.hpp"
+#include "exceptions/forcequit.hpp"
+
+#include <iostream>
+
+using namespace std;
+using namespace flat;
+using namespace flat::core;
+
+
+class sender : public object
+{
+ const char * message;
+ channel::ptr chan;
+
+public:
+
+ sender(const char * message, channel::ptr chan) : message(message), chan(chan)
+ {
+ }
+
+ void send()
+ {
+ signal msg(this, "", (void*)message);
+ chan->emit(msg);
+ }
+};
+
+void function_listener(const object*, core::signal::package msg)
+{
+ cout << "Funzione: " << msg.get<const char>() << endl;
+}
+
+class c_listener
+{
+
+ listener::ptr lis;
+
+public:
+
+ c_listener(channel::ptr chan)
+ {
+ lis = chan->connect(&c_listener::method_listener, *this);
+ }
+
+ void method_listener(const object *o, signal::package msg)
+ {
+ cout << "Metodo" << msg.get<const char>() << endl;
+ }
+};
+
+/* Objects definition */
+
+channel::ptr alpha;
+sender * m_sender;
+c_listener * m_listener;
+listener::ptr f_listener;
+
+int steps = 0;
+
+void lifeloop()
+{
+ if (!(steps % 10))
+ cout << "Step: " << steps << endl;
+
+ if (!(steps % 40))
+ m_sender->send();
+
+ if (++steps > 2000)
+ {
+ signal quit(0, "quit");
+
+ // quit request
+ flat::core_channel().emit(quit);
+ }
+
+ if (steps > 2100)
+ throw flat::ForceQuit("Too many steps");
+}
+
+int main()
+{
+ FlatWindow win(600, 900, "Test 3");
+ flat_status status;
+
+ alpha = channel::create("alpha");
+
+ if (alpha == nullptr)
+ {
+ cout << "Could not create channel alpha" << endl;
+ return -1;
+ }
+
+ // create sender
+ m_sender = new sender("Ciao", alpha);
+ m_listener = new c_listener(alpha);
+
+ // Connect listener to alpha channel
+ f_listener = alpha->connect(&function_listener);
+
+ // bind counter task
+ task::ptr looptask = flat::main_job().delegate_task(lifeloop);
+
+ init_flatland(&win, status, 60);
+
+ delete m_sender;
+ delete m_listener;
+
+ return 0;
+}