| 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 |  *      Reto Grieder | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |     @file | 
|---|
| 31 |     @ingroup Util | 
|---|
| 32 | */ | 
|---|
| 33 |  | 
|---|
| 34 | #ifndef _Clock_H__ | 
|---|
| 35 | #define _Clock_H__ | 
|---|
| 36 |  | 
|---|
| 37 | #include "UtilPrereqs.h" | 
|---|
| 38 | #include "OgreForwardRefs.h" | 
|---|
| 39 |  | 
|---|
| 40 | namespace orxonox | 
|---|
| 41 | { | 
|---|
| 42 |     /** Simple real time clock based on Ogre::Timer | 
|---|
| 43 |     @details | 
|---|
| 44 |         The class can be used to both capture the current real time or to | 
|---|
| 45 |         incrementally capture the time and then distribute that time information | 
|---|
| 46 |         via Clock& references (for instance for the game tick). <br> | 
|---|
| 47 |         Precision: <br> | 
|---|
| 48 |     @par Precision | 
|---|
| 49 |         The maximum precision is given by the Ogre::Timer and that is somewhere | 
|---|
| 50 |         in the microsecond range for both Windows and UNIX. | 
|---|
| 51 |     @par Remarks for Usage on Windows | 
|---|
| 52 |         For proper functionality this class MUST be used in the same thread! <br> | 
|---|
| 53 |         Furthermore it might be possible that the Ogre::Timer has a performance | 
|---|
| 54 |         caveat because it will only capture the time on the same CPU core. | 
|---|
| 55 |         Confining the main thread to one process could speed up the game. | 
|---|
| 56 |         See \ref cmdargspage "Ccommandline Argument" 'limitToCPU' (only on Windows) | 
|---|
| 57 |     */ | 
|---|
| 58 |     class _UtilExport Clock | 
|---|
| 59 |     { | 
|---|
| 60 |     public: | 
|---|
| 61 |         /// Starts the time at 0 | 
|---|
| 62 |         Clock(); | 
|---|
| 63 |         ~Clock(); | 
|---|
| 64 |  | 
|---|
| 65 |         /** Internally captures the time and stays at that particular time | 
|---|
| 66 |         @remarks | 
|---|
| 67 |             Mind the types! Ogre::Timer::getMicroseconds() will return an unsigned | 
|---|
| 68 |             long, which will eventually overflow. But if you use the subtraction of | 
|---|
| 69 |             the current time minus the last time the timer gave us and sum these up to | 
|---|
| 70 |             a 64 bit integer, we get the desired result. <br> | 
|---|
| 71 |             Also mind that we don't have to store the last timer's time as unsigned long | 
|---|
| 72 |             as well because (unsigned long)tickTime_ will do exactly that. | 
|---|
| 73 |         */ | 
|---|
| 74 |         void capture(); | 
|---|
| 75 |  | 
|---|
| 76 |         /// Returns the last captured absolute time in microseconds | 
|---|
| 77 |         unsigned long long getMicroseconds() const | 
|---|
| 78 |             { return tickTime_; } | 
|---|
| 79 |         /// Returns the last captured absolute time in milliseconds | 
|---|
| 80 |         unsigned long long getMilliseconds() const | 
|---|
| 81 |             { return tickTime_ / 1000; } | 
|---|
| 82 |         /// Returns the last captured absolute time in seconds | 
|---|
| 83 |         unsigned long getSeconds() const | 
|---|
| 84 |             { return static_cast<long> (tickTime_ / 1000000); } | 
|---|
| 85 |         /// Returns the last captured absolute time in seconds as float | 
|---|
| 86 |         float getSecondsPrecise() const | 
|---|
| 87 |             { return static_cast<float>(tickTime_ / 1000000.0f); } | 
|---|
| 88 |  | 
|---|
| 89 |         /// Returns the timespan in seconds between the last two calls to capture() | 
|---|
| 90 |         float getDeltaTime() const | 
|---|
| 91 |             { return tickDtFloat_; } | 
|---|
| 92 |         /// Returns the timespan in microseconds between the last two calls to capture() | 
|---|
| 93 |         long getDeltaTimeMicroseconds() const | 
|---|
| 94 |             { return tickDt_; } | 
|---|
| 95 |  | 
|---|
| 96 |         /** Returns the current real time in microseconds | 
|---|
| 97 |         @note | 
|---|
| 98 |             This is especially useful to measure execution times because of the | 
|---|
| 99 |             high precision. | 
|---|
| 100 |         */ | 
|---|
| 101 |         unsigned long long getRealMicroseconds() const; | 
|---|
| 102 |  | 
|---|
| 103 |     private: | 
|---|
| 104 |         /// Undefined | 
|---|
| 105 |         Clock(const Clock& instance); | 
|---|
| 106 |  | 
|---|
| 107 |         Ogre::Timer*       timer_;       ///< Ogre timer object | 
|---|
| 108 |         unsigned long long tickTime_;    ///< Currently captured time | 
|---|
| 109 |         long               tickDt_;      ///< Delta time in microseconds (cache value) | 
|---|
| 110 |         float              tickDtFloat_; ///< Delta time in seconds (cache value) | 
|---|
| 111 |     }; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | #endif /* _Clock_H__ */ | 
|---|