1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "OgreWin32Timer.h"
|
---|
26 |
|
---|
27 | namespace Ogre {
|
---|
28 | //-------------------------------------------------------------------------
|
---|
29 | void Win32Timer::reset()
|
---|
30 | {
|
---|
31 | Timer::reset();
|
---|
32 | QueryPerformanceFrequency(&mFrequency);
|
---|
33 | QueryPerformanceCounter(&mStartTime);
|
---|
34 | mStartTick = GetTickCount();
|
---|
35 | mLastTime = 0;
|
---|
36 | }
|
---|
37 | //-------------------------------------------------------------------------
|
---|
38 | unsigned long Win32Timer::getMilliseconds()
|
---|
39 | {
|
---|
40 | LARGE_INTEGER curTime;
|
---|
41 | QueryPerformanceCounter(&curTime);
|
---|
42 | LONGLONG newTime = curTime.QuadPart - mStartTime.QuadPart;
|
---|
43 |
|
---|
44 | // scale by 1000 for milliseconds
|
---|
45 | unsigned long newTicks = (unsigned long) (1000 * newTime / mFrequency.QuadPart);
|
---|
46 |
|
---|
47 | // detect and compensate for performance counter leaps
|
---|
48 | // (surprisingly common, see Microsoft KB: Q274323)
|
---|
49 | unsigned long check = GetTickCount() - mStartTick;
|
---|
50 | signed long msecOff = (signed long)(newTicks - check);
|
---|
51 | if (msecOff < -100 || msecOff > 100)
|
---|
52 | {
|
---|
53 | // We must keep the timer running forward :)
|
---|
54 | LONGLONG adjust = (std::min)(msecOff * mFrequency.QuadPart / 1000, newTime - mLastTime);
|
---|
55 | mStartTime.QuadPart += adjust;
|
---|
56 | newTime -= adjust;
|
---|
57 |
|
---|
58 | // Re-calculate milliseconds
|
---|
59 | newTicks = (unsigned long) (1000 * newTime / mFrequency.QuadPart);
|
---|
60 | }
|
---|
61 |
|
---|
62 | // Record last time for adjust
|
---|
63 | mLastTime = newTime;
|
---|
64 |
|
---|
65 | return newTicks;
|
---|
66 | }
|
---|
67 | //-------------------------------------------------------------------------
|
---|
68 | unsigned long Win32Timer::getMicroseconds()
|
---|
69 | {
|
---|
70 | LARGE_INTEGER curTime;
|
---|
71 | QueryPerformanceCounter(&curTime);
|
---|
72 | LONGLONG newTime = curTime.QuadPart - mStartTime.QuadPart;
|
---|
73 |
|
---|
74 | // get milliseconds to check against GetTickCount
|
---|
75 | unsigned long newTicks = (unsigned long) (1000 * newTime / mFrequency.QuadPart);
|
---|
76 |
|
---|
77 | // detect and compensate for performance counter leaps
|
---|
78 | // (surprisingly common, see Microsoft KB: Q274323)
|
---|
79 | unsigned long check = GetTickCount() - mStartTick;
|
---|
80 | signed long msecOff = (signed long)(newTicks - check);
|
---|
81 | if (msecOff < -100 || msecOff > 100)
|
---|
82 | {
|
---|
83 | // We must keep the timer running forward :)
|
---|
84 | LONGLONG adjust = (std::min)(msecOff * mFrequency.QuadPart / 1000, newTime - mLastTime);
|
---|
85 | mStartTime.QuadPart += adjust;
|
---|
86 | newTime -= adjust;
|
---|
87 | }
|
---|
88 |
|
---|
89 | // Record last time for adjust
|
---|
90 | mLastTime = newTime;
|
---|
91 |
|
---|
92 | // scale by 1000000 for microseconds
|
---|
93 | unsigned long newMicro = (unsigned long) (1000000 * newTime / mFrequency.QuadPart);
|
---|
94 |
|
---|
95 | return newMicro;
|
---|
96 | }
|
---|
97 |
|
---|
98 | }
|
---|