Changeset 995 for code/branches/core2/src/orxonox/tools
- Timestamp:
- Apr 5, 2008, 6:46:43 PM (17 years ago)
- Location:
- code/branches/core2/src/orxonox/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core2/src/orxonox/tools/Timer.cc
r871 r995 1 /* 2 * ORXONOX - the hottest 3D action shooter ever to exist 3 * 4 * 5 * License notice: 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 * 21 * Author: 22 * Fabian 'x3n' Landau 23 * Co-authors: 24 * ... 25 * 26 */ 27 28 #include "core/Executor.h" 1 29 #include "core/CoreIncludes.h" 30 #include "core/ConsoleCommand.h" 31 #include "core/CommandExecutor.h" 2 32 #include "Timer.h" 3 33 4 34 namespace orxonox 5 35 { 36 ConsoleCommandShortcutExtern(delay, AccessLevel::None); 37 38 void delay(float delay, const std::string& command) 39 { 40 StaticTimer *delaytimer = new StaticTimer(); 41 ExecutorStatic* delayexecutor = createExecutor(createFunctor(&executeDelayedCommand)); 42 delayexecutor->setDefaultValues(delaytimer, command); 43 delaytimer->setTimer(delay, false, delayexecutor); 44 } 45 46 void executeDelayedCommand(StaticTimer* timer, const std::string& command) 47 { 48 CommandExecutor::execute(command); 49 delete timer; 50 } 51 6 52 /** 7 53 @brief Constructor: Sets the default-values. … … 11 57 RegisterRootObject(TimerBase); 12 58 59 this->executor_ = 0; 13 60 this->interval_ = 0; 14 61 this->bLoop_ = false; … … 17 64 this->time_ = 0; 18 65 } 66 67 TimerBase::~TimerBase() 68 { 69 delete this->executor_; 70 } 71 72 void TimerBase::run() const 73 { 74 (*this->executor_)(); 75 } 19 76 } -
code/branches/core2/src/orxonox/tools/Timer.h
r970 r995 43 43 44 44 source.cc: 45 include "core/Executor.h" 46 45 47 ClassName::ClassName() 46 48 { 47 myTimer.setTimer(interval_in_seconds, bLoop, this, &ClassName::functionName);49 myTimer.setTimer(interval_in_seconds, bLoop, this, createExecutor(createFunctor(&ClassName::functionName))); 48 50 } 49 51 … … 59 61 60 62 #include <OgreFrameListener.h> 61 #include "../OrxonoxPrereqs.h" 63 #include "OrxonoxPrereqs.h" 64 #include "core/CorePrereqs.h" 62 65 63 66 namespace orxonox 64 67 { 68 class StaticTimer; 69 void delay(float delay, const std::string& command); 70 void executeDelayedCommand(StaticTimer* timer, const std::string& command); 71 65 72 //! TimerBase is the parent of the Timer class. 66 73 class _OrxonoxExport TimerBase : public OrxonoxClass … … 69 76 70 77 public: 78 ~TimerBase(); 79 80 void run() const; 81 82 /** @brief Starts the Timer: Function-call after 'interval' seconds. */ 83 inline void startTimer() 84 { this->bActive_ = true; this->time_ = this->interval_; } 85 /** @brief Stops the Timer. */ 86 inline void stopTimer() 87 { this->bActive_ = false; this->time_ = this->interval_; } 88 /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ 89 inline void pauseTimer() 90 { this->bActive_ = false; } 91 /** @brief Unpauses the Timer - continues with the given state. */ 92 inline void unpauseTimer() 93 { this->bActive_ = true; } 94 /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */ 95 inline bool isActive() const 96 { return this->bActive_; } 97 /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */ 98 inline void addTime(float time) 99 { this->time_ += time; } 100 /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */ 101 inline void removeTime(float time) 102 { this->time_ -= time; } 103 /** @brief Sets the interval of the Timer. @param interval The interval */ 104 inline void setInterval(float interval) 105 { this->interval_ = interval; } 106 /** @brief Sets bLoop to a given value. @param bLoop True = loop */ 107 inline void setLoop(bool bLoop) 108 { this->bLoop_ = bLoop; } 109 110 protected: 71 111 TimerBase(); 72 112 73 virtual void run() const = 0; 74 75 /** @brief Starts the Timer: Function-call after 'interval' seconds. */ 76 inline void startTimer() { this->bActive_ = true; this->time_ = this->interval_; } 77 /** @brief Stops the Timer. */ 78 inline void stopTimer() { this->bActive_ = false; this->time_ = this->interval_; } 79 /** @brief Pauses the Timer - it will continue with the actual state if you unpause it. */ 80 inline void pauseTimer() { this->bActive_ = false; } 81 /** @brief Unpauses the Timer - continues with the given state. */ 82 inline void unpauseTimer() { this->bActive_ = true; } 83 /** @brief Returns true if the Timer is active (= not stoped, not paused). @return True = Time is active */ 84 inline bool isActive() const { return this->bActive_; } 85 /** @brief Gives the Timer some extra time. @param time The amount of extra time in seconds */ 86 inline void addTime(float time) { this->time_ += time; } 87 /** @brief Decreases the remaining time of the Timer. @param time The amount of time to remove */ 88 inline void removeTime(float time) { this->time_ -= time; } 89 90 protected: 91 float interval_; //!< The time-interval in seconds 92 bool bLoop_; //!< If true, the function gets called every 'interval' seconds 93 bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up 94 95 float time_; //!< Internal variable, counting the time till the next function-call 113 Executor* executor_; //!< The executor of the function that should be called when the time expires 114 115 float interval_; //!< The time-interval in seconds 116 bool bLoop_; //!< If true, the function gets called every 'interval' seconds 117 bool bActive_; //!< If true, the Timer ticks and calls the function if the time's up 118 119 float time_; //!< Internal variable, counting the time till the next function-call 96 120 }; 97 121 … … 101 125 { 102 126 public: 103 /** @brief Constructor: Sets the default-values. */ 104 Timer() 105 { 106 this->timerFunction_ = 0; 107 this->object_ = 0; 108 } 127 Timer() {} 109 128 110 129 /** … … 113 132 @param bLoop If true, the function gets called every 'interval' seconds 114 133 @param object The object owning the timer and the function 115 @param timerFunction A function pointer tothe function to call116 */ 117 Timer(float interval, bool bLoop, T* object, void (T::*timerFunction)())118 { 119 this->setTimer(interval, bLoop, timerFunction, object);134 @param exeuctor A executor of the function to call 135 */ 136 Timer(float interval, bool bLoop, T* object, ExecutorMember<T>* exeuctor) 137 { 138 this->setTimer(interval, bLoop, object, exeuctor); 120 139 } 121 140 … … 125 144 @param bLoop If true, the function gets called every 'interval' seconds 126 145 @param object The object owning the timer and the function 127 @param timerFunction A function pointer tothe function to call128 */ 129 void setTimer(float interval, bool bLoop, T* object, void (T::*timerFunction)())146 @param exeuctor A executor of the function to call 147 */ 148 void setTimer(float interval, bool bLoop, T* object, ExecutorMember<T>* executor) 130 149 { 131 150 this->interval_ = interval; 132 151 this->bLoop_ = bLoop; 133 this->timerFunction_ = timerFunction;134 this-> object_ = object;152 executor->setObject(object); 153 this->executor_ = (Executor*)executor; 135 154 this->bActive_ = true; 136 155 137 156 this->time_ = interval; 138 157 } 139 140 /** @brief Calls the given function of the given object. */ 141 void run() const 142 { 143 ((*this->object_).*timerFunction_)(); 144 } 145 146 private: 147 void (T::*timerFunction_)(); 148 T* object_; 158 }; 159 160 //! The StaticTimer is a callback-object, calling a static function after a given time-interval. 161 class StaticTimer : public TimerBase 162 { 163 public: 164 StaticTimer() {} 165 166 /** 167 @brief Constructor: Initializes the Timer with given values. 168 @param interval The timer-interval in seconds 169 @param bLoop If true, the function gets called every 'interval' seconds 170 @param exeuctor A executor of the function to call 171 */ 172 StaticTimer(float interval, bool bLoop, ExecutorStatic* executor) 173 { 174 this->setTimer(interval, bLoop, executor); 175 } 176 177 /** 178 @brief Initializes the Timer with given values. 179 @param interval The timer-interval in seconds 180 @param bLoop If true, the function gets called every 'interval' seconds 181 @param object The object owning the timer and the function 182 @param executor A executor of the function to call 183 */ 184 void setTimer(float interval, bool bLoop, ExecutorStatic* executor) 185 { 186 this->interval_ = interval; 187 this->bLoop_ = bLoop; 188 this->executor_ = (Executor*)executor; 189 this->bActive_ = true; 190 191 this->time_ = interval; 192 } 149 193 }; 150 194
Note: See TracChangeset
for help on using the changeset viewer.