diff engine/core/model/metamodel/timeprovider.cpp @ 181:56ac89189bc4

fixed time handling in fife: - the time scaling method was unreliable when changing the time factors (jumping in the moment of the change) - replaced much calls to SDL_GetTicks with TimerManager::instance()->getTime() - some other time related cleanup
author spq@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 31 Jan 2009 04:15:43 +0000
parents 90005975cdbb
children
line wrap: on
line diff
--- a/engine/core/model/metamodel/timeprovider.cpp	Fri Jan 30 00:58:43 2009 +0000
+++ b/engine/core/model/metamodel/timeprovider.cpp	Sat Jan 31 04:15:43 2009 +0000
@@ -35,7 +35,8 @@
 namespace FIFE {
 	TimeProvider::TimeProvider(TimeProvider* master): 
 		m_master(master), 
-		m_multiplier(1.0) { 
+		m_multiplier(1.0) {
+		m_time_static = m_time_scaled = master ? master->getGameTime() : TimeManager::instance()->getTime();
 	}
 	
 	TimeProvider::~TimeProvider() {}
@@ -44,6 +45,8 @@
 		if (multiplier < 0.0) {
 			throw NotSupported("Negative time multiplier are not supported");
 		}
+		m_time_static = getPreciseGameTime();
+		m_time_scaled = m_master ? m_master->getPreciseGameTime() : static_cast<float>(TimeManager::instance()->getTime());
 		m_multiplier = multiplier;
 	}
 	
@@ -59,14 +62,14 @@
 		}
 	}
 	
-	unsigned int TimeProvider::getGameTicks() const {
-		if (m_master) {
-			return m_master->getGameTicks();
-		} else {
-			return TimeManager::instance()->getTime();
-		}
+	unsigned int TimeProvider::getGameTime() const {
+		return static_cast<unsigned int>(getPreciseGameTime());
 	}
-
+	
+	double TimeProvider::getPreciseGameTime() const {
+		return m_time_static + m_multiplier * ((m_master ? m_master->getPreciseGameTime() : static_cast<float>(TimeManager::instance()->getTime())) - m_time_scaled);
+	}
+	
 	unsigned int scaleTime(float multiplier, unsigned int ticks) {
 		return static_cast<unsigned int>(static_cast<float>(ticks) * multiplier);
 	}