-
Notifications
You must be signed in to change notification settings - Fork 0
/
FramerateModule.cpp
92 lines (78 loc) · 1.74 KB
/
FramerateModule.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Implementation of the FramerateModule class.
*
* @author Francis BISSON
*/
// INCLUDES ////////////////////////////////////////////////////////////////////
#include <sstream>
#include <SDL/SDL.h>
#include "Color.h"
#include "Debug.h"
#include "DisplayContext.h"
#include "Font.h"
#include "FramerateModule.h"
#include "Game.h"
#include "GameObject.h"
#include "Point.hpp"
#include "Text.h"
#include "Utility.h"
// IMPLEMENTATION //////////////////////////////////////////////////////////////
FramerateModule::FramerateModule(Game* game, unsigned long targetFps)
:GameObject(game)
,m_FrameCount(0)
,m_Time(0.0)
,m_FrameTime(0.0)
,m_TargetFps(targetFps)
#ifndef NDEBUG
,m_Font( new Font("Fonts/dejavu.bmp", Color::Grey ) )
,m_FPS( m_Font )
#endif
{
DEBUG_ASSERT(m_TargetFps > 0 && m_TargetFps <= 200);
}
FramerateModule::~FramerateModule()
{
}
bool FramerateModule::Init()
{
m_FrameCount = 0;
m_Time = 0.0;
#ifndef NDEBUG
m_FPS.Hide();
#endif
return true;
}
void FramerateModule::Update(double deltaTime)
{
m_FrameTime = deltaTime;
m_FrameCount++;
m_Time += deltaTime;
if(m_Time >= 1.0)
{
#ifndef NDEBUG
std::stringstream fps;
fps << "FPS:" << m_FrameCount;
m_FPS.SetText(fps.str());
m_FPS.Show();
#endif
m_FrameCount = 0;
m_Time = 0.0;
}
}
void FramerateModule::Render(DisplayContext* displayContext)
{
#ifndef NDEBUG
displayContext->DrawText(m_FPS, Point::Zero);
#else
UNUSED(displayContext);
#endif
}
void FramerateModule::LimitFramerate()
{
double targetFrameTime = 1.0 / (double)m_TargetFps;
if(targetFrameTime >= m_FrameTime)
{
Uint32 delay = (Uint32)((targetFrameTime - m_FrameTime) * 1000.0);
SDL_Delay(delay);
}
}