winTimer.cpp

Engine/source/platformWin32/winTimer.cpp

More...

Classes:

Public Defines

Detailed Description

Public Defines

WIN32_LEAN_AND_MEAN() 
 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2012 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24// Grab the win32 headers so we can access QPC
25#define WIN32_LEAN_AND_MEAN
26#include <Windows.h>
27
28#include "platform/platformTimer.h"
29#include "math/mMath.h"
30
31class Win32Timer : public PlatformTimer
32{
33private:
34   U32 mTickCountCurrent;
35   U32 mTickCountNext;
36   S64 mPerfCountCurrent;
37   S64 mPerfCountNext;
38   S64 mFrequency;
39   F64 mPerfCountRemainderCurrent;
40   F64 mPerfCountRemainderNext;
41   bool mUsingPerfCounter;
42public:
43
44   Win32Timer()
45   {
46      mPerfCountRemainderCurrent = 0.0f;
47      mPerfCountRemainderNext = 0.0f;
48
49      // Attempt to use QPC for high res timing, otherwise fallback to GTC.
50      mUsingPerfCounter = QueryPerformanceFrequency((LARGE_INTEGER *) &mFrequency);
51      if(mUsingPerfCounter)
52         mUsingPerfCounter = QueryPerformanceCounter((LARGE_INTEGER *) &mPerfCountCurrent);
53      if(!mUsingPerfCounter)
54      {
55         mTickCountCurrent = GetTickCount();
56         mTickCountNext = 0;
57      }
58   }
59
60   const S32 getElapsedMs()
61   {
62      if(mUsingPerfCounter)
63      {
64         // Use QPC, update remainders so we don't leak time, and return the elapsed time.
65         QueryPerformanceCounter( (LARGE_INTEGER *) &mPerfCountNext);
66         F64 elapsedF64 = (1000.0 * F64(mPerfCountNext - mPerfCountCurrent) / F64(mFrequency));
67         elapsedF64 += mPerfCountRemainderCurrent;
68         U32 elapsed = (U32)mFloor(elapsedF64);
69         mPerfCountRemainderNext = elapsedF64 - F64(elapsed);
70
71         return elapsed;
72      }
73      else
74      {
75         // Do something naive with GTC.
76         mTickCountNext = GetTickCount();
77         return mTickCountNext - mTickCountCurrent;
78      }
79   }
80
81   void reset()
82   {
83      // Do some simple copying to reset the timer to 0.
84      mTickCountCurrent = mTickCountNext;
85      mPerfCountCurrent = mPerfCountNext;
86      mPerfCountRemainderCurrent = mPerfCountRemainderNext;
87   }
88};
89
90PlatformTimer *PlatformTimer::create()
91{
92   return new Win32Timer();
93}
94