hx3d  1
2D/3D Simple Game Framework
timer.cpp
1 /*
2  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/timer.hpp"
22 
23 #include <algorithm>
24 
25 namespace hx3d {
26 
28  Timer(-1) {}
29 
30 Timer::Timer(long delay, bool loop):
31  _delay(delay), _elapsed(0), _alreadyEnded(false), _loop(loop)
32 {}
33 
34 void Timer::initialize(long delay, bool loop) {
35  _delay = delay;
36  _loop = loop;
37 
38  reset();
39 }
40 
41 void Timer::reset() {
42  _elapsed = 0;
43  _alreadyEnded = false;
44 }
45 
47  if (_delay == -1)
48  return 0xFF;
49 
50  long elapsed = _elapsed * 1000;
51  return std::max(0L, _delay - elapsed);
52 }
53 
55  return _loop;
56 }
57 
59 
60  if (_alreadyEnded)
61  return false;
62 
63  if (remaining() == 0) {
64  if (_loop) {
65  reset();
66  return true;
67  } else {
68  _alreadyEnded = true;
69  return true;
70  }
71  }
72 
73  return false;
74 }
75 
76 void Timer::update(float delta) {
77  _elapsed += delta;
78 }
79 
80 } /* hx3d */
Simple timer.
Definition: timer.hpp:31
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
bool isLooping()
Is the timer looping ?
Definition: timer.cpp:54
hx3d framework namespace
Definition: audio.hpp:26
Timer()
Create an uninitialized timer. See initialize.
Definition: timer.cpp:27
long remaining()
Get the remaining time as milliseconds.
Definition: timer.cpp:46