Mercurial > fife-parpg
comparison engine/core/util/time/timemanager.cpp @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 90005975cdbb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 /*************************************************************************** | |
2 * Copyright (C) 2005-2008 by the FIFE team * | |
3 * http://www.fifengine.de * | |
4 * This file is part of FIFE. * | |
5 * * | |
6 * FIFE is free software; you can redistribute it and/or modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, 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 unsigned long 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 } | |
58 m_time_delta = m_current_time; | |
59 m_current_time = SDL_GetTicks(); | |
60 m_time_delta = m_current_time - m_time_delta; | |
61 m_average_frame_time = m_average_frame_time * avg_multiplier + | |
62 double(m_time_delta) * (1.0 - avg_multiplier); | |
63 | |
64 // Update live events. | |
65 // | |
66 // It is very important to NOT use iterators (over a vector) | |
67 // here, as an event might add enough events to resize the vector. | |
68 // -> Ugly segfault | |
69 for (size_t i = 0; i != m_events_list.size(); ++i) { | |
70 TimeEvent* event = m_events_list[ i ]; | |
71 if( event ) { | |
72 event->managerUpdateEvent(m_current_time); | |
73 } | |
74 } | |
75 | |
76 // Remove dead events | |
77 std::vector<TimeEvent*>::iterator it; | |
78 it = std::remove( m_events_list.begin(), m_events_list.end(), static_cast<TimeEvent*>(0)); | |
79 m_events_list.erase( it, m_events_list.end()); | |
80 } | |
81 | |
82 void TimeManager::registerEvent(TimeEvent* event) { | |
83 // Register. | |
84 m_events_list.push_back(event); | |
85 } | |
86 | |
87 void TimeManager::unregisterEvent(TimeEvent* event) { | |
88 // Unregister. | |
89 std::vector<TimeEvent*>::iterator i = m_events_list.begin(); | |
90 for(; i != m_events_list.end(); ++i) { | |
91 if( (*i) == event ) { | |
92 *i = 0; | |
93 return; | |
94 } | |
95 } | |
96 } | |
97 | |
98 unsigned long TimeManager::getTime() const { | |
99 return m_current_time; | |
100 } | |
101 | |
102 unsigned long TimeManager::getTimeDelta() const { | |
103 return m_time_delta; | |
104 } | |
105 | |
106 double TimeManager::getAverageFrameTime() const { | |
107 return m_average_frame_time; | |
108 } | |
109 | |
110 void TimeManager::printStatistics() const { | |
111 FL_LOG(_log, LMsg("Timers: ") << m_events_list.size()); | |
112 } | |
113 | |
114 } //FIFE | |
115 | |
116 |