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   S64 mPerfCountCurrent;
35   S64 mPerfCountNext;
36   S64 mFrequency;
37   F64 mPerfCountRemainderCurrent;
38   F64 mPerfCountRemainderNext;
39public:
40
41   Win32Timer()
42   {
43      mPerfCountRemainderCurrent = 0.0f;
44      mPerfCountRemainderNext = 0.0f;
45
46      QueryPerformanceFrequency((LARGE_INTEGER *) &mFrequency);
47      QueryPerformanceCounter((LARGE_INTEGER *) &mPerfCountCurrent);
48      mPerfCountNext = 0.0;
49   }
50
51   const S32 getElapsedMs()
52   {
53      // Use QPC, update remainders so we don't leak time, and return the elapsed time.
54      QueryPerformanceCounter( (LARGE_INTEGER *) &mPerfCountNext);
55      F64 elapsedF64 = (1000.0 * F64(mPerfCountNext - mPerfCountCurrent) / F64(mFrequency));
56      elapsedF64 += mPerfCountRemainderCurrent;
57      U32 elapsed = (U32)mFloor(elapsedF64);
58      mPerfCountRemainderNext = elapsedF64 - F64(elapsed);
59
60      return elapsed;
61   }
62
63   void reset()
64   {
65      // Do some simple copying to reset the timer to 0.
66      mPerfCountCurrent = mPerfCountNext;
67      mPerfCountRemainderCurrent = mPerfCountRemainderNext;
68   }
69};
70
71PlatformTimer *PlatformTimer::create()
72{
73   return new Win32Timer();
74}
75