| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 4 |
* This file is part of FIFE. * |
| 5 |
* * |
| 6 |
* FIFE is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Lesser General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2.1 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 |
* Lesser General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Lesser General Public * |
| 17 |
* License along with this library; if not, write to the * |
| 18 |
* Free Software Foundation, Inc., * |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 |
***************************************************************************/ |
| 21 |
|
| 22 |
// Standard C++ library includes |
| 23 |
#include <algorithm> |
| 24 |
#include <cassert> |
| 25 |
|
| 26 |
// 3rd party library includes |
| 27 |
#include <SDL.h> |
| 28 |
|
| 29 |
// FIFE includes |
| 30 |
// These includes are split up in two parts, separated by one empty line |
| 31 |
// First block: files included from the FIFE root src directory |
| 32 |
// Second block: files included from the same folder |
| 33 |
#include "util/log/logger.h" |
| 34 |
|
| 35 |
#include "timeevent.h" |
| 36 |
#include "timemanager.h" |
| 37 |
|
| 38 |
namespace FIFE { |
| 39 |
static const uint32_t UNDEFINED_TIME_DELTA = 999999; |
| 40 |
static Logger _log(LM_UTIL); |
| 41 |
|
| 42 |
TimeManager::TimeManager(): |
| 43 |
m_current_time (0), |
| 44 |
m_time_delta(UNDEFINED_TIME_DELTA), |
| 45 |
m_average_frame_time(0) { |
| 46 |
} |
| 47 |
|
| 48 |
TimeManager::~TimeManager() { |
| 49 |
} |
| 50 |
|
| 51 |
void TimeManager::update() { |
| 52 |
// if first update... |
| 53 |
double avg_multiplier = 0.985; |
| 54 |
if (m_current_time == 0) { |
| 55 |
m_current_time = SDL_GetTicks(); |
| 56 |
avg_multiplier = 0; |
| 57 |
m_time_delta = 0; |
| 58 |
} else { |
| 59 |
m_time_delta = m_current_time; |
| 60 |
m_current_time = SDL_GetTicks(); |
| 61 |
m_time_delta = m_current_time - m_time_delta; |
| 62 |
} |
| 63 |
m_average_frame_time = m_average_frame_time * avg_multiplier + |
| 64 |
double(m_time_delta) * (1.0 - avg_multiplier); |
| 65 |
|
| 66 |
// Update live events. |
| 67 |
// |
| 68 |
// It is very important to NOT use iterators (over a vector) |
| 69 |
// here, as an event might add enough events to resize the vector. |
| 70 |
// -> Ugly segfault |
| 71 |
for (size_t i = 0; i < m_events_list.size(); ++i) { |
| 72 |
TimeEvent* event = m_events_list[ i ]; |
| 73 |
if( event ) { |
| 74 |
event->managerUpdateEvent(m_current_time); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// Remove dead events |
| 79 |
std::vector<TimeEvent*>::iterator it; |
| 80 |
it = std::remove( m_events_list.begin(), m_events_list.end(), static_cast<TimeEvent*>(0)); |
| 81 |
m_events_list.erase( it, m_events_list.end()); |
| 82 |
} |
| 83 |
|
| 84 |
void TimeManager::registerEvent(TimeEvent* event) { |
| 85 |
// Register. |
| 86 |
m_events_list.push_back(event); |
| 87 |
} |
| 88 |
|
| 89 |
void TimeManager::unregisterEvent(TimeEvent* event) { |
| 90 |
// Unregister. |
| 91 |
for (size_t i = 0; i < m_events_list.size(); ++i) { |
| 92 |
TimeEvent*& event_i = m_events_list[ i ]; |
| 93 |
if( event_i == event) { |
| 94 |
event_i = 0; |
| 95 |
return; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
uint32_t TimeManager::getTime() const { |
| 101 |
return m_current_time; |
| 102 |
} |
| 103 |
|
| 104 |
uint32_t TimeManager::getTimeDelta() const { |
| 105 |
return m_time_delta; |
| 106 |
} |
| 107 |
|
| 108 |
double TimeManager::getAverageFrameTime() const { |
| 109 |
return m_average_frame_time; |
| 110 |
} |
| 111 |
|
| 112 |
void TimeManager::printStatistics() const { |
| 113 |
FL_LOG(_log, LMsg("Timers: ") << m_events_list.size()); |
| 114 |
} |
| 115 |
|
| 116 |
} //FIFE |
| 117 |
|
| 118 |
|