-
Notifications
You must be signed in to change notification settings - Fork 2
/
FixedTimestep.h
55 lines (41 loc) · 1.32 KB
/
FixedTimestep.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once
#ifndef GRIFFIN_FIXEDTIMESTEP_H_
#define GRIFFIN_FIXEDTIMESTEP_H_
#include <cstdint>
#include <functional>
#include <cmath>
namespace griffin {
class FixedTimestep {
public:
using UpdateFuncType = std::function<void(const int64_t, const int64_t, const int64_t,
const float, const float, const float)>;
explicit FixedTimestep(float deltaMs,
int64_t countsPerMs,
UpdateFuncType&& update_) :
m_deltaMs{ deltaMs },
m_deltaCounts{ static_cast<int64_t>(deltaMs * countsPerMs) },
update{ std::forward<UpdateFuncType>(update_) }
{}
inline float tick(const int64_t realTime, const int64_t countsPassed, float gameSpeed = 1.0)
{
m_accumulator += static_cast<int64_t>(countsPassed * gameSpeed);
while (m_accumulator >= m_deltaCounts) {
update(m_virtualTime, m_gameTime, m_deltaCounts, m_deltaMs, m_deltaMs / 1000.0f, gameSpeed);
m_gameTime += m_deltaCounts;
m_virtualTime += m_deltaCounts;
m_accumulator -= m_deltaCounts;
}
m_virtualTime = realTime;
float interpolation = static_cast<float>(m_accumulator) / static_cast<float>(m_deltaCounts);
return interpolation;
}
// Variables
int64_t m_accumulator = 0;
int64_t m_gameTime = 0;
int64_t m_virtualTime = 0;
int64_t m_deltaCounts;
float m_deltaMs;
UpdateFuncType update;
};
}
#endif