Version 4 (modified by landauf, 8 years ago) (diff) |
---|
Timer
Description
The Timer calls a function after a specific amount of time. The function has to be a member-function of the following form:
void SomeClass::function(void);
The Timer allows you to call a function every xxx seconds (loop) or once after xxx seconds (no loop). You can stop and restart the Timer or pause/unpause it. In some cases the Timer can replace the tick(dt) function of an object or just simplify the process of waiting a specific amount of time befor doing some action (for example if you want to delete an object after 10 seconds).
Functions
- Creation:
- Timer(interval, bLoop, object, timerFunction): The constructor creates a new Timer that calls the timerFunction of object after interval seconds and loops if bLoop is true.
- setTimer(interval, bLoop, object, timerFunction): Does exactly the same like the constructor.
- Manipulation:
- startTimer(): (Re)starts the timer.
- stopTimer(): Stops the timer. You can activate it againg by restarting it.
- pauseTimer(): Pauses the timer - the current state gets saved, unpausing is possible.
- unpauseTimer(): Unpauses the timer - it continuous with the saved state.
Example
Definition of a class with timer:
class MyClass { public: MyClass(); private: void beep(); Timer<MyClass> timer_; };
Implementation of the class with timer:
MyClass::MyClass() { std::cout << "Created MyClass." << std::endl; this->timer_.setTimer(3.0, true, this, &MyClass::beep); } void MyClass::beep() { std::cout << "beep!" << std::endl; }
Output: One "beep!" every 3 seconds
Created MyClass. beep! beep! beep! beep! beep! beep! beep! beep! beep! ...