Mercurial > fife-parpg
comparison engine/core/util/time/timer.h @ 0:4a0efb7baf70
* Datasets becomes the new trunk and retires after that :-)
author | mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Sun, 29 Jun 2008 18:44:17 +0000 |
parents | |
children | 90005975cdbb |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 /*************************************************************************** | |
2 * Copyright (C) 2005-2008 by the FIFE team * | |
3 * http://www.fifengine.de * | |
4 * This file is part of FIFE. * | |
5 * * | |
6 * FIFE is free software; you can redistribute it and/or modify * | |
7 * it under the terms of the GNU General Public License as published by * | |
8 * the Free Software Foundation; either version 2 of the License, or * | |
9 * (at your option) any later version. * | |
10 * * | |
11 * This program 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 * | |
14 * GNU General Public License for more details. * | |
15 * * | |
16 * You should have received a copy of the GNU General Public License * | |
17 * along with this program; if not, write to the * | |
18 * Free Software Foundation, Inc., * | |
19 * 51 Franklin St, 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 | |
27 // 3rd party library includes | |
28 #include <boost/function.hpp> | |
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 class TimerListener { | |
39 public: | |
40 virtual void onTimer() = 0; | |
41 virtual ~TimerListener() {} | |
42 }; | |
43 | |
44 /** Simple Timer class | |
45 * | |
46 * This is a simple wrapper around the TimeEvent, | |
47 * which makes the later usable without having to | |
48 * subclass it every time. | |
49 * | |
50 * @example MemberUpdater | |
51 * @code | |
52 * m_timer.setInterval(1000); | |
53 * m_timer.setCallback( boost::bind(&Class::update, this) ); | |
54 * m_timer.start(); | |
55 * @endcode | |
56 * | |
57 * @warning Note that the callback must be available | |
58 * when it is triggered. If your called function is a | |
59 * bound member function and it goes out of scope, | |
60 * when the callback is called you'll most likely | |
61 * have a segfault. So this is best used as a member | |
62 * calling an update function. | |
63 * | |
64 * @note You don't have to call TimeManager::registerEvent @b ever | |
65 * | |
66 * @see Console | |
67 */ | |
68 class Timer : protected TimeEvent { | |
69 public: | |
70 typedef boost::function0<void> type_callback; | |
71 | |
72 /** Default constructor. | |
73 * | |
74 * Constructs an idle timer, use @see setInterval and @see setCallback | |
75 * to set it up for use. | |
76 */ | |
77 Timer(); | |
78 | |
79 /** Destructor. | |
80 * | |
81 * Stops and destroys the timer. | |
82 */ | |
83 virtual ~Timer(); | |
84 | |
85 /** Set the interval in milliseconds | |
86 * | |
87 * @param msec The interval | |
88 * If you use an intervall 0 the timer will fire every frame. | |
89 */ | |
90 void setInterval(unsigned long msec); | |
91 | |
92 /** Start the timer | |
93 * | |
94 * Without calling this function, nothing will happen. | |
95 * It is save to call this more than once. | |
96 */ | |
97 void start(); | |
98 | |
99 /** Stop the timer | |
100 * | |
101 * Stops execution of the callback | |
102 * It is save to call this more than once. | |
103 */ | |
104 void stop(); | |
105 | |
106 /** Set the callback that will be called | |
107 * | |
108 * @param callback A @c boost::function0 returning void | |
109 */ | |
110 void setCallback(const type_callback& callback); | |
111 | |
112 /** Set the listener that will be called | |
113 * | |
114 */ | |
115 void setListener(TimerListener* listener); | |
116 | |
117 | |
118 protected: | |
119 bool m_active; | |
120 type_callback m_callback; | |
121 TimerListener* m_listener; | |
122 void updateEvent(unsigned long); | |
123 }; | |
124 | |
125 }//FIFE | |
126 | |
127 #endif |