gltut / framework / Timer.cpp

#include <math.h>
#include <glm/glm.hpp>
#include <GL/freeglut.h>
#include "framework.h"
#include "Timer.h"



namespace Framework
{
	Timer::Timer( Type eType, float fDuration )
		: m_eType(eType)
		, m_secDuration(fDuration)
		, m_hasUpdated(false)
		, m_isPaused(false)
		, m_absPrevTime(0.0f)
		, m_secAccumTime(0.0f)
	{
		if(m_eType != TT_INFINITE)
			assert(m_secDuration > 0.0f);
	}

	void Timer::Reset()
	{
		m_hasUpdated = false;
		m_secAccumTime = 0.0f;
	}

	bool Timer::TogglePause()
	{
		m_isPaused = !m_isPaused;
		return m_isPaused;
	}

	bool Timer::IsPaused() const
	{
		return m_isPaused;
	}

	void Timer::SetPause( bool pause )
	{
		m_isPaused = pause;
	}

	bool Timer::Update()
	{
		float absCurrTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
		if(!m_hasUpdated)
		{
			m_absPrevTime = absCurrTime;
			m_hasUpdated = true;
		}

		if(m_isPaused)
		{
			m_absPrevTime = absCurrTime;
			return false;
		}

		float fDeltaTime = absCurrTime - m_absPrevTime;
		m_secAccumTime += fDeltaTime;

		m_absPrevTime = absCurrTime;
		if(m_eType == TT_SINGLE)
			return m_secAccumTime > m_secDuration;

		return false;
	}

	void Timer::Rewind( float secRewind )
	{
		m_secAccumTime -= secRewind;
		if(m_secAccumTime < 0.0f)
			m_secAccumTime = 0.0f;
	}

	void Timer::Fastforward( float secFF )
	{
		m_secAccumTime += secFF;
	}

	float Timer::GetAlpha() const
	{
		switch(m_eType)
		{
		case TT_LOOP:
			return fmodf(m_secAccumTime, m_secDuration) / m_secDuration;
		case TT_SINGLE:
			return glm::clamp(m_secAccumTime / m_secDuration, 0.0f, 1.0f);
		}

		return -1.0f;	//Garbage.
	}

	float Timer::GetProgression() const
	{
		switch(m_eType)
		{
		case TT_LOOP:
			return fmodf(m_secAccumTime, m_secDuration);
		case TT_SINGLE:
			return glm::clamp(m_secAccumTime, 0.0f, m_secDuration);
		}

		return -1.0f;	//Garbage.
	}

	float Timer::GetTimeSinceStart() const
	{
		return m_secAccumTime;
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.