FIFE
2008.0
|
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 #include <algorithm> 00024 #include <cassert> 00025 00026 // 3rd party library includes 00027 #include <SDL.h> 00028 00029 // FIFE includes 00030 // These includes are split up in two parts, separated by one empty line 00031 // First block: files included from the FIFE root src directory 00032 // Second block: files included from the same folder 00033 #include "util/log/logger.h" 00034 00035 #include "timeevent.h" 00036 #include "timemanager.h" 00037 00038 namespace FIFE { 00039 static const unsigned long UNDEFINED_TIME_DELTA = 999999; 00040 static Logger _log(LM_UTIL); 00041 00042 TimeManager::TimeManager(): 00043 m_current_time (0), 00044 m_time_delta(UNDEFINED_TIME_DELTA), 00045 m_average_frame_time(0) { 00046 } 00047 00048 TimeManager::~TimeManager() { 00049 } 00050 00051 void TimeManager::update() { 00052 // if first update... 00053 double avg_multiplier = 0.985; 00054 if (m_current_time == 0) { 00055 m_current_time = SDL_GetTicks(); 00056 avg_multiplier = 0; 00057 m_time_delta = 0; 00058 } else { 00059 m_time_delta = m_current_time; 00060 m_current_time = SDL_GetTicks(); 00061 m_time_delta = m_current_time - m_time_delta; 00062 } 00063 m_average_frame_time = m_average_frame_time * avg_multiplier + 00064 double(m_time_delta) * (1.0 - avg_multiplier); 00065 00066 // Update live events. 00067 // 00068 // It is very important to NOT use iterators (over a vector) 00069 // here, as an event might add enough events to resize the vector. 00070 // -> Ugly segfault 00071 for (size_t i = 0; i < m_events_list.size(); ++i) { 00072 TimeEvent* event = m_events_list[ i ]; 00073 if( event ) { 00074 event->managerUpdateEvent(m_current_time); 00075 } 00076 } 00077 00078 // Remove dead events 00079 std::vector<TimeEvent*>::iterator it; 00080 it = std::remove( m_events_list.begin(), m_events_list.end(), static_cast<TimeEvent*>(0)); 00081 m_events_list.erase( it, m_events_list.end()); 00082 } 00083 00084 void TimeManager::registerEvent(TimeEvent* event) { 00085 // Register. 00086 m_events_list.push_back(event); 00087 } 00088 00089 void TimeManager::unregisterEvent(TimeEvent* event) { 00090 // Unregister. 00091 for (size_t i = 0; i < m_events_list.size(); ++i) { 00092 TimeEvent*& event_i = m_events_list[ i ]; 00093 if( event_i == event) { 00094 event_i = 0; 00095 return; 00096 } 00097 } 00098 } 00099 00100 unsigned long TimeManager::getTime() const { 00101 return m_current_time; 00102 } 00103 00104 unsigned long TimeManager::getTimeDelta() const { 00105 return m_time_delta; 00106 } 00107 00108 double TimeManager::getAverageFrameTime() const { 00109 return m_average_frame_time; 00110 } 00111 00112 void TimeManager::printStatistics() const { 00113 FL_LOG(_log, LMsg("Timers: ") << m_events_list.size()); 00114 } 00115 00116 } //FIFE 00117 00118