forked from FrictionalGames/AmnesiaTheDarkDescent
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: correctly handle closing the game window
Signed-off-by: Michael Pollind <[email protected]>
- Loading branch information
Showing
18 changed files
with
543 additions
and
525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
#pragma once | ||
|
||
#include "engine/Event.h" | ||
#include "engine/Interface.h" | ||
#include "engine/UpdateEventLoop.h" | ||
|
||
namespace hpl { | ||
|
||
/** | ||
* Events are queued and processed on the event loop. | ||
*/ | ||
template<typename... Params> | ||
class QueuedEventLoopHandler | ||
{ | ||
public: | ||
using TargetEvent = hpl::Event<Params...>; | ||
|
||
struct Options { | ||
public: | ||
/** | ||
* called before processing queued events | ||
*/ | ||
std::function<void()> onBegin = [](){}; | ||
/** | ||
* called after all queued events have been processed | ||
*/ | ||
std::function<void()> onEnd = [](){}; | ||
/** | ||
* a filter function that returns true if the event should be queued | ||
*/ | ||
std::function<bool(Params...)> filter = [](Params...){ return true; }; | ||
}; | ||
inline QueuedEventLoopHandler(BroadcastEvent event, TargetEvent& targetEvent, TargetEvent::Callback callback, const Options options = Options {}): | ||
m_dispatchHandler([&, options, callback](float value) { | ||
std::lock_guard<std::mutex> lock(m_mutex); | ||
options.onBegin(); | ||
while(!m_queuedEvents.empty()) { | ||
auto& data = m_queuedEvents.front(); | ||
std::apply(callback, data); | ||
m_queuedEvents.pop(); | ||
} | ||
options.onEnd(); | ||
}), | ||
m_handler([&, options](Params... params) { | ||
std::lock_guard<std::mutex> lock(m_mutex); | ||
if(options.filter(params...)) { | ||
m_queuedEvents.emplace(std::tuple<Params...>(params...)); | ||
} | ||
}) | ||
{ | ||
Interface<IUpdateEventLoop>::Get()->Subscribe(event, m_dispatchHandler); | ||
m_handler.Connect(targetEvent); | ||
} | ||
|
||
QueuedEventLoopHandler(const QueuedEventLoopHandler&) = delete; | ||
QueuedEventLoopHandler(QueuedEventLoopHandler&&) = delete; | ||
QueuedEventLoopHandler& operator=(const QueuedEventLoopHandler&) = delete; | ||
QueuedEventLoopHandler& operator=(QueuedEventLoopHandler&&) = delete; | ||
private: | ||
hpl::IUpdateEventLoop::UpdateEvent::Handler m_dispatchHandler; | ||
hpl::Event<Params...>::Handler m_handler; | ||
std::queue<std::tuple<typename std::remove_reference<Params>::type...>> m_queuedEvents; | ||
std::mutex m_mutex; | ||
}; | ||
|
||
} // namespace hpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.