Mirai-Team / mirai-project

C++ library for games making purposes.
Other
3 stars 0 forks source link

Add an Event Manager #38

Closed Lomadriel closed 9 years ago

Lomadriel commented 9 years ago

Allows you to run a function in a class according to an action from another class. (Listener and Notifier system)

CBenoit commented 9 years ago

I assume that the system is pretty similar to the InputManager part, so I would like to signal exactly the same thing that I said for issue https://github.com/Mirai-Team/mirai-project/issues/43 about parameters.

Lomadriel commented 9 years ago

nop. It's no the same thing. AddListener(string, function). Broadcast(string, param...)

Lomadriel commented 9 years ago

Added with parameter in EventManager2. Need a overhaul but it's functional.

CBenoit commented 9 years ago

Exemple not compiling on my computer :

[ 77%] Built target EncryptionExample
[ 80%] Building CXX object examples/eventManager/CMakeFiles/EventManagerExample.dir/src/main.cpp.o
In file included from /home/benoit/Dev/mirai-project/include/MiraiProject/eventManager/EventManager.hpp:111:0,
                 from /home/benoit/Dev/mirai-project/examples/eventManager/src/main.cpp:28:
/home/benoit/Dev/mirai-project/include/MiraiProject/eventManager/EventManager.tpp: In member function ‘T mp::EventManager::broadcast(std::string, Args ...) [with T = int; Args = {int}; std::string = std::basic_string<char>]’:
/home/benoit/Dev/mirai-project/include/MiraiProject/eventManager/EventManager.tpp:52:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
Lomadriel commented 9 years ago

Hum, you have only quoted a warning. So I don't know why the compilation fails.

But I want to update the example which is quite useless.

Lomadriel commented 9 years ago

Done

CBenoit commented 9 years ago

Uh, yes, It was just a warning and the executable was produced in fact. I didn't seen it at the moment.

The new example is better, but you should define Player class in another file like Monster.

About the EventManager class : There is probably a way to replace static std::shared_ptr<EventManager> instance_; by static EventManager instance_; Here https://github.com/Mirai-Team/mirai-project/blob/EventManager2/include/MiraiProject/eventManager/EventManager.hpp#L105

Then, you simply return a reference here : https://github.com/Mirai-Team/mirai-project/blob/EventManager2/include/MiraiProject/eventManager/EventManager.hpp#L47

Like in WindowManager class : https://github.com/Mirai-Team/mirai-project/blob/EventManager2/include/MiraiProject/util/WindowManager.hpp

It is better to not use pointers if possible.

Lomadriel commented 9 years ago

In this case we can't, it's a singleton pattern. Indeed there is a singleton pattern with reference but unsafe so I think it's better to use pointer

Lomadriel commented 9 years ago

This message replace the previous one. Indeed we can. But I don't if gcc supports it. I check it and if gcc support it. I do it.

CBenoit commented 9 years ago

I see no reason this way could works only for the very case of the WindowManager and not for this one. And why it would be unsafe to do it here ?

Lomadriel commented 9 years ago

WindowManager isn't singleton but I have to find a way to keep this class singleton and thread-safe.

CBenoit commented 9 years ago

But you can still return a reference. Using the shared_ptr is thread-safe ?

Lomadriel commented 9 years ago

If I return a reference like you do in WindowManager class, this class will no longer be singleton.

CBenoit commented 9 years ago

A reference isn't a copy, but an hidden pointer to the same instance. So I don't see why this is no longer a singleton.

Lomadriel commented 9 years ago

I know that but it's a little bit more complicated. I can do it like in WindowManager but if I do it the class is "singleton" but absolutely not thread-safe.