-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTiming.cpp
More file actions
63 lines (49 loc) · 957 Bytes
/
Timing.cpp
File metadata and controls
63 lines (49 loc) · 957 Bytes
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
#include "Timing.h"
#include <SDL2/SDL.h>
#include <cstdio>
#include <iostream>
Timing::Timing()
{
}
void Timing::FpsLimitInit()
{
_initTick = SDL_GetTicks();
}
void Timing::TimeInit()
{
_fpsLastTime = SDL_GetTicks();
_fpsCurrent = 0;
_fpsFrames = 0;
}
void Timing::CalculateFPS(bool printFPS)
{
_fpsFrames++;
if (_fpsLastTime < SDL_GetTicks() - (1.0f * 1000.0f)) {
_fpsLastTime = SDL_GetTicks();
_fpsCurrent = _fpsFrames;
if (printFPS)
printf("FPS: %u\n", _fpsCurrent);
_deltaTime = float(_fpsCurrent - _fpsLastTime);
_fpsFrames = 0;
}
}
void Timing::LimitFPS(const float MAX_FPS)
{
unsigned int ticks = SDL_GetTicks() - _initTick;
if (1000.0f / MAX_FPS > ticks) {
SDL_Delay(1000.0f / MAX_FPS - ticks);
}
}
void Timing::calcDeltaTime()
{
static long last = 0;
_deltaTime = 0.0f;
long now = SDL_GetTicks();
if (now > last) {
_deltaTime = ((float)(now - last)) / 1000.0f;
last = now;
}
}
Timing::~Timing()
{
}