hx3d  1
2D/3D Simple Game Framework
timer_manager.cpp
1 /*
2  Timer manager.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/utils/timer_manager.hpp"
22 
23 #include "hx3d/utils/algorithm.hpp"
24 #include "hx3d/utils/log.hpp"
25 
26 namespace hx3d {
27 
28 TimerManager::TimerManager() {}
29 
30 void TimerManager::addNamedTimer(std::string name, CallbackTimer& timer) {
31  _registered[name] = timer;
32 }
33 
34 void TimerManager::createNamedTimer(std::string name, float delay, std::function<void()> callback, bool loop) {
35  _registered[name].initialize(delay, callback, loop);
36 }
37 
38 void TimerManager::resetNamedTimer(std::string name) {
39  _registered[name].reset();
40 }
41 
42 void TimerManager::removeNamedTimer(std::string name) {
43  if (_registered.find(name) != _registered.end()) {
44  _registered.erase(name);
45  }
46 }
47 
49  _temporaries.push_back(timer);
50 }
51 
52 void TimerManager::createTemporaryTimer(float delay, std::function<void()> callback, bool loop) {
53  CallbackTimer timer;
54  timer.initialize(delay, callback, loop);
55 
56  _temporaries.push_back(timer);
57 }
58 
59 void TimerManager::update(float delta) {
60  std::vector<std::string> toRemoveReg;
61  std::vector<int> toRemoveTemp;
62 
63  for (auto& pair: _registered) {
64  auto& reg = pair.second;
65  reg.update(delta);
66 
67  if (reg.hasEnded()) {
68  toRemoveReg.push_back(pair.first);
69  }
70  }
71 
72  int i = 0;
73  algo::apply(_temporaries, [&i,&toRemoveTemp,delta](CallbackTimer& reg) {
74  reg.update(delta);
75  if (reg.hasEnded()) {
76  toRemoveTemp.push_back(i);
77  }
78 
79  ++i;
80  });
81 
82  while (!toRemoveReg.empty()) {
83  _registered.erase(toRemoveReg.back());
84  }
85 
86  while (!toRemoveTemp.empty()) {
87  _temporaries.erase(_temporaries.begin() + toRemoveTemp.back());
88  }
89 }
90 
92  _registered.clear();
93  _temporaries.clear();
94 }
95 
96 } /* hx3d */
void addTemporaryTimer(CallbackTimer &timer)
Add an existing temporary timer.
void clear()
Clear all the timers.
hx3d framework namespace
Definition: audio.hpp:26
void resetNamedTimer(std::string name)
Reset a named timer.
void update(float delta)
Update the timer.
Improved timer with callback execution.
void initialize(float delay, std::function< void()> function, bool loop=false)
Initialize the timer. The delay is in milliseconds.
void removeNamedTimer(std::string name)
Remove a named timer.
void createTemporaryTimer(float delay, std::function< void()> callback, bool loop=false)
Create a new temporary timer.
void apply(Container &container, Function func)
Function application helper on a container.
void update(float delta)
Update the timers.
bool hasEnded()
Did the timer ended ?
void createNamedTimer(std::string name, float delay, std::function< void()> callback, bool loop=false)
Create a new named timer.
void addNamedTimer(std::string name, CallbackTimer &timer)
Add an existing named timer.