[1674] | 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: |
---|
[5695] | 23 | * Reto Grieder |
---|
[1674] | 24 | * Co-authors: |
---|
[5695] | 25 | * ... |
---|
[1674] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #ifndef _Clock_H__ |
---|
| 30 | #define _Clock_H__ |
---|
| 31 | |
---|
[5855] | 32 | #include "UtilPrereqs.h" |
---|
| 33 | #include "OgreForwardRefs.h" |
---|
[1674] | 34 | |
---|
| 35 | namespace orxonox |
---|
| 36 | { |
---|
[7331] | 37 | /** Simple real time clock based on Ogre::Timer |
---|
| 38 | @details |
---|
| 39 | The class can be used to both capture the current real time or to |
---|
| 40 | incrementally capture the time and then distribute that time information |
---|
| 41 | via Clock& references (for instance for the game tick). <br> |
---|
| 42 | Precision: <br> |
---|
| 43 | The maximum precision is given by the Ogre::Timer and that is somewhere |
---|
| 44 | in the microsecond range for both Windows and UNIX. |
---|
| 45 | @remarks |
---|
| 46 | For proper functionality this class MUST be used in the same thread! <br> |
---|
| 47 | Further more it might be possible that the Ogre::Timer has a performance |
---|
| 48 | caveat on Windows because it will only capture the time on the same |
---|
| 49 | CPU core. Confining the main thread to one process could speed up the game. |
---|
| 50 | See command line argument 'limitToCPU'. |
---|
| 51 | */ |
---|
[5855] | 52 | class _UtilExport Clock |
---|
[1674] | 53 | { |
---|
| 54 | public: |
---|
[7331] | 55 | //! Starts the time at 0 |
---|
[1674] | 56 | Clock(); |
---|
| 57 | ~Clock(); |
---|
| 58 | |
---|
[7331] | 59 | //! Internally captures the time and stays at that particular time |
---|
[2896] | 60 | void capture(); |
---|
| 61 | |
---|
[7331] | 62 | //! Returns the last captured absolute time in microseconds |
---|
| 63 | unsigned long long getMicroseconds() const |
---|
| 64 | { return tickTime_; } |
---|
| 65 | //! Returns the last captured absolute time in milliseconds |
---|
| 66 | unsigned long long getMilliseconds() const |
---|
| 67 | { return tickTime_ / 1000; } |
---|
| 68 | //! Returns the last captured absolute time in seconds |
---|
| 69 | unsigned long getSeconds() const |
---|
| 70 | { return static_cast<long> (tickTime_ / 1000000); } |
---|
| 71 | //! Returns the last captured absolute time in seconds as float |
---|
| 72 | float getSecondsPrecise() const |
---|
| 73 | { return static_cast<float>(tickTime_ / 1000000.0f); } |
---|
[1674] | 74 | |
---|
[7331] | 75 | //! Returns the timespan in seconds between the last two calls to capture() |
---|
| 76 | float getDeltaTime() const |
---|
| 77 | { return tickDtFloat_; } |
---|
| 78 | //! Returns the timespan in microseconds between the last two calls to capture() |
---|
| 79 | long getDeltaTimeMicroseconds() const |
---|
| 80 | { return tickDt_; } |
---|
[1674] | 81 | |
---|
[7331] | 82 | /** Returns the current real time in microseconds |
---|
| 83 | @note |
---|
| 84 | This is especially useful to measure execution times because of the |
---|
| 85 | high precision. |
---|
| 86 | */ |
---|
[1724] | 87 | unsigned long long getRealMicroseconds() const; |
---|
[1674] | 88 | |
---|
| 89 | private: |
---|
[7331] | 90 | //! Undefined |
---|
[1674] | 91 | Clock(const Clock& instance); |
---|
| 92 | |
---|
[7331] | 93 | Ogre::Timer* timer_; //!< Ogre timer object |
---|
| 94 | unsigned long long tickTime_; //!< Currently captured time |
---|
| 95 | long tickDt_; //!< Delta time in microseconds (cache value) |
---|
| 96 | float tickDtFloat_; //!< Delta time in seconds (cache value) |
---|
[1674] | 97 | }; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | #endif /* _Clock_H__ */ |
---|