charcoal/OpenGLEngine/FPS.cpp
2018-09-04 15:25:54 -04:00

48 lines
946 B
C++

#include "FPS.h"
FPS::FPS(unsigned short frames /* = 2 */) :
m_iter(frames), m_frames(frames)
{
if (frames < 2)
throw "Frames is too small";
m_clocks = new clock_t[m_frames];
}
FPS::~FPS()
{
if (m_clocks)
delete[] m_clocks;
}
const clock_t& FPS::get_clock() const
{
return m_clocks[m_iter];
}
float FPS::get_spf() const
{
clock_t diff = (m_clocks[m_iter] - m_clocks[(m_iter + 1) % m_frames]) / (m_frames - 1);
return ((float)diff) / CLOCKS_PER_SEC;
}
float FPS::get_fps() const
{
clock_t diff = (m_clocks[m_iter] - m_clocks[(m_iter + 1) % m_frames]) / (m_frames - 1);
return ((float)CLOCKS_PER_SEC) / diff;
}
void FPS::prepare()
{
clock_t c = clock();
for (unsigned short i = 0; i < m_frames; ++i)
m_clocks[i] = c;
}
float FPS::mark()
{
(++m_iter) %= m_frames;
m_clocks[m_iter] = clock();
clock_t diff = (m_clocks[m_iter] - m_clocks[(m_iter > 0 ? m_iter - 1 : m_frames - 1)]);
return ((float)diff) / CLOCKS_PER_SEC;
}