Commits

Jason McKesson committed d0ce054

Timers can now rewind.

Comments (0)

Files changed (5)

Tut 12 Dynamic Range/Lights.cpp

 	{timeData.second.TogglePause();}
 };
 
+struct RewindTimer
+{
+	RewindTimer(float _secRewind) : secRewind(_secRewind) {}
+
+	void operator()(Framework::Timer &timer) {timer.Rewind(secRewind);}
+	void operator()(std::pair<const std::string, Framework::Timer> &timeData)
+	{timeData.second.Rewind(secRewind);}
+
+	float secRewind;
+};
+
 void LightManager::UpdateTime()
 {
 	m_keyLightTimer.Update();
 	return m_keyLightTimer.TogglePause();
 }
 
+void LightManager::RewindTime( float secRewind )
+{
+	m_keyLightTimer.Rewind(secRewind);
+	std::for_each(m_lightTimers.begin(), m_lightTimers.end(), RewindTimer(secRewind));
+	std::for_each(m_extraTimers.begin(), m_extraTimers.end(), RewindTimer(secRewind));
+}
+
 LightBlock LightManager::GetLightPositions( const glm::mat4 &worldToCameraMat ) const
 {
 	LightBlock lightData;

Tut 12 Dynamic Range/Lights.h

 	LightManager();
 
 	void UpdateTime();
-
 	bool TogglePause();
+	void RewindTime(float secRewind);
 
 	LightBlock GetLightPositions(const glm::mat4 &worldToCameraMat) const;
 

Tut 12 Dynamic Range/Scene Lighting.cpp

 		break;
 		
 	case 'b': g_lights.TogglePause(); break;
+	case 'g': g_lights.RewindTime(1.0f); break;
 	case 't': g_bDrawCameraPos = !g_bDrawCameraPos; break;
 
 	case 'w': g_mousePole.OffsetTargetPos(Framework::MousePole::DIR_FORWARD, 5.0f); break;

framework/Timer.cpp

 {
 	Timer::Timer( Type eType, float fDuration )
 		: m_eType(eType)
-		, m_fDuration(fDuration)
-		, m_bHasUpdated(false)
-		, m_bIsPaused(false)
+		, m_secDuration(fDuration)
+		, m_hasUpdated(false)
+		, m_isPaused(false)
 		, m_absPrevTime(0.0f)
-		, m_fAccumTime(0.0f)
+		, m_secAccumTime(0.0f)
 	{
 		if(m_eType != TT_INFINITE)
-			assert(m_fDuration > 0.0f);
+			assert(m_secDuration > 0.0f);
 	}
 
 	void Timer::Reset()
 	{
-		m_bHasUpdated = false;
-		m_fAccumTime = 0.0f;
+		m_hasUpdated = false;
+		m_secAccumTime = 0.0f;
 	}
 
 	bool Timer::TogglePause()
 	{
-		m_bIsPaused = !m_bIsPaused;
-		return m_bIsPaused;
+		m_isPaused = !m_isPaused;
+		return m_isPaused;
 	}
 
 	bool Timer::Update()
 	{
 		float absCurrTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
-		if(!m_bHasUpdated)
+		if(!m_hasUpdated)
 		{
 			m_absPrevTime = absCurrTime;
-			m_bHasUpdated = true;
+			m_hasUpdated = true;
 		}
 
-		if(m_bIsPaused)
+		if(m_isPaused)
 		{
 			m_absPrevTime = absCurrTime;
 			return false;
 		}
 
 		float fDeltaTime = absCurrTime - m_absPrevTime;
-		m_fAccumTime += fDeltaTime;
+		m_secAccumTime += fDeltaTime;
 
 		m_absPrevTime = absCurrTime;
 		if(m_eType == TT_SINGLE)
-			return m_fAccumTime > m_fDuration;
+			return m_secAccumTime > m_secDuration;
 
 		return false;
 	}
 
+	void Timer::Rewind( float secRewind )
+	{
+		m_secAccumTime -= secRewind;
+	}
+
 	float Timer::GetAlpha() const
 	{
 		switch(m_eType)
 		{
 		case TT_LOOP:
-			return fmodf(m_fAccumTime, m_fDuration) / m_fDuration;
+			return fmodf(m_secAccumTime, m_secDuration) / m_secDuration;
 		case TT_SINGLE:
-			return glm::clamp(m_fAccumTime / m_fDuration, 0.0f, 1.0f);
+			return glm::clamp(m_secAccumTime / m_secDuration, 0.0f, 1.0f);
 		}
 
 		return -1.0f;	//Garbage.
 		switch(m_eType)
 		{
 		case TT_LOOP:
-			return fmodf(m_fAccumTime, m_fDuration) * m_fDuration;
+			return fmodf(m_secAccumTime, m_secDuration) * m_secDuration;
 		case TT_SINGLE:
-			return glm::clamp(m_fAccumTime, 0.0f, m_fDuration);
+			return glm::clamp(m_secAccumTime, 0.0f, m_secDuration);
 		}
 
 		return -1.0f;	//Garbage.
 
 	float Timer::GetTimeSinceStart() const
 	{
-		return m_fAccumTime;
+		return m_secAccumTime;
 	}
 }

framework/Timer.h

 		//Will only return true for SINGLE timers that have reached their duration.
 		bool Update();
 
+		//Subtracts secRewind from the current time and continues from there.
+		void Rewind(float secRewind);
+
 		//Returns a number [0, 1], representing progress through the duration. Only used
 		//for SINGLE and LOOP timers.
 		float GetAlpha() const;
 
 	private:
 		Type m_eType;
-		float m_fDuration;
+		float m_secDuration;
 
-		bool m_bHasUpdated;
-		bool m_bIsPaused;
+		bool m_hasUpdated;
+		bool m_isPaused;
 
 		float m_absPrevTime;
-		float m_fAccumTime;
+		float m_secAccumTime;
 	};
 }