Rise
The Vieneo Province
Loading...
Searching...
No Matches
ModuleScheduler.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4#include <cstddef>
5#include <vector>
6
7// One module per simulation frame. Timers receive time since that module's
8// previous turn, while sensors can distinguish scheduling from a stalled frame.
10{
11 struct Pending { double seconds = 0; float maxFrameSeconds = 0; };
12 std::vector<Pending> pending;
13 size_t next = 0;
14public:
15 void Reset() { pending.clear(); next = 0; }
16 template<class Dispatch> void Advance(float frameSeconds, size_t count, Dispatch dispatch)
17 {
18 if (!count || !std::isfinite(frameSeconds) || frameSeconds < 0)
19 { Reset(); return; }
20 if (pending.size() != count) { pending.assign(count, {}); next = 0; }
21 if (frameSeconds == 0) return;
22 for (auto& time : pending)
23 {
24 time.seconds += frameSeconds;
25 time.maxFrameSeconds = (std::max)(time.maxFrameSeconds, frameSeconds);
26 }
27 const auto time = pending[next];
28 pending[next] = {};
29 const size_t index = next;
30 next = (next + 1) % count;
31 dispatch(index, float(time.seconds), time.maxFrameSeconds);
32 }
33};
std::vector< Pending > pending
void Advance(float frameSeconds, size_t count, Dispatch dispatch)