#include "blitz/misc/TickCounter.h"

#include "blitz/misc/Time.h"

namespace blitz {
namespace utils {

void TickCounter::Reset() {
	m_TPS = m_TargetTPS;
	m_LastTPSTime = utils::GetTime();
	m_TickCount = 0;
}

bool TickCounter::Update() {  // return true when tps is updated
	m_TickCount++;
	if (m_TickCount >= m_TargetTPS) {
		std::uint64_t timeElapsedSinceLastTicks = utils::GetTime() - m_LastTPSTime;
		m_TPS = static_cast<float>(m_TargetTPS) / static_cast<float>(timeElapsedSinceLastTicks / 1000.0f);
		m_TickCount = 0;
		m_LastTPSTime = utils::GetTime();
		return true;
	}
	return false;
}

}  // namespace utils
}  // namespace blitz
