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 | mQueryCount = 0;
|
---|
37 |
|
---|
38 | // Save the current process
|
---|
39 | HANDLE mProc = GetCurrentProcess();
|
---|
40 |
|
---|
41 | // Get the current Affinity
|
---|
42 | GetProcessAffinityMask(mProc, &mProcMask, &mSysMask);
|
---|
43 |
|
---|
44 | mThread = GetCurrentThread();
|
---|
45 | }
|
---|
46 | //-------------------------------------------------------------------------
|
---|
47 | unsigned long Win32Timer::getMilliseconds()
|
---|
48 | {
|
---|
49 | LARGE_INTEGER curTime;
|
---|
50 |
|
---|
51 | // Set affinity to the first core
|
---|
52 | SetThreadAffinityMask(mThread, 1);
|
---|
53 |
|
---|
54 | // Query the timer
|
---|
55 | QueryPerformanceCounter(&curTime);
|
---|
56 |
|
---|
57 | // Reset affinity
|
---|
58 | SetThreadAffinityMask(mThread, mProcMask);
|
---|
59 |
|
---|
60 | // Resample the frequency
|
---|
61 | mQueryCount++;
|
---|
62 | if(mQueryCount == FREQUENCY_RESAMPLE_RATE)
|
---|
63 | {
|
---|
64 | mQueryCount = 0;
|
---|
65 | QueryPerformanceFrequency(&mFrequency);
|
---|
66 | }
|
---|
67 |
|
---|
68 | LONGLONG newTime = curTime.QuadPart - mStartTime.QuadPart;
|
---|
69 |
|
---|
70 | // scale by 1000 for milliseconds
|
---|
71 | unsigned long newTicks = (unsigned long) (1000 * newTime / mFrequency.QuadPart);
|
---|
72 |
|
---|
73 | // detect and compensate for performance counter leaps
|
---|
74 | // (surprisingly common, see Microsoft KB: Q274323)
|
---|
75 | unsigned long check = GetTickCount() - mStartTick;
|
---|
76 | signed long msecOff = (signed long)(newTicks - check);
|
---|
77 | if (msecOff < -100 || msecOff > 100)
|
---|
78 | {
|
---|
79 | // We must keep the timer running forward :)
|
---|
80 | LONGLONG adjust = (std::min)(msecOff * mFrequency.QuadPart / 1000, newTime - mLastTime);
|
---|
81 | mStartTime.QuadPart += adjust;
|
---|
82 | newTime -= adjust;
|
---|
83 |
|
---|
84 | // Re-calculate milliseconds
|
---|
85 | newTicks = (unsigned long) (1000 * newTime / mFrequency.QuadPart);
|
---|
86 | }
|
---|
87 |
|
---|
88 | // Record last time for adjust
|
---|
89 | mLastTime = newTime;
|
---|
90 |
|
---|
91 | return newTicks;
|
---|
92 | }
|
---|
93 | //-------------------------------------------------------------------------
|
---|
94 | unsigned long Win32Timer::getMicroseconds()
|
---|
95 | {
|
---|
96 | LARGE_INTEGER curTime;
|
---|
97 | QueryPerformanceCounter(&curTime);
|
---|
98 | LONGLONG newTime = curTime.QuadPart - mStartTime.QuadPart;
|
---|
99 |
|
---|
100 | // get milliseconds to check against GetTickCount
|
---|
101 | unsigned long newTicks = (unsigned long) (1000 * newTime / mFrequency.QuadPart);
|
---|
102 |
|
---|
103 | // detect and compensate for performance counter leaps
|
---|
104 | // (surprisingly common, see Microsoft KB: Q274323)
|
---|
105 | unsigned long check = GetTickCount() - mStartTick;
|
---|
106 | signed long msecOff = (signed long)(newTicks - check);
|
---|
107 | if (msecOff < -100 || msecOff > 100)
|
---|
108 | {
|
---|
109 | // We must keep the timer running forward :)
|
---|
110 | LONGLONG adjust = (std::min)(msecOff * mFrequency.QuadPart / 1000, newTime - mLastTime);
|
---|
111 | mStartTime.QuadPart += adjust;
|
---|
112 | newTime -= adjust;
|
---|
113 | }
|
---|
114 |
|
---|
115 | // Record last time for adjust
|
---|
116 | mLastTime = newTime;
|
---|
117 |
|
---|
118 | // scale by 1000000 for microseconds
|
---|
119 | unsigned long newMicro = (unsigned long) (1000000 * newTime / mFrequency.QuadPart);
|
---|
120 |
|
---|
121 | return newMicro;
|
---|
122 | }
|
---|
123 |
|
---|
124 | }
|
---|