Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

Diff Revisions 336 vs 337 for /trunk/Source Code/Core/ApplicationTimer.cpp

Diff revisions: vs.
  @@ -1,4 +1,6 @@
1 1 #include "Headers/ApplicationTimer.h"
2 + #include "Headers/ProfileTimer.h"
3 +
2 4 #include "Core/Headers/Console.h"
3 5 #include "Core/Math/Headers/MathHelper.h"
4 6 #include "Utility/Headers/Localization.h"
  @@ -8,83 +10,33 @@
8 10
9 11 namespace Divide {
10 12
11 - ProfileTimer::~ProfileTimer()
13 + ApplicationTimer::ApplicationTimer() : _targetFrameRate(Config::TARGET_FRAME_RATE),
14 + _ticksPerMicrosecond(0.0),
15 + _speedfactor(1.0f),
16 + _elapsedTimeUs(0ULL),
17 + _init(false),
18 + _benchmark(false)
12 19 {
13 - ApplicationTimer::getInstance().removeTimer(this);
20 + _ticksPerSecond.QuadPart = 0LL;
21 + _frameDelay.QuadPart = 0LL;
22 + _startupTicks.QuadPart = 0LL;
14 23 }
15 24
16 - #if defined(_DEBUG) || defined(_PROFILE)
17 - ProfileTimer::ProfileTimer()
25 + ApplicationTimer::~ApplicationTimer()
18 26 {
19 - _init = false;
20 - _paused = false;
21 - _timer = 0.0;
22 - _timerAverage = 0.0;
23 - _timerCounter = 0;
24 - }
25 -
26 - void ProfileTimer::reset() {
27 - _timerAverage = 0.0;
28 - _timerCounter = 0;
29 - }
30 -
31 - void ProfileTimer::start() {
32 - _timer = getUsToMs(ApplicationTimer::getInstance().getElapsedTime(true));
33 - }
34 -
35 - void ProfileTimer::stop() {
36 - if(_paused) {
37 - reset();
38 - return;
39 - }
40 -
41 - _timer = getUsToMs(ApplicationTimer::getInstance().getElapsedTime(true)) - _timer;
42 - _timerAverage = _timerAverage + _timer;
43 - _timerCounter++;
44 - }
45 -
46 - void ProfileTimer::create(const stringImpl& name){
47 - if (_init) {
48 - return;
49 - }
50 -
51 - _name = name;
52 - _init = true;
53 - // should never be called twice for the same object
54 - ApplicationTimer::getInstance().addTimer(this);
55 27 }
56 28
57 - void ProfileTimer::print() const {
58 - if (!_paused) {
59 - PRINT_FN("[ %s ] : [ %5.3f ms]", _name.c_str(), _timerAverage / _timerCounter);
60 - }
61 - }
62 -
63 - #endif
64 -
65 29 void ApplicationTimer::addTimer(ProfileTimer* const timer) {
66 30 _profileTimers.push_back(timer);
67 31 }
68 32
69 33 void ApplicationTimer::removeTimer(ProfileTimer* const timer) {
70 - const stringImpl& timerName = timer->name();
71 - _profileTimers.erase(std::remove_if(_profileTimers.begin(), _profileTimers.end(),
34 + const stringImpl& timerName = timer->name();
35 + _profileTimers.erase(std::remove_if(_profileTimers.begin(), _profileTimers.end(),
72 36 [&timerName](ProfileTimer* tTimer)->bool {
73 - return tTimer->name().compare(timerName) == 0;
74 - }),
75 - _profileTimers.end());
76 - }
77 -
78 - ApplicationTimer::ApplicationTimer() : _targetFrameRate(Config::TARGET_FRAME_RATE),
79 - _ticksPerMicrosecond(0.0),
80 - _speedfactor(1.0f),
81 - _elapsedTimeUs(0ULL),
82 - _init(false),
83 - _benchmark(false)
84 - {
85 - _ticksPerSecond.QuadPart = 0LL;
86 - _frameDelay.QuadPart = 0LL;
87 - _startupTicks.QuadPart = 0LL;
37 + return tTimer->name().compare(timerName) == 0;
38 + }),
39 + _profileTimers.end());
88 40 }
89 41
90 42 ///No need for init to be threadsafe
  @@ -94,8 +46,8 @@
94 46 _targetFrameRate = static_cast<U32>(targetFrameRate);
95 47
96 48 #if defined( OS_WINDOWS )
97 - bool queryAvailable = QueryPerformanceFrequency(&_ticksPerSecond) != 0;
98 - DIVIDE_ASSERT(queryAvailable, "Current system does not support 'QueryPerformanceFrequency calls!");
49 + bool queryAvailable = QueryPerformanceFrequency(&_ticksPerSecond) != 0;
50 + DIVIDE_ASSERT(queryAvailable, "Current system does not support 'QueryPerformanceFrequency calls!");
99 51 QueryPerformanceCounter(&_startupTicks);
100 52 #else
101 53 gettimeofday(&_startupTicks, nullptr);
  @@ -107,77 +59,77 @@
