Rise
The Vieneo Province
Toggle main menu visibility
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.
9
class
ModuleScheduler
10
{
11
struct
Pending
{
double
seconds
= 0;
float
maxFrameSeconds
= 0; };
12
std::vector<Pending>
pending
;
13
size_t
next
= 0;
14
public
:
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
};
ModuleScheduler
Definition
ModuleScheduler.h:10
ModuleScheduler::Reset
void Reset()
Definition
ModuleScheduler.h:15
ModuleScheduler::next
size_t next
Definition
ModuleScheduler.h:13
ModuleScheduler::pending
std::vector< Pending > pending
Definition
ModuleScheduler.h:12
ModuleScheduler::Advance
void Advance(float frameSeconds, size_t count, Dispatch dispatch)
Definition
ModuleScheduler.h:16
ModuleScheduler::Pending
Definition
ModuleScheduler.h:11
ModuleScheduler::Pending::maxFrameSeconds
float maxFrameSeconds
Definition
ModuleScheduler.h:11
ModuleScheduler::Pending::seconds
double seconds
Definition
ModuleScheduler.h:11
Modules
ModuleScheduler.h
Generated on
for Rise by
1.18.0