Spectre/Game/FPSCounter: Use sp::Time
This commit is contained in:
parent
6de6028ba4
commit
919aa7740a
2 changed files with 19 additions and 18 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
#ifndef SPECTRE_FPS_COUNTER_H
|
#ifndef SPECTRE_FPS_COUNTER_H
|
||||||
#define SPECTRE_FPS_COUNTER_H
|
#define SPECTRE_FPS_COUNTER_H
|
||||||
|
|
||||||
|
#include <Spectre/Math/Time.h>
|
||||||
|
|
||||||
// Simple FPS counter.
|
// Simple FPS counter.
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
@ -19,7 +21,7 @@ public :
|
||||||
|
|
||||||
// Set the update rate (in seconds).
|
// Set the update rate (in seconds).
|
||||||
// How often the FPS should be sampled and calculated.
|
// How often the FPS should be sampled and calculated.
|
||||||
void setUpdateRate(unsigned int seconds);
|
void setUpdateRate(unsigned int rate);
|
||||||
|
|
||||||
// Update the internal time measurment.
|
// Update the internal time measurment.
|
||||||
bool update();
|
bool update();
|
||||||
|
|
@ -29,16 +31,16 @@ public :
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
// Frame counter.
|
// Number of frames since last update.
|
||||||
unsigned int m_count;
|
unsigned int m_count;
|
||||||
|
|
||||||
// time (in ms)
|
// Last time we updated the counter.
|
||||||
unsigned int m_time;
|
Time m_time;
|
||||||
|
|
||||||
// Update rate (in ms)
|
// Update rate (at what interval should we update)
|
||||||
unsigned int m_updateRate;
|
Time m_updateRate;
|
||||||
|
|
||||||
// Calculated fps.
|
// Calculated Frames per second.
|
||||||
float m_fps;
|
float m_fps;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
FPSCounter::FPSCounter() :
|
FPSCounter::FPSCounter() :
|
||||||
m_fps (60.0f),
|
m_fps (60.0f)
|
||||||
m_updateRate (2000)
|
|
||||||
{
|
{
|
||||||
|
m_updateRate = Time::seconds(2);
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -21,14 +21,14 @@ float FPSCounter::getFPS() const
|
||||||
return m_fps;
|
return m_fps;
|
||||||
}
|
}
|
||||||
|
|
||||||
void FPSCounter::setUpdateRate(unsigned int ups)
|
void FPSCounter::setUpdateRate(unsigned int rate)
|
||||||
{
|
{
|
||||||
// Clamp to 1.
|
// Clamp to 1.
|
||||||
if (ups < 1) {
|
if (rate < 1) {
|
||||||
ups = 1;
|
rate = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_updateRate = ups * 1000;
|
m_updateRate = Time::seconds(rate);
|
||||||
|
|
||||||
// Must reset the counter.
|
// Must reset the counter.
|
||||||
reset();
|
reset();
|
||||||
|
|
@ -36,12 +36,11 @@ void FPSCounter::setUpdateRate(unsigned int ups)
|
||||||
|
|
||||||
bool FPSCounter::update()
|
bool FPSCounter::update()
|
||||||
{
|
{
|
||||||
unsigned int current = system::getMilliseconds();
|
Time current = Time::milliseconds(system::getMilliseconds());
|
||||||
unsigned int elapsed = current - m_time;
|
Time elapsed = current - m_time;
|
||||||
|
|
||||||
if (elapsed >= m_updateRate) {
|
if (elapsed >= m_updateRate) {
|
||||||
float fraction = m_count / ((float) elapsed);
|
m_fps = m_count / elapsed.seconds();
|
||||||
m_fps = fraction * 1000.f;
|
|
||||||
|
|
||||||
m_time = current;
|
m_time = current;
|
||||||
m_count = 0;
|
m_count = 0;
|
||||||
|
|
@ -52,7 +51,7 @@ bool FPSCounter::update()
|
||||||
|
|
||||||
void FPSCounter::reset()
|
void FPSCounter::reset()
|
||||||
{
|
{
|
||||||
m_time = system::getMilliseconds();
|
m_time = Time::milliseconds(system::getMilliseconds());
|
||||||
m_count = 0;
|
m_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue