Zappy Server
C++ game server: world, rules, scheduler, network
Loading...
Searching...
No Matches
Scheduler.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <chrono>
4#include <functional>
5#include <queue>
6
8struct TimedEvent {
9 std::chrono::steady_clock::time_point fireAt;
10 std::function<void()> callback;
11 bool operator>(const TimedEvent& other) const { return fireAt > other.fireAt; }
12};
13
23class Scheduler {
24 public:
25 void schedule(std::chrono::milliseconds delay, std::function<void()> cb);
26 void tick();
27
29 void clear();
30
35 int msUntilNext() const;
36
43 void rescale(float ratio);
44
45 private:
46 std::priority_queue<TimedEvent, std::vector<TimedEvent>, std::greater<TimedEvent>> _queue;
47};
Priority queue of timed callbacks. Drives all game timing.
Definition Scheduler.hpp:23
void schedule(std::chrono::milliseconds delay, std::function< void()> cb)
Definition Scheduler.cpp:3
void clear()
Drop all pending events (e.g. on game end, to freeze the world instantly).
Definition Scheduler.cpp:27
std::priority_queue< TimedEvent, std::vector< TimedEvent >, std::greater< TimedEvent > > _queue
Definition Scheduler.hpp:46
int msUntilNext() const
Milliseconds until the next event. Pass directly to poll() as timeout. Returns -1 if the queue is emp...
Definition Scheduler.cpp:18
void rescale(float ratio)
Rescale all pending fire times by ratio.
Definition Scheduler.cpp:32
void tick()
Definition Scheduler.cpp:8
A callback and the time it should fire. Min-heap ordered by fireAt.
Definition Scheduler.hpp:8
std::function< void()> callback
Definition Scheduler.hpp:10
std::chrono::steady_clock::time_point fireAt
Definition Scheduler.hpp:9
bool operator>(const TimedEvent &other) const
Definition Scheduler.hpp:11