hx3d  1
2D/3D Simple Game Framework
callback_timer.cpp
1 /*
2  Callback Timer.
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/callback_timer.hpp"
22 
23 namespace hx3d {
24 
26  _timer(-1), _loop(false) {}
27 
28 CallbackTimer::CallbackTimer(float delay, std::function<void()> function, bool loop):
29  _timer(delay), _function(function), _loop(loop) {}
30 
31 void CallbackTimer::initialize(float delay, std::function<void()> function, bool loop) {
32  _timer.initialize(delay);
33 
34  _loop = loop;
35  _function = function;
36 }
37 
39  return _timer.remaining();
40 }
41 
42 void CallbackTimer::update(float delta) {
43  _timer.update(delta);
44 
45  if (_timer.hasEnded()) {
46  if (_function)
47  _function();
48 
49  if (_loop) {
50  reset();
51  }
52  }
53 }
54 
56  return _timer.hasEnded();
57 }
58 
60  _timer.reset();
61 }
62 
64  return _loop;
65 }
66 
67 } /* hx3d */
void update(float delta)
Update the timer.
Definition: timer.cpp:76
void reset()
Reset the timer.
Definition: timer.cpp:41
void initialize(long delay, bool loop=false)
Initialize the timer with a delay.
Definition: timer.cpp:34
bool hasEnded()
Test if the timer has ended.
Definition: timer.cpp:58
void reset()
Reset the timer.
hx3d framework namespace
Definition: audio.hpp:26
void update(float delta)
Update the timer.
void initialize(float delay, std::function< void()> function, bool loop=false)
Initialize the timer. The delay is in milliseconds.
long remaining()
Get the remaining time in milliseconds.
bool isLooping()
Is the timer looping ?
CallbackTimer()
Create an uninitialized callback timer.
long remaining()
Get the remaining time as milliseconds.
Definition: timer.cpp:46
bool hasEnded()
Did the timer ended ?