FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
timeprovider.cpp
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 
00024 // 3rd party library includes
00025 
00026 // FIFE includes
00027 // These includes are split up in two parts, separated by one empty line
00028 // First block: files included from the FIFE root src directory
00029 // Second block: files included from the same folder
00030 #include "util/base/exception.h"
00031 #include "util/time/timemanager.h"
00032 
00033 #include "timeprovider.h"
00034 
00035 namespace FIFE {
00036     TimeProvider::TimeProvider(TimeProvider* master): 
00037         m_master(master), 
00038         m_multiplier(1.0) {
00039         m_time_static = m_time_scaled = master ? master->getGameTime() : TimeManager::instance()->getTime();
00040     }
00041     
00042     TimeProvider::~TimeProvider() {}
00043     
00044     void TimeProvider::setMultiplier(float multiplier) { 
00045         if (multiplier < 0.0) {
00046             throw NotSupported("Negative time multiplier are not supported");
00047         }
00048         m_time_static = getPreciseGameTime();
00049         m_time_scaled = m_master ? m_master->getPreciseGameTime() : static_cast<float>(TimeManager::instance()->getTime());
00050         m_multiplier = multiplier;
00051     }
00052     
00053     float TimeProvider::getMultiplier() const { 
00054         return m_multiplier;
00055     }
00056     
00057     float TimeProvider::getTotalMultiplier() const {
00058         if (m_master) {
00059             return m_master->getTotalMultiplier() * m_multiplier;
00060         } else {
00061             return m_multiplier;
00062         }
00063     }
00064     
00065     unsigned int TimeProvider::getGameTime() const {
00066         return static_cast<unsigned int>(getPreciseGameTime());
00067     }
00068     
00069     double TimeProvider::getPreciseGameTime() const {
00070         return m_time_static + m_multiplier * ((m_master ? m_master->getPreciseGameTime() : static_cast<float>(TimeManager::instance()->getTime())) - m_time_scaled);
00071     }
00072     
00073     unsigned int scaleTime(float multiplier, unsigned int ticks) {
00074         return static_cast<unsigned int>(static_cast<float>(ticks) * multiplier);
00075     }
00076 }