xoreos  0.0.5
fpscounter.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/util.h"
28 
30 
31 #include "src/events/events.h"
32 
33 namespace Graphics {
34 
35 FPSCounter::FPSCounter(size_t secs) : _seconds(secs) {
36  assert(_seconds > 0);
37 
39 
40  reset();
41 }
42 
44 }
45 
47  return _fps;
48 }
49 
51  _lastSampled = 0;
52 
53  _currentSecond = 0;
54 
55  _hasFullSeconds = false;
56 
57  _fps = 0;
58 
59  for (size_t i = 0; i < _seconds; i++)
60  _frames[i] = 0;
61 }
62 
64  uint32 now = EventMan.getTimestamp();
65 
66  if (_lastSampled == 0)
67  _lastSampled = now;
68 
69  if ((now - _lastSampled) >= 1000) {
70  // We had one second worth of frames
71 
72  // Advance to the next second
74  _lastSampled = now;
75 
76  // We filled all samples
77  if (_currentSecond == 0)
78  _hasFullSeconds = true;
79 
80  // Calculate the new FPS value
81  calculateFPS();
82 
83  // Reset the counter
85  }
86 
87  // Another frame!
89 }
90 
92  size_t seconds = _hasFullSeconds ? _seconds : _currentSecond;
93  uint32 frames = 0;
94  for (size_t i = 0; i < seconds; i++)
95  frames += _frames[i];
96 
97  _fps = seconds ? (frames / seconds) : 0;
98 }
99 
100 } // End of namespace Graphics
size_t _seconds
Number of seconds over which to average the FPS.
Definition: fpscounter.h:52
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
size_t _currentSecond
Current second we measure.
Definition: fpscounter.h:53
Common::ScopedArray< uint32 > _frames
All frame counters.
Definition: fpscounter.h:59
void calculateFPS()
Calculate the average FPS value.
Definition: fpscounter.cpp:91
Counting FPS.
FPSCounter(size_t secs)
Average the FPS over that many seconds.
Definition: fpscounter.cpp:35
Utility templates and functions.
void reset()
Reset the counter.
Definition: fpscounter.cpp:50
The global events manager.
uint32 _fps
The current FPS value.
Definition: fpscounter.h:57
bool _hasFullSeconds
Got all samples?
Definition: fpscounter.h:55
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
uint32 getFPS() const
Get the current FPS value.
Definition: fpscounter.cpp:46
uint32_t uint32
Definition: types.h:204
uint32 _lastSampled
The last time a finished frame was signaled.
Definition: fpscounter.h:50
void finishedFrame()
Signal a finished frame.
Definition: fpscounter.cpp:63