| 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 |
#ifndef FIFE_TIMER_H |
| 23 |
#define FIFE_TIMER_H |
| 24 |
|
| 25 |
// Standard C++ library includes |
| 26 |
#include <functional> |
| 27 |
|
| 28 |
// 3rd party library includes |
| 29 |
|
| 30 |
// FIFE includes |
| 31 |
// These includes are split up in two parts, separated by one empty line |
| 32 |
// First block: files included from the FIFE root src directory |
| 33 |
// Second block: files included from the same folder |
| 34 |
#include "timeevent.h" |
| 35 |
|
| 36 |
namespace FIFE { |
| 37 |
|
| 38 |
/** Simple Timer class |
| 39 |
* |
| 40 |
* This is a simple wrapper around the TimeEvent, |
| 41 |
* which makes the later usable without having to |
| 42 |
* subclass it every time. |
| 43 |
* |
| 44 |
* @code |
| 45 |
* m_timer.setInterval(1000); |
| 46 |
* m_timer.setCallback(std::bind(&Class::update, this) ); |
| 47 |
* m_timer.start(); |
| 48 |
* @endcode |
| 49 |
* |
| 50 |
* @warning Note that the callback must be available |
| 51 |
* when it is triggered. If your called function is a |
| 52 |
* bound member function and it goes out of scope, |
| 53 |
* when the callback is called you'll most likely |
| 54 |
* have a segfault. So this is best used as a member |
| 55 |
* calling an update function. |
| 56 |
* |
| 57 |
* @note You don't have to call TimeManager::registerEvent @b ever |
| 58 |
* |
| 59 |
* @see Console |
| 60 |
*/ |
| 61 |
class Timer : protected TimeEvent { |
| 62 |
public: |
| 63 |
typedef std::function<void()> type_callback; |
| 64 |
|
| 65 |
/** Default constructor. |
| 66 |
* |
| 67 |
* Constructs an idle timer, use @see setInterval and @see setCallback |
| 68 |
* to set it up for use. |
| 69 |
*/ |
| 70 |
Timer(); |
| 71 |
|
| 72 |
/** Destructor. |
| 73 |
* |
| 74 |
* Stops and destroys the timer. |
| 75 |
*/ |
| 76 |
virtual ~Timer(); |
| 77 |
|
| 78 |
/** Set the interval in milliseconds |
| 79 |
* |
| 80 |
* @param msec The interval |
| 81 |
* If you use an intervall 0 the timer will fire every frame. |
| 82 |
*/ |
| 83 |
void setInterval(int32_t msec); |
| 84 |
|
| 85 |
/** Start the timer |
| 86 |
* |
| 87 |
* Without calling this function, nothing will happen. |
| 88 |
* It is save to call this more than once. |
| 89 |
*/ |
| 90 |
void start(); |
| 91 |
|
| 92 |
/** Stop the timer |
| 93 |
* |
| 94 |
* Stops execution of the callback |
| 95 |
* It is save to call this more than once. |
| 96 |
*/ |
| 97 |
void stop(); |
| 98 |
|
| 99 |
/** Set the callback that will be called |
| 100 |
* |
| 101 |
* @param callback A @c std::function returning void |
| 102 |
*/ |
| 103 |
void setCallback(const type_callback& callback); |
| 104 |
|
| 105 |
protected: |
| 106 |
bool m_active; |
| 107 |
type_callback m_callback; |
| 108 |
void updateEvent(uint32_t); |
| 109 |
}; |
| 110 |
|
| 111 |
}//FIFE |
| 112 |
|
| 113 |
#endif |