107 59 }
108 60
109 61 ApplicationTimer::LI ApplicationTimer::getCurrentTicksInternal() const {
110 - LI currentTicks;
111 - currentTicks.QuadPart = 0;
62 + LI currentTicks;
63 + currentTicks.QuadPart = 0;
112 64 #if defined( OS_WINDOWS )
113 - QueryPerformanceCounter(&currentTicks);
65 + QueryPerformanceCounter(&currentTicks);
114 66 #else
115 - gettimeofday(&currentTicks,nullptr);
67 + gettimeofday(&currentTicks,nullptr);
116 68 #endif
117 - return currentTicks;
69 + return currentTicks;
118 70 }
119 71
120 72 U64 ApplicationTimer::getElapsedTimeInternal(LI currentTicks) const {
121 - return static_cast<U64>((currentTicks.QuadPart - _startupTicks.QuadPart) / _ticksPerMicrosecond);
73 + return static_cast<U64>((currentTicks.QuadPart - _startupTicks.QuadPart) / _ticksPerMicrosecond);
122 74 }
123 75
124 76 void ApplicationTimer::update(U32 frameCount) {
125 - LI currentTicks = getCurrentTicksInternal();
126 - _elapsedTimeUs = getElapsedTimeInternal(currentTicks);
77 + LI currentTicks = getCurrentTicksInternal();
78 + _elapsedTimeUs = getElapsedTimeInternal(currentTicks);
127 79
128 - _speedfactor = static_cast<F32>((currentTicks.QuadPart - _frameDelay.QuadPart) /
80 + _speedfactor = static_cast<F32>((currentTicks.QuadPart - _frameDelay.QuadPart) /
129 81 (_ticksPerSecond.QuadPart / static_cast<F32>(_targetFrameRate)));
130 - _frameDelay = currentTicks;
82 + _frameDelay = currentTicks;
131 83
132 - if (_speedfactor <= 0.0f) {
133 - _speedfactor = 1.0f;
134 - }
84 + if (_speedfactor <= 0.0f) {
85 + _speedfactor = 1.0f;
86 + }
135 87
136 - _fps = _targetFrameRate / _speedfactor;
137 - _frameTime = 1000.0 / _fps;
88 + _fps = _targetFrameRate / _speedfactor;
89 + _frameTime = 1000.0 / _fps;
138 90
139 - benchmarkInternal(frameCount);
91 + benchmarkInternal(frameCount);
140 92 }
141 93
142 94 namespace {
143 - static U32 g_averageCount = 0;
144 - static F32 g_maxFps = std::numeric_limits<F32>::min();
145 - static F32 g_minFps = std::numeric_limits<F32>::max();
146 - static F32 g_averageFps = 0.0f;
147 - static F32 g_averageFpsTotal = 0.0f;
95 + static U32 g_averageCount = 0;
96 + static F32 g_maxFps = std::numeric_limits<F32>::min();
97 + static F32 g_minFps = std::numeric_limits<F32>::max();
98 + static F32 g_averageFps = 0.0f;
99 + static F32 g_averageFpsTotal = 0.0f;
148 100 };
149 101
150 102 void ApplicationTimer::benchmarkInternal(U32 frameCount){
151 - if (!_benchmark) {
152 - return;
153 - }
103 + if (!_benchmark) {
104 + return;
105 + }
154 106
155 107 //Average FPS
156 108 g_averageFps += _fps;
157 - g_averageCount++;
109 + g_averageCount++;
158 110
159 111 //Min/Max FPS (after every target second)
160 - if (frameCount % _targetFrameRate == 0) {
161 - g_maxFps = std::max(g_maxFps, _fps);
162 - g_minFps = std::min(g_minFps, _fps);
163 - }
164 -
112 + if (frameCount % _targetFrameRate == 0) {
113 + g_maxFps = std::max(g_maxFps, _fps);
114 + g_minFps = std::min(g_minFps, _fps);
115 + }
116 +
165 117 //Every 10 seconds (targeted)
166 - if (frameCount % (_targetFrameRate * 10) == 0) {
167 - g_averageFpsTotal += g_averageFps;
118 + if (frameCount % (_targetFrameRate * 10) == 0) {
119 + g_averageFpsTotal += g_averageFps;
168 120
169 - F32 avgFPS = g_averageFpsTotal / g_averageCount;
170 - PRINT_FN(Locale::get("FRAMERATE_FPS_OUTPUT"), avgFPS, g_maxFps, g_minFps, 1000.0f / avgFPS);
121 + F32 avgFPS = g_averageFpsTotal / g_averageCount;
122 + PRINT_FN(Locale::get("FRAMERATE_FPS_OUTPUT"), avgFPS, g_maxFps, g_minFps, 1000.0f / avgFPS);
171 123
172 - # if defined(_DEBUG) || defined(_PROFILE)
173 - for (ProfileTimer* const timer : _profileTimers) {
174 - timer->print();
175 - timer->reset();
176 - }
177 -
178 - # endif
179 - g_averageFps = 0;
180 - }
124 + # if defined(_DEBUG) || defined(_PROFILE)
125 + for (ProfileTimer* const timer : _profileTimers) {
126 + timer->print();
127 + timer->reset();
128 + }
129 +
130 + # endif
131 + g_averageFps = 0;
132 + }
181 133 }
182 134
183 135 };