CTIMER类-无论机器运行速度如何,都能保持恒定的频率

无论机器运行速度如何,都能保持恒定的频率
 CTIMER.CPP
-------------------------

#include "CTimer.h"

//---------------------- default constructor------------------------------
//
//-------------------------------------------------------------------------
CTimer::CTimer(): m_FPS(0),
             m_TimeElapsed(0.0f),
             m_FrameTime(0),
             m_LastTime(0),
             m_PerfCountFreq(0)
{
 //how many ticks per sec do we get
 QueryPerformanceFrequency( (LARGE_INTEGER*)&m_PerfCountFreq);
 
 m_TimeScale = 1.0f/m_PerfCountFreq;
}

//---------------------- constructor-------------------------------------
//
// use to specify FPS
//
//-------------------------------------------------------------------------
CTimer::CTimer(float fps): m_FPS(fps),
                    m_TimeElapsed(0.0f),
                    m_LastTime(0),
                    m_PerfCountFreq(0)
{

 //how many ticks per sec do weget
 QueryPerformanceFrequency( (LARGE_INTEGER*)&m_PerfCountFreq);

 m_TimeScale =1.0f/m_PerfCountFreq;

 //calculate ticks perframe
 m_FrameTime = (LONGLONG)(m_PerfCountFreq /m_FPS);
}


//------------------------Start()-----------------------------------------
//
// call this immediately prior to game loop.Starts the timer (obviously!)
//
//--------------------------------------------------------------------------
void CTimer::Start()
{
 //get the time
 QueryPerformanceCounter( (LARGE_INTEGER*)&m_LastTime);

 //update time to render next frame
 m_NextTime = m_LastTime + m_FrameTime;

 return;
}

//-------------------------ReadyForNextFrame()-------------------------------
//
// returns true if it is time to move on to thenext frame step. To be used if
// FPS is set.
//
//----------------------------------------------------------------------------
bool CTimer::ReadyForNextFrame()
{
 if (!m_FPS)
  {
   MessageBox(NULL, "No FPS set in timer", "Doh!", 0);

    returnfalse;
  }
 
  QueryPerformanceCounter( (LARGE_INTEGER*)&m_CurrentTime);

 if (m_CurrentTime >m_NextTime)
 {

  m_TimeElapsed =(m_CurrentTime - m_LastTime) * m_TimeScale;
  m_LastTime  =m_CurrentTime;

  //update time to render nextframe
  m_NextTime = m_CurrentTime +m_FrameTime;

  return true;
 }

 return false;
}

//--------------------------- TimeElapsed--------------------------------
//
// returns time elapsed since last call to thisfunction. Use in main
// when calculations are to be based on dt.
//
//-------------------------------------------------------------------------
double CTimer::TimeElapsed()
{
 QueryPerformanceCounter( (LARGE_INTEGER*)&m_CurrentTime);
 
 m_TimeElapsed = (m_CurrentTime -m_LastTime) * m_TimeScale;
 
 m_LastTime  =m_CurrentTime;

 return m_TimeElapsed;
  
}

 

CTIMER.H

----------------

#ifndef CTIMER_H
#define CTIMER_H
//-----------------------------------------------------------------------
//
//  Name: CTimer.h
//
//  Author: Mat Buckland 2002
//
// Desc: Windows timer class for the book GameAI
//  Programming with Neural Nets and GeneticAlgorithms.
//
//-----------------------------------------------------------------------

#include<windows.h>


class CTimer
{

private:

 LONGLONG m_CurrentTime,
          m_LastTime,
       m_NextTime,
       m_FrameTime,
       m_PerfCountFreq;

 double  m_TimeElapsed,
       m_TimeScale;

 float   m_FPS;


public:

  //ctors
 CTimer();
 CTimer(float fps);


 //whatdayaknow, this starts the timer
 void  Start();

 //determines if enough time has passed to moveonto next frame
 bool  ReadyForNextFrame();

 //only use this after a call to theabove.
 double GetTimeElapsed(){returnm_TimeElapsed;}

 double TimeElapsed();

};

 

#endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值