1 /************************************************************************ 2 * * 3 * FreeSynd - a remake of the classic Bullfrog game "Syndicate". * 4 * * 5 * Copyright (C) 2012 Benoit Blancard <benblan@users.sourceforge.net>* 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 as * 9 * published by the Free Software Foundation; either version 2 of the * 10 * 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 GNU * 15 * General Public License for more details. * 16 * * 17 * You can view the GNU General Public License, online, at the GNU * 18 * project's web site; see <http://www.gnu.org/licenses/gpl.html>. * 19 * The full text of the license is also included in the file COPYING. * 20 * * 21 ************************************************************************/ 22 23 #ifndef UTILS_TIMER_H_ 24 #define UTILS_TIMER_H_ 25 26 #include "common.h" 27 namespace fs_utils { 28 29 /*! 30 * A simple timer implementation. Create the timer with 31 * max time and call update in each frame animation with the 32 * elapsed time since the last call. 33 * When the maximum is reached, update() returns true otherwise 34 * it returns false. 35 * Call reset to set the timer to zero. 36 */ 37 class Timer { 38 public: 39 /*! 40 * Contructor to set the max time. 41 */ Timer(uint32 i_max)42 Timer(uint32 i_max) { 43 reset(i_max); 44 } 45 46 /*! 47 * Reset the counter. 48 */ reset()49 void reset() { 50 i_counter_ = 0; 51 } 52 53 /*! 54 * Reset the counter and sets a new max time. 55 */ reset(uint32 i_max)56 void reset(uint32 i_max) { 57 i_counter_ = 0; 58 i_max_ = i_max; 59 } 60 61 /*! 62 * Adds time to time and returns true if max is reached. 63 * If max is reached, counter is reset. 64 * \return True if timer has reached max. 65 */ update(uint32 elapsed)66 bool update(uint32 elapsed) { 67 i_counter_ += elapsed; 68 if (i_counter_ > i_max_) { 69 i_counter_ = 0; 70 return true; 71 } 72 return false; 73 } 74 75 /*! 76 * Set the counter to max so next time update is called, 77 * it automatically returns true. 78 * Useful when you want to force the timer to pass next time update is called. 79 */ setToMax()80 void setToMax() { 81 i_counter_ = i_max_; 82 } 83 84 private: 85 uint32 i_counter_; 86 uint32 i_max_; 87 }; 88 89 /*! 90 * A convenient timer to store a boolean value. 91 * Each time the timer reaches the max, the value is set to the opposite. 92 * Call state() to get the current state. 93 */ 94 class BoolTimer : public Timer { 95 public: 96 /*! 97 * Contructor to set the max time. 98 * \param i_max Maximum value of the counter 99 * \param b_state The initial state. 100 */ BoolTimer(uint32 i_max,bool b_state)101 BoolTimer(uint32 i_max, bool b_state) : Timer(i_max) { 102 b_state_ = b_state; 103 } 104 105 //! Return the current state state()106 bool state() { return b_state_; } 107 108 /*! 109 * Adds time to time and returns true if max is reached. 110 * If max is reached, counter is reset and state value is set 111 * to the opposite. 112 * \return True if timer has reached max. 113 */ update(uint32 elapsed)114 bool update(uint32 elapsed) { 115 bool res = Timer::update(elapsed); 116 if (res) { 117 b_state_ = !b_state_; 118 } 119 return res; 120 } 121 122 private: 123 bool b_state_; 124 }; 125 }; 126 127 #endif // UTILS_TIMER_H_ 128