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

FPSCounter::FPSCounter() :
m_fps		(60.0f),
m_updateRate	(2000)
{
	reset();
}

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

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

void FPSCounter::setUpdateRate(unsigned int ups)
{
	// Clamp to 1.
	if (ups < 1) {
		ups = 1;
	}
	
	m_updateRate = ups * 1000;

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

bool FPSCounter::update()
{
	unsigned int current = System::getMilliseconds();
	unsigned int elapsed = current - m_time;

	if (elapsed >= m_updateRate) {
		float fraction = m_count / ((float) elapsed);
		m_fps = fraction * 1000.f;

		m_time = current;
		m_count = 0;
		return true;
	}
	return false;
}

void FPSCounter::reset()
{
	m_time = System::getMilliseconds();
	m_count = 0;
}
