comparison 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
comparison
equal deleted inserted replaced
180:66bd7f8c541e 181:56ac89189bc4
33 #include "timeprovider.h" 33 #include "timeprovider.h"
34 34
35 namespace FIFE { 35 namespace FIFE {
36 TimeProvider::TimeProvider(TimeProvider* master): 36 TimeProvider::TimeProvider(TimeProvider* master):
37 m_master(master), 37 m_master(master),
38 m_multiplier(1.0) { 38 m_multiplier(1.0) {
39 m_time_static = m_time_scaled = master ? master->getGameTime() : TimeManager::instance()->getTime();
39 } 40 }
40 41
41 TimeProvider::~TimeProvider() {} 42 TimeProvider::~TimeProvider() {}
42 43
43 void TimeProvider::setMultiplier(float multiplier) { 44 void TimeProvider::setMultiplier(float multiplier) {
44 if (multiplier < 0.0) { 45 if (multiplier < 0.0) {
45 throw NotSupported("Negative time multiplier are not supported"); 46 throw NotSupported("Negative time multiplier are not supported");
46 } 47 }
48 m_time_static = getPreciseGameTime();
49 m_time_scaled = m_master ? m_master->getPreciseGameTime() : static_cast<float>(TimeManager::instance()->getTime());
47 m_multiplier = multiplier; 50 m_multiplier = multiplier;
48 } 51 }
49 52
50 float TimeProvider::getMultiplier() const { 53 float TimeProvider::getMultiplier() const {
51 return m_multiplier; 54 return m_multiplier;
57 } else { 60 } else {
58 return m_multiplier; 61 return m_multiplier;
59 } 62 }
60 } 63 }
61 64
62 unsigned int TimeProvider::getGameTicks() const { 65 unsigned int TimeProvider::getGameTime() const {
63 if (m_master) { 66 return static_cast<unsigned int>(getPreciseGameTime());
64 return m_master->getGameTicks();
65 } else {
66 return TimeManager::instance()->getTime();
67 }
68 } 67 }
69 68
69 double TimeProvider::getPreciseGameTime() const {
70 return m_time_static + m_multiplier * ((m_master ? m_master->getPreciseGameTime() : static_cast<float>(TimeManager::instance()->getTime())) - m_time_scaled);
71 }
72
70 unsigned int scaleTime(float multiplier, unsigned int ticks) { 73 unsigned int scaleTime(float multiplier, unsigned int ticks) {
71 return static_cast<unsigned int>(static_cast<float>(ticks) * multiplier); 74 return static_cast<unsigned int>(static_cast<float>(ticks) * multiplier);
72 } 75 }
73 } 76 }