音视频代码模块
+ -

DX计数时源代码-可用于音视频渲染时间驱动

2024-06-06 19 0

Games.h

#ifndef GAMETIMER_H
#define GAMETIMER_H

class GameTimer
{
public:
    GameTimer();

    float TotalRunTime() const;
    float DeltaTime() const;


    void Reset();
    void Start();
    void Stop();
    void Tick();

private:
    double mSecondsPerCount;//1秒多少计数
    double mDeltaTime;    // 当前帧的间隔时间,如果暂停则为0

    __int64 mBaseTime; //开始的基准绝对时间点
    __int64 mStopTime; //记录停止的时间点

    __int64 mPausedTime; //停止了多长时间

    __int64 mPrevTime;
    __int64 mCurrTime;

    bool mStopped;

};

#endif

Games.cpp

#include "pch.h"
#include <windows.h>
#include "GameTimer.h"

GameTimer::GameTimer(): 
    mSecondsPerCount(0.0), 
    mDeltaTime(-1.0), 
    mBaseTime(0), 
    mPausedTime(0), 
    mPrevTime(0), 
    mCurrTime(0), 
    mStopped(false)
{
    __int64 countsPreSec;
    QueryPerformanceFrequency((LARGE_INTEGER*)&countsPreSec);
    mSecondsPerCount = 1.0 / (double)countsPreSec;
}

float GameTimer::TotalRunTime() const
{
    if (mStopped)
    {
        return (float)(((mStopTime - mPausedTime) - mBaseTime)*mSecondsPerCount);
    }
    else
    {
        return (float)(((mCurrTime - mPausedTime) - mBaseTime) * mSecondsPerCount);
    }
}

float GameTimer::DeltaTime() const
{
    return (float)mDeltaTime;
}


//复位即自动Start
void GameTimer::Reset()
{
    __int64 currTime;
    QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
    mBaseTime = currTime;
    mPrevTime = currTime;
    mStopTime = 0;
    mStopped = false;
}

void GameTimer::Start()
{
    __int64 startTime;
    QueryPerformanceCounter((LARGE_INTEGER*)&startTime);

    if (mStopped)
    {
        // 增加暂停时间
        mPausedTime += (startTime - mStopTime);

        mPrevTime = startTime;
        mStopTime = 0;
        mStopped = false;
    }

}

//停止
void GameTimer::Stop()
{
    //如果没有停止
    if (!mStopped)
    {
        __int64 currTime;
        QueryPerformanceCounter((LARGE_INTEGER*)&currTime);

        // 记录暂停的时间点,并更新标识位
        mStopTime = currTime;
        mStopped = true;

    }
    //else 已经停止,则不再动作
}

// 每帧调用
void GameTimer::Tick()
{
    if (mStopped)
    {
        //如果停止,时间间隔为0
        mDeltaTime = 0.0f;
        return;
    }

    //如果没有停止,则计算两次调用的时间间隔mDeltaTime,并更新时间点的标识

    // 查询当前是第几帧
    __int64 currTime;
    QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
    mCurrTime = currTime;

    // 根据经过的帧数 和 每帧的时间 算出时间间隔
    mDeltaTime = (mCurrTime - mPrevTime) * mSecondsPerCount;
    mPrevTime = mCurrTime;

    // 在处理器在进入省电模式、或切换处理器的时候,mDeltaTime可能为负值(DXSDK)
    if (mDeltaTime < 0.0)
    {
        mDeltaTime = 0.0;
    }
}

0 篇笔记 写笔记

作者信息
站长漫谈
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!