| [1505] | 1 | /* | 
|---|
|  | 2 | *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
|  | 3 | *                    > www.orxonox.net < | 
|---|
|  | 4 | * | 
|---|
|  | 5 | * | 
|---|
|  | 6 | *   License notice: | 
|---|
|  | 7 | * | 
|---|
|  | 8 | *   This program is free software; you can redistribute it and/or | 
|---|
|  | 9 | *   modify it under the terms of the GNU General Public License | 
|---|
|  | 10 | *   as published by the Free Software Foundation; either version 2 | 
|---|
|  | 11 | *   of the License, or (at your option) any later version. | 
|---|
|  | 12 | * | 
|---|
|  | 13 | *   This program is distributed in the hope that it will be useful, | 
|---|
|  | 14 | *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
|  | 15 | *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
|  | 16 | *   GNU General Public License for more details. | 
|---|
|  | 17 | * | 
|---|
|  | 18 | *   You should have received a copy of the GNU General Public License | 
|---|
|  | 19 | *   along with this program; if not, write to the Free Software | 
|---|
|  | 20 | *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
|  | 21 | * | 
|---|
|  | 22 | *   Author: | 
|---|
|  | 23 | *      Fabian 'x3n' Landau | 
|---|
|  | 24 | *   Co-authors: | 
|---|
|  | 25 | *      ... | 
|---|
|  | 26 | * | 
|---|
|  | 27 | */ | 
|---|
|  | 28 |  | 
|---|
|  | 29 | /*! | 
|---|
| [2171] | 30 | @file | 
|---|
| [1505] | 31 | @brief Definition and Implementation of the Timer class. | 
|---|
|  | 32 |  | 
|---|
|  | 33 | The Timer is a callback-object, calling a given function after a given time-interval. | 
|---|
|  | 34 |  | 
|---|
|  | 35 | Usage: | 
|---|
|  | 36 | header.h: | 
|---|
|  | 37 | class ClassName | 
|---|
|  | 38 | { | 
|---|
|  | 39 | public: | 
|---|
|  | 40 | ClassName(); | 
|---|
|  | 41 | void functionName(); | 
|---|
| [5929] | 42 | Timer myTimer; | 
|---|
| [1505] | 43 | }; | 
|---|
|  | 44 |  | 
|---|
|  | 45 | source.cc: | 
|---|
|  | 46 | include "core/Executor.h" | 
|---|
|  | 47 |  | 
|---|
|  | 48 | ClassName::ClassName() | 
|---|
|  | 49 | { | 
|---|
| [5929] | 50 | myTimer.setTimer(interval_in_seconds, bLoop, createExecutor(createFunctor(&ClassName::functionName, this))); | 
|---|
| [1505] | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 | void ClassName::functionName() | 
|---|
|  | 54 | { | 
|---|
|  | 55 | whateveryouwant(); | 
|---|
|  | 56 | something(else); | 
|---|
|  | 57 | } | 
|---|
|  | 58 | */ | 
|---|
|  | 59 |  | 
|---|
|  | 60 | #ifndef _Timer_H__ | 
|---|
|  | 61 | #define _Timer_H__ | 
|---|
|  | 62 |  | 
|---|
| [5693] | 63 | #include "tools/ToolsPrereqs.h" | 
|---|
| [3196] | 64 |  | 
|---|
| [2662] | 65 | #include "core/Executor.h" | 
|---|
| [1755] | 66 | #include "core/OrxonoxClass.h" | 
|---|
| [5693] | 67 | #include "tools/interfaces/TimeFactorListener.h" | 
|---|
| [1505] | 68 |  | 
|---|
|  | 69 | namespace orxonox | 
|---|
|  | 70 | { | 
|---|
|  | 71 | void delay(float delay, const std::string& command); | 
|---|
|  | 72 | void killdelays(); | 
|---|
| [5929] | 73 | void executeDelayedCommand(Timer* timer, const std::string& command); | 
|---|
| [1505] | 74 |  | 
|---|
| [5929] | 75 | //! The Timer is a callback-object, calling a given function after a given time-interval. | 
|---|
|  | 76 | class _ToolsExport Timer : public TimeFactorListener | 
|---|
| [1505] | 77 | { | 
|---|
|  | 78 | public: | 
|---|
| [5929] | 79 | Timer(); | 
|---|
|  | 80 | ~Timer(); | 
|---|
| [1505] | 81 |  | 
|---|
| [5929] | 82 | Timer(float interval, bool bLoop, Executor* executor, bool bKillAfterCall = false); | 
|---|
|  | 83 |  | 
|---|
|  | 84 | /** | 
|---|
|  | 85 | @brief Initializes the Timer with given values. | 
|---|
|  | 86 | @param interval The timer-interval in seconds | 
|---|
|  | 87 | @param bLoop If true, the function gets called every 'interval' seconds | 
|---|
|  | 88 | @param object The object owning the timer and the function | 
|---|
|  | 89 | @param executor A executor of the function to call | 
|---|
|  | 90 | */ | 
|---|
|  | 91 | void setTimer(float interval, bool bLoop, Executor* executor, bool bKillAfterCall = false) | 
|---|
|  | 92 | { | 
|---|
|  | 93 | this->deleteExecutor(); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | this->setInterval(interval); | 
|---|
|  | 96 | this->bLoop_ = bLoop; | 
|---|
|  | 97 | this->executor_ = executor; | 
|---|
|  | 98 | this->bActive_ = true; | 
|---|
|  | 99 |  | 
|---|
|  | 100 | this->time_ = this->interval_; | 
|---|
|  | 101 | this->bKillAfterCall_ = bKillAfterCall; | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | void run(); | 
|---|
| [1552] | 105 | void deleteExecutor(); | 
|---|
| [1505] | 106 |  | 
|---|
|  | 107 | /** @brief Starts the Timer: Function-call after 'interval' seconds. */ | 
|---|
|  | 108 | inline void startTimer() | 
|---|
|  | 109 | { this->bActive_ = true; this->time_ = this->interval_; } | 
|---|
|  | 110 | /** @brief Stops the Timer. */ | 
|---|
|  | 111 | inline void stopTimer() | 
|---|
|  | 112 | { this->bActive_ = false; this->time_ = this->interval_; } | 
|---|
|  | 113 | /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ | 
|---|
|  | 114 | inline void pauseTimer() | 
|---|
|  | 115 | { this->bActive_ = false; } | 
|---|
|  | 116 | /** @brief Unpauses the Timer - continues with the given state. */ | 
|---|
|  | 117 | inline void unpauseTimer() | 
|---|
|  | 118 | { this->bActive_ = true; } | 
|---|
|  | 119 | /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */ | 
|---|
|  | 120 | inline bool isActive() const | 
|---|
|  | 121 | { return this->bActive_; } | 
|---|
| [1608] | 122 | /** @brief Returns the remaining time until the Timer calls the function. @return The remaining time */ | 
|---|
|  | 123 | inline float getRemainingTime() const | 
|---|
| [3300] | 124 | { return static_cast<float>(this->time_ / 1000000.0f); } | 
|---|
| [1505] | 125 | /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */ | 
|---|
|  | 126 | inline void addTime(float time) | 
|---|
| [3301] | 127 | { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); } | 
|---|
| [1505] | 128 | /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */ | 
|---|
|  | 129 | inline void removeTime(float time) | 
|---|
| [3301] | 130 | { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); } | 
|---|
| [1505] | 131 | /** @brief Sets the interval of the Timer. @param interval The interval */ | 
|---|
|  | 132 | inline void setInterval(float interval) | 
|---|
| [3301] | 133 | { this->interval_ = static_cast<long long>(interval * 1000000.0f); } | 
|---|
| [1505] | 134 | /** @brief Sets bLoop to a given value. @param bLoop True = loop */ | 
|---|
|  | 135 | inline void setLoop(bool bLoop) | 
|---|
|  | 136 | { this->bLoop_ = bLoop; } | 
|---|
|  | 137 |  | 
|---|
| [1755] | 138 | void tick(const Clock& time); | 
|---|
| [1505] | 139 |  | 
|---|
| [5929] | 140 | private: | 
|---|
|  | 141 | void init(); | 
|---|
| [6387] | 142 |  | 
|---|
| [2087] | 143 | Executor* executor_;  //!< The executor of the function that should be called when the time expires | 
|---|
| [1505] | 144 |  | 
|---|
| [2087] | 145 | long long interval_;  //!< The time-interval in micro seconds | 
|---|
|  | 146 | bool bLoop_;          //!< If true, the function gets called every 'interval' seconds | 
|---|
|  | 147 | bool bActive_;        //!< If true, the Timer ticks and calls the function if the time's up | 
|---|
|  | 148 | bool bKillAfterCall_; //!< If true the timer gets deleted after it called the function | 
|---|
| [1505] | 149 |  | 
|---|
| [2087] | 150 | long long time_;      //!< Internal variable, counting the time till the next function-call | 
|---|
| [1505] | 151 | }; | 
|---|
|  | 152 | } | 
|---|
|  | 153 |  | 
|---|
|  | 154 | #endif /* _Timer_H__ */ | 
|---|