summaryrefslogtreecommitdiffstats
path: root/src/Utility.cpp
blob: 3bc9df9b178ad4ff4f12d502977d6d8122fdb03a (plain) (blame)
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
#include "Utility.hpp"

#include <thread>

#include <optick.h>
#include <easylogging++.h>

LoopExecutionTimeController::LoopExecutionTimeController(duration delayLength)
        : delayLength(delayLength) {
    previousUpdate = clock::now();
}

void LoopExecutionTimeController::SetDelayLength(duration length) {
    delayLength = length;
}

unsigned long long LoopExecutionTimeController::GetIterations() {
    return iterations;
}

void LoopExecutionTimeController::Update() {
    iterations++;
    auto timeToSleep = delayLength - GetDelta();    
    if (timeToSleep.count() > 0)
        std::this_thread::sleep_for(timeToSleep);
    previousPreviousUpdate = previousUpdate;
    previousUpdate = clock::now();    
}

double LoopExecutionTimeController::GetDeltaMs() {
    std::chrono::duration<double, std::milli> delta = GetDelta();    
    return delta.count();
}

LoopExecutionTimeController::duration LoopExecutionTimeController::GetDelta() {
    auto now = clock::now();
    return duration(now-previousUpdate);
}

double LoopExecutionTimeController::GetDeltaS() {
    std::chrono::duration<double, std::ratio<1,1>> delta = GetDelta();
    return delta.count();
}

double LoopExecutionTimeController::GetRealDeltaS() {
    return std::chrono::duration<double,std::ratio<1,1>>(previousUpdate - previousPreviousUpdate).count();
}

double LoopExecutionTimeController::RemainTimeMs() {
    auto remain = delayLength - GetDelta();
    return remain.count();
}