FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
timemanager.cpp
Go to the documentation of this file.
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 
43  m_current_time (0),
44  m_time_delta(UNDEFINED_TIME_DELTA),
45  m_average_frame_time(0) {
46  }
47 
49  }
50 
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 {
60  m_current_time = SDL_GetTicks();
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 
85  // Register.
86  m_events_list.push_back(event);
87  }
88 
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 
101  return m_current_time;
102  }
103 
105  return m_time_delta;
106  }
107 
109  return m_average_frame_time;
110  }
111 
113  FL_LOG(_log, LMsg("Timers: ") << m_events_list.size());
114  }
115 
116 } //FIFE
117 
118 
uint32_t getTime() const
Get the time.
void registerEvent(TimeEvent *event)
Adds a TimeEvent.
Definition: timemanager.cpp:84
Helper class to create log strings out from separate parts Usage: LMsg("some text") << variable << "...
Definition: logger.h:82
uint32_t getTimeDelta() const
Get the time since the last frame.
static Logger _log(LM_AUDIO)
static const uint32_t UNDEFINED_TIME_DELTA
Definition: timemanager.cpp:39
double m_average_frame_time
Average frame time in milliseconds.
Definition: timemanager.h:106
void unregisterEvent(TimeEvent *event)
Removes a TimeEvent.
Definition: timemanager.cpp:89
Interface for events to be registered with TimeManager.
Definition: timeevent.h:48
double getAverageFrameTime() const
Gets average frame time.
uint32_t m_current_time
Current time in milliseconds.
Definition: timemanager.h:102
std::vector< TimeEvent * > m_events_list
List of active TimeEvents.
Definition: timemanager.h:109
uint32_t m_time_delta
Time since last frame in milliseconds.
Definition: timemanager.h:104
virtual ~TimeManager()
Destructor.
Definition: timemanager.cpp:48
#define FL_LOG(logger, msg)
Definition: logger.h:71
TimeManager()
Default constructor.
Definition: timemanager.cpp:42
void update()
Called once a frame and updates the timer objects and events.
Definition: timemanager.cpp:51
void printStatistics() const
Prints Timer statistics.
Create a Logger instance to communicate with LogManager Logger stores information about the current m...
Definition: logger.h:211
unsigned int uint32_t
Definition: core.h:40