
#include <Spectre/Game/FPSCounter.h>
#include <Spectre/System/System.h>

namespace sp {

FPSCounter::FPSCounter() :
m_fps (60.0f)
{
	m_updateRate = Time::seconds(2);
	reset();
}

void FPSCounter::addFrame()
{
	m_count++;
}

double FPSCounter::getFPS() const
{
	return m_fps;
}

void FPSCounter::setUpdateRate(unsigned int rate)
{
	// Clamp to 1.
	if (rate < 1) {
		rate = 1;
	}

	m_updateRate = Time::seconds(rate);

	// Must reset the counter.
	reset();
}

bool FPSCounter::update()
{
	Time elapsed = m_watch.elapsed();

	if (elapsed >= m_updateRate) {
		m_fps = m_count / elapsed.seconds();

		reset();
		return true;
	}
	return false;
}

void FPSCounter::reset()
{
	m_watch.restart();
	m_count = 0;
}

} // namespace sp
