-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
388 additions
and
81 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
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
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,58 @@ | ||
// Copyright 2017-2017 the openage authors. See copying.md for legal info. | ||
|
||
#include "events.h" | ||
|
||
namespace openage { | ||
namespace curve { | ||
|
||
void EventQueue::addcallback (const curve_time_t &at, | ||
const EventQueue::condition &precond, | ||
const EventQueue::callback &trigger) { | ||
Event e; | ||
e.event = trigger; | ||
e.precond = precond; | ||
e.time = at; | ||
|
||
addcallback(e); | ||
} | ||
|
||
void EventQueue::addcallback (const curve_time_t &at, | ||
const EventQueue::condition_void &precond, | ||
const EventQueue::callback_void &trigger) { | ||
Event e; | ||
e.event = [trigger](const curve_time_t &) { trigger(); }; | ||
e.precond = [precond](const curve_time_t &) { return precond(); }; | ||
e.time = at; | ||
|
||
addcallback(e); | ||
} | ||
|
||
void EventQueue::addcallback (const curve_time_t &at, | ||
const EventQueue::callback &trigger) { | ||
this->addcallback(at, &EventQueue::_true, trigger); | ||
} | ||
|
||
void EventQueue::addcallback (const curve_time_t &at, | ||
const EventQueue::callback_void &trigger) { | ||
this->addcallback(at, &EventQueue::_true, [trigger](const curve_time_t &) { | ||
trigger(); | ||
}); | ||
} | ||
|
||
void EventQueue::addcallback (const EventQueue::Event &e) { | ||
queue.insert(e.time, e); | ||
} | ||
|
||
void EventQueue::trigger(const curve_time_t &from, const curve_time_t &to) { | ||
for (auto it = queue.between(from, to); it != queue.end(); ++it) { | ||
if (it.value().precond(it.value().time)) { | ||
it.value().event(it.value().time); | ||
} | ||
} | ||
} | ||
|
||
bool EventQueue::_true(const curve_time_t &) { | ||
return true; | ||
} | ||
|
||
}} // namespace openage::curve |
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,45 @@ | ||
// Copyright 2017-2017 the openage authors. See copying.md for legal info. | ||
|
||
#pragma once | ||
|
||
#include "curve.h" | ||
#include "queue.h" | ||
|
||
#include <functional> | ||
|
||
namespace openage { | ||
namespace curve { | ||
|
||
class EventQueue { | ||
|
||
public: | ||
typedef std::function<void(const curve_time_t &)> callback; | ||
typedef std::function<bool(const curve_time_t &)> condition; | ||
|
||
typedef std::function<void(void)> callback_void; | ||
typedef std::function<bool(void)> condition_void; | ||
|
||
class Event { | ||
public: | ||
callback event; | ||
condition precond; | ||
curve_time_t time; | ||
}; | ||
|
||
void addcallback (const curve_time_t &at, const condition &, const callback &); | ||
void addcallback (const curve_time_t &at, const condition_void &, const callback_void &); | ||
|
||
void addcallback (const curve_time_t &at, const callback &); | ||
void addcallback (const curve_time_t &at, const callback_void &); | ||
|
||
void addcallback (const Event &); | ||
|
||
void trigger(const curve_time_t &from, const curve_time_t &to); | ||
|
||
private: | ||
Queue<Event> queue; | ||
|
||
static bool _true(const curve_time_t &); | ||
}; | ||
|
||
}} // namespace openage::curve |
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.