-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimeStepController.h
78 lines (74 loc) · 1.76 KB
/
TimeStepController.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#define TIME_STEP_CONTROLLER
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <glog/logging.h>
#define TIME_ACCURACY 1e-8
class TimeStepController {
public:
TimeStepController() {}
TimeStepController(float totalTime, int fps, float defaultDt) {
_totalTime = totalTime;
_spf = 1.0f / fps;
_frameFlag = false;
_currentTime = 0;
_frameTime = _spf;
_frameCnt = 0;
_defaultDt = defaultDt;
_stepCount = 0;
}
TimeStepController(int totalFrame, int fps, float defaultDt) {
_spf = 1.0f / fps;
_totalTime = totalFrame * _spf;
_frameFlag = false;
_currentTime = 0;
_frameTime = _spf;
_frameCnt = 0;
_defaultDt = defaultDt;
_stepCount = 0;
}
void dumpToFile(std::ofstream& fout) {
fout.write((char*) this, sizeof(TimeStepController));
}
void loadFromFile(std::ifstream& fin) {
fin.read((char*) this, sizeof(TimeStepController));
}
bool isFinished() {
return _currentTime>_totalTime;
}
float getStepDt() {
LOG(INFO) << "dt = " << _defaultDt;
LOG(INFO) << "Current Simulation Time: " << _currentTime << std::endl;
float dt = _defaultDt;
float supposeNextTime = _currentTime + dt;
if (supposeNextTime - _frameTime>-TIME_ACCURACY) {
dt = _frameTime - _currentTime;
_currentTime = _frameTime;
_frameTime += _spf;
_frameFlag = true;
}
else {
_currentTime = supposeNextTime;
_frameFlag = false;
}
return dt;
}
/* Whenever the dt * fps -> one frame duration,
the frameFlag is set, indicating a new actual 'frame' shoule be generated.
*/
bool isFrameTime(int &frameCnt) {
if (_frameFlag) {
frameCnt = _frameCnt++;
}
return _frameFlag;
}
float _currentTime;
int _stepCount;
protected:
float _totalTime;
float _spf;
float _defaultDt;
float _frameTime;
bool _frameFlag;
int _frameCnt;
};