home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Common / GameTimer.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2016-03-02  |  3.3 KB  |  128 lines

  1. //***************************************************************************************
  2. // GameTimer.cpp by Frank Luna (C) 2011 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. #include <windows.h>
  6. #include "GameTimer.h"
  7.  
  8. GameTimer::GameTimer()
  9. : mSecondsPerCount(0.0), mDeltaTime(-1.0), mBaseTime(0), 
  10.   mPausedTime(0), mPrevTime(0), mCurrTime(0), mStopped(false)
  11. {
  12.     __int64 countsPerSec;
  13.     QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);
  14.     mSecondsPerCount = 1.0 / (double)countsPerSec;
  15. }
  16.  
  17. // Returns the total time elapsed since Reset() was called, NOT counting any
  18. // time when the clock is stopped.
  19. float GameTimer::TotalTime()const
  20. {
  21.     // If we are stopped, do not count the time that has passed since we stopped.
  22.     // Moreover, if we previously already had a pause, the distance 
  23.     // mStopTime - mBaseTime includes paused time, which we do not want to count.
  24.     // To correct this, we can subtract the paused time from mStopTime:  
  25.     //
  26.     //                     |<--paused time-->|
  27.     // ----*---------------*-----------------*------------*------------*------> time
  28.     //  mBaseTime       mStopTime        startTime     mStopTime    mCurrTime
  29.  
  30.     if( mStopped )
  31.     {
  32.         return (float)(((mStopTime - mPausedTime)-mBaseTime)*mSecondsPerCount);
  33.     }
  34.  
  35.     // The distance mCurrTime - mBaseTime includes paused time,
  36.     // which we do not want to count.  To correct this, we can subtract 
  37.     // the paused time from mCurrTime:  
  38.     //
  39.     //  (mCurrTime - mPausedTime) - mBaseTime 
  40.     //
  41.     //                     |<--paused time-->|
  42.     // ----*---------------*-----------------*------------*------> time
  43.     //  mBaseTime       mStopTime        startTime     mCurrTime
  44.     
  45.     else
  46.     {
  47.         return (float)(((mCurrTime-mPausedTime)-mBaseTime)*mSecondsPerCount);
  48.     }
  49. }
  50.  
  51. float GameTimer::DeltaTime()const
  52. {
  53.     return (float)mDeltaTime;
  54. }
  55.  
  56. void GameTimer::Reset()
  57. {
  58.     __int64 currTime;
  59.     QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
  60.  
  61.     mBaseTime = currTime;
  62.     mPrevTime = currTime;
  63.     mStopTime = 0;
  64.     mStopped  = false;
  65. }
  66.  
  67. void GameTimer::Start()
  68. {
  69.     __int64 startTime;
  70.     QueryPerformanceCounter((LARGE_INTEGER*)&startTime);
  71.  
  72.  
  73.     // Accumulate the time elapsed between stop and start pairs.
  74.     //
  75.     //                     |<-------d------->|
  76.     // ----*---------------*-----------------*------------> time
  77.     //  mBaseTime       mStopTime        startTime     
  78.  
  79.     if( mStopped )
  80.     {
  81.         mPausedTime += (startTime - mStopTime);    
  82.  
  83.         mPrevTime = startTime;
  84.         mStopTime = 0;
  85.         mStopped  = false;
  86.     }
  87. }
  88.  
  89. void GameTimer::Stop()
  90. {
  91.     if( !mStopped )
  92.     {
  93.         __int64 currTime;
  94.         QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
  95.  
  96.         mStopTime = currTime;
  97.         mStopped  = true;
  98.     }
  99. }
  100.  
  101. void GameTimer::Tick()
  102. {
  103.     if( mStopped )
  104.     {
  105.         mDeltaTime = 0.0;
  106.         return;
  107.     }
  108.  
  109.     __int64 currTime;
  110.     QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
  111.     mCurrTime = currTime;
  112.  
  113.     // Time difference between this frame and the previous.
  114.     mDeltaTime = (mCurrTime - mPrevTime)*mSecondsPerCount;
  115.  
  116.     // Prepare for next frame.
  117.     mPrevTime = mCurrTime;
  118.  
  119.     // Force nonnegative.  The DXSDK's CDXUTTimer mentions that if the 
  120.     // processor goes into a power save mode or we get shuffled to another
  121.     // processor, then mDeltaTime can be negative.
  122.     if(mDeltaTime < 0.0)
  123.     {
  124.         mDeltaTime = 0.0;
  125.     }
  126. }
  127.  
  128.