青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天行健 君子當(dāng)自強而不息

游戲中時間的封裝

時鐘類GE_TIMER可用來取得游戲已進(jìn)行的時間,計算出兩個時間點之間的時間片大小,從而可在某一時間點處,自動更新某些游戲狀態(tài)。此外,還可用來獲取程序的幀頻FSP(Frame Per Second)大小,檢驗3D渲染的速度,即游戲速度。

Windows API函數(shù)timeGetTime用來取得游戲開始后的時間,返回的時間值單位為ms(毫秒)。

The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since Windows was started.

DWORD timeGetTime(VOID);

Parameters

This function does not take parameters.

Return Values

Returns the system time, in milliseconds.

但是這個函數(shù)的精度只有10ms左右,如果需要采用更為精確的時間,可使用小于1ms時間精度的Windows API函數(shù)QueryPerformanceCounter和QueryPerformanceFrequency,這兩個函數(shù)直接使用了Windows 內(nèi)核的精度非常高的定時器。不同的硬件和操作系統(tǒng),定時器的頻率稍有不同。

The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter,
if one exists. The frequency cannot change while the system is running.

Syntax

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

Parameters

lpFrequency
[out] Pointer to a variable that receives the current performance-counter frequency, in counts per second.
If the installed hardware does not support a high-resolution performance counter, this parameter can be zero.

Return Value

If the installed hardware supports a high-resolution performance counter, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError. For example,
if the installed hardware does not support a high-resolution performance counter, the function fails. 

The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter. 

Syntax

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);

Parameters

lpPerformanceCount
[out] Pointer to a variable that receives the current performance-counter value, in counts.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError. 

Remarks

On a multiprocessor computer, it should not matter which processor is called. However, you can get different results on
different processors due to bugs in the basic input/output system (BIOS) or the hardware abstraction layer (HAL).
To specify processor affinity for a thread, use the SetThreadAffinityMask function.

這兩個函數(shù)都使用了結(jié)構(gòu)體LARGE_INTEGER,我們來看看它的結(jié)構(gòu):

The LARGE_INTEGER structure is used to represent a 64-bit signed integer value.

Note  Your C compiler may support 64-bit integers natively. For example, Microsoft® Visual C++® supports the __int64 sized integer type.
For more information, see the documentation included with your C compiler.

typedef union _LARGE_INTEGER
{
     struct {    DWORD LowPart;    LONG HighPart;  }; 
     struct {    DWORD LowPart;    LONG HighPart;  } u;
     LONGLONG QuadPart;
} LARGE_INTEGER, *PLARGE_INTEGER;

Members

LowPart 
Low-order 32 bits.

HighPart 
High-order 32 bits.


LowPart 
Low-order 32 bits. 
HighPart 
High-order 32 bits.

QuadPart 
Signed 64-bit integer.

Remarks

The LARGE_INTEGER structure is actually a union. If your compiler has built-in support for 64-bit integers,
use the QuadPart member to store the 64-bit integer. Otherwise, use the LowPart and HighPart members to store the 64-bit integer.

看的出來,它實際上是1個聯(lián)合體。

提示:要正確編譯運行,需要鏈接winmm.lib。
由于本人水平有限,可能存在錯誤,敬請指出。

源碼下載

好了,現(xiàn)在看看GE_COMMON.h的定義,主要用來包含公用的頭文件和宏定義:

/*************************************************************************************
 [Include File]

 PURPOSE: 
    Include common header files and common macro.
************************************************************************************
*/

#ifndef GAME_ENGINE_COMMON_H
#define GAME_ENGINE_COMMON_H

#define DIRECTINPUT_VERSION 0x0800  // let compile shut up

#include 
<windows.h>
#include 
<tchar.h>
#include 
<string.h>
#include 
<stdio.h>

#include 
<d3d9.h>
#include 
<d3dx9.h>
#include 
<dinput.h>
#include 
<dsound.h>

// defines for small numbers
#define EPSILON_E3  (float)(1E-3)
#define EPSILON_E4  (float)(1E-4)
#define EPSILON_E5  (float)(1E-5)
#define EPSILON_E6  (float)(1E-6)

#define Safe_Release(object) if((object) != NULL) { (object)->Release(); (object)=NULL; }

#define FCMP(a, b) (fabs((a) - (b)) < EPSILON_E3 ? 1 : 0)

#endif

由于浮點數(shù)不能直接比較大小,所以定義了1個宏來比較浮點數(shù)的大小。

#define FCMP(a, b) (fabs((a) -& nbsp;(b)) < EPSILON_E3 ? 1& nbsp;: 0)

再來看看GE_TIMER.h的定義:

/*************************************************************************************
 [Include File]

 PURPOSE: 
    Encapsulate system time for game.
************************************************************************************
*/

#ifndef GAME_ENGINE_TIMER_H
#define GAME_ENGINE_TIMER_H

class GE_TIMER
{
private:
    
bool _use_large_time;               // flag that indicate whether use large time

    __int64 _one_second_ticks;          
// ticks count in one second
    __int64 _tick_counts_start;         // tick counts at start count time

    unsigned 
long _time_start;          // start time for timeGetTime()

    
int _frame_count;                   // frame count number
    float _fps;                         // frame per second
    float _time1, _time2, _time_slice;  // time flag and time slice

public:
    GE_TIMER();
    
~GE_TIMER();
    
void Init_Game_Time();
    
float Get_Game_Play_Time();
    
void Update_FPS();

    
float Get_FPS() { return _fps; }
};

#endif

并非所有系統(tǒng)都支持內(nèi)核的定時器讀取,因此要定義一個
_use_large_time來標(biāo)志是否使用這個高精度的定時器,否則將使用timeGetTime函數(shù)進(jìn)行時間計算。

我們來看看構(gòu)造函數(shù)和析構(gòu)函數(shù)的定義:

//------------------------------------------------------------------------------------
// Constructor, initialize game time.
//------------------------------------------------------------------------------------
GE_TIMER::GE_TIMER()
{
    Init_Game_Time();
}

//------------------------------------------------------------------------------------
// Destructor, do nothing.
//------------------------------------------------------------------------------------
GE_TIMER::~GE_TIMER()

}

看的出來,構(gòu)造函數(shù)只是調(diào)用了Init_Game_Time來初始化游戲時間,而析構(gòu)函數(shù)什么都不做。

再來看看
Init_Game_Time的定義:

//------------------------------------------------------------------------------------
// Initialize game time.
//------------------------------------------------------------------------------------
void GE_TIMER::Init_Game_Time()
{
    _frame_count 
= 0;
    _fps 
= 0;
    _time1 
= _time2 = _time_slice = 0;

    
if(QueryPerformanceFrequency((LARGE_INTEGER*&_one_second_ticks))
    {
        _use_large_time 
= true;
        QueryPerformanceCounter((LARGE_INTEGER
*&_tick_counts_start);
    }
    
else
    {
        _use_large_time 
= false;
        _time_start 
= timeGetTime();
    }
}

我們使用Get_Game_Play_Time來取得當(dāng)前的游戲時間,來看看它的定義:

//------------------------------------------------------------------------------------
// Get time has escaped since game start.
//------------------------------------------------------------------------------------
float GE_TIMER::Get_Game_Play_Time()
{
    __int64 current_tick_counts;

    
if(_use_large_time)
    {
        QueryPerformanceCounter((LARGE_INTEGER
*&current_tick_counts);
        
return ((float) (current_tick_counts - _tick_counts_start) / _one_second_ticks) * 1000
    }

    
return (float)(timeGetTime() - _time_start);
}

分兩種情況進(jìn)行處理,如果使用高精度時鐘,將計算開始和結(jié)束時鐘計數(shù)之差,除以時鐘頻率,再乘以1000,即獲得時間片大小,單位為 ms。
否則直接利用timeGetTime函數(shù)計算時間片大小。

更新幀頻通過Update_FPS函數(shù)來進(jìn)行,每5幀更新一次。

//------------------------------------------------------------------------------------
// Update FPS.
//------------------------------------------------------------------------------------
void GE_TIMER::Update_FPS()
{
    
// increment frame count by one
    _frame_count++;

    
if(_frame_count % 5 == 1)
        _time1 
= Get_Game_Play_Time() / 1000;
    
else if(_frame_count % 5 == 0)
    {
        _time2 
= Get_Game_Play_Time() / 1000;
        _time_slice 
= (float) fabs(_time1 - _time2);    // calculate time escaped
    }

    
// update fps
    if(! FCMP(_time_slice, 0.0))
        _fps 
= 5 / _time_slice;
}

完整的GE_TIMER.cpp實現(xiàn)如下所示:

/*************************************************************************************
 [Implement File]

 PURPOSE: 
    Encapsulate system time for game.
************************************************************************************
*/

#include 
"GE_COMMON.h"
#include 
"GE_TIMER.h"

//------------------------------------------------------------------------------------
// Constructor, initialize game time.
//------------------------------------------------------------------------------------
GE_TIMER::GE_TIMER()
{
    Init_Game_Time();
}

//------------------------------------------------------------------------------------
// Destructor, do nothing.
//------------------------------------------------------------------------------------
GE_TIMER::~GE_TIMER()

}

//------------------------------------------------------------------------------------
// Initialize game time.
//------------------------------------------------------------------------------------
void GE_TIMER::Init_Game_Time()
{
    _frame_count 
= 0;
    _fps 
= 0;
    _time1 
= _time2 = _time_slice = 0;

    
if(QueryPerformanceFrequency((LARGE_INTEGER*&_one_second_ticks))
    {
        _use_large_time 
= true;
        QueryPerformanceCounter((LARGE_INTEGER
*&_tick_counts_start);
    }
    
else
    {
        _use_large_time 
= false;
        _time_start 
= timeGetTime();
    }
}

//------------------------------------------------------------------------------------
// Get time has escaped since game start.
//------------------------------------------------------------------------------------
float GE_TIMER::Get_Game_Play_Time()
{
    __int64 current_tick_counts;

    
if(_use_large_time)
    {
        QueryPerformanceCounter((LARGE_INTEGER
*&current_tick_counts);
        
return ((float) (current_tick_counts - _tick_counts_start) / _one_second_ticks) * 1000
    }

    
return (float)(timeGetTime() - _time_start);
}

//------------------------------------------------------------------------------------
// Update FPS.
//------------------------------------------------------------------------------------
void GE_TIMER::Update_FPS()
{
    
// increment frame count by one
    _frame_count++;

    
if(_frame_count % 5 == 1)
        _time1 
= Get_Game_Play_Time() / 1000;
    
else if(_frame_count % 5 == 0)
    {
        _time2 
= Get_Game_Play_Time() / 1000;
        _time_slice 
= (float) fabs(_time1 - _time2);    // calculate time escaped
    }

    
// update fps
    if(! FCMP(_time_slice, 0.0))
        _fps 
= 5 / _time_slice;
}

posted on 2007-05-07 21:33 lovedday 閱讀(930) 評論(0)  編輯 收藏 引用 所屬分類: ■ DirectX 9 Program

公告

導(dǎo)航

統(tǒng)計

常用鏈接

隨筆分類(178)

3D游戲編程相關(guān)鏈接

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            美女精品自拍一二三四| 久久另类ts人妖一区二区| 欧美黑人多人双交| 亚洲伦理精品| 亚洲最快最全在线视频| 国产精品啊啊啊| 久久国产精品免费一区| 久久久久综合一区二区三区| 性久久久久久久久久久久| 欧美一区永久视频免费观看| 亚洲国产中文字幕在线观看| 99ri日韩精品视频| 亚洲六月丁香色婷婷综合久久| 久久精品99| 亚洲天堂免费观看| 久久精品综合网| 在线视频精品一区| 久久精品国产91精品亚洲| 午夜精品久久久久久久99樱桃| 久久久人成影片一区二区三区观看| 欧美一区1区三区3区公司| 猫咪成人在线观看| 午夜欧美精品| 国产精品三上| 亚洲精品1234| 久久免费精品视频| 亚洲在线播放| 欧美大尺度在线观看| 欧美一二三区在线观看| 国产精品日韩在线一区| 亚洲综合日韩| 亚洲视频日本| 国产精品福利在线| 亚洲国产精品成人一区二区| 国产老女人精品毛片久久| 亚洲国内精品在线| 99这里只有精品| 久热精品视频在线观看| 欧美在线免费观看| 欧美视频在线观看| 亚洲国产日韩欧美在线99| 亚洲精品在线二区| 欧美视频精品一区| 午夜精品久久久久久久白皮肤| 中国日韩欧美久久久久久久久| 欧美特黄一区| 欧美一级午夜免费电影| 亚洲欧美另类久久久精品2019| 欧美精品久久久久久久| 欧美激情视频给我| 亚洲电影毛片| 久久天天躁夜夜躁狠狠躁2022| 欧美顶级大胆免费视频| 在线观看视频日韩| 久久亚洲图片| 艳女tv在线观看国产一区| 久久精品成人| 亚洲精品视频啊美女在线直播| 蜜臀a∨国产成人精品| 一区二区三区.www| 亚洲综合日韩在线| 一区二区亚洲精品国产| 久久只有精品| 亚洲视频在线观看三级| 欧美亚洲日本一区| 亚洲人成精品久久久久| 欧美激情一二区| 午夜精品免费视频| 亚洲欧美网站| 亚洲国产欧美一区| 国产精品一区二区黑丝| 男同欧美伦乱| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 久久久999| 黄色亚洲免费| 久久亚洲一区二区三区四区| 一本久久综合亚洲鲁鲁| 欧美福利专区| 久久aⅴ国产欧美74aaa| 一区二区三区精品| 亚洲国产精品福利| 欧美精品1区| 久久久久久9| 亚洲欧美日韩国产精品| 亚洲九九精品| 久久riav二区三区| 亚洲影视在线播放| 999亚洲国产精| 国产精品青草久久久久福利99| 免费一级欧美片在线观看| 欧美在线精品免播放器视频| 亚洲一区亚洲二区| 欧美.www| 亚洲欧美日本国产专区一区| 99国产精品久久久| 亚洲精品黄色| 91久久夜色精品国产网站| 尤妮丝一区二区裸体视频| 国产亚洲一区二区三区在线观看| 你懂的视频欧美| 久久亚洲综合色一区二区三区| 欧美一站二站| 欧美有码在线视频| 欧美亚洲综合久久| 欧美在线观看天堂一区二区三区| 西西人体一区二区| 亚洲欧美伊人| 欧美在线啊v一区| 欧美在线网站| 久久久国产精彩视频美女艺术照福利| 欧美在线免费| 久久精品一区蜜桃臀影院| 久久成人免费| 久久视频一区二区| 免费在线一区二区| 欧美经典一区二区三区| 欧美日韩日本视频| 久久综合中文色婷婷| 快she精品国产999| 欧美韩日视频| 国产精品99一区| 国产老女人精品毛片久久| 国产揄拍国内精品对白| 国产精品国产三级国产专播精品人| 欧美视频第二页| 国产深夜精品福利| 国产精品网站在线播放| 国内久久婷婷综合| 亚洲肉体裸体xxxx137| 亚洲天堂av综合网| 久久不见久久见免费视频1| 蜜乳av另类精品一区二区| 亚洲国产精品va在线观看黑人| 亚洲美女少妇无套啪啪呻吟| 亚洲国产综合在线| 一区二区三区视频在线播放| 欧美一区二区视频在线观看| 久久综合伊人77777| 欧美色区777第一页| 国产综合视频| 夜久久久久久| 久久久99免费视频| 亚洲激情在线观看| 香蕉精品999视频一区二区 | 欧美sm极限捆绑bd| 欧美系列电影免费观看| 极品少妇一区二区三区| 中文亚洲欧美| 老司机精品视频一区二区三区| 久久久久久久久蜜桃| 最新热久久免费视频| 欧美一级在线亚洲天堂| 欧美欧美天天天天操| 欧美日产一区二区三区在线观看| 欧美另类综合| 韩国一区电影| 亚洲尤物在线视频观看| 欧美国产视频日韩| 午夜天堂精品久久久久| 欧美日韩国产成人精品| 黄色av成人| 欧美一级免费视频| 亚洲日韩第九十九页| 在线一区免费观看| 欧美一区二区三区免费观看视频 | 葵司免费一区二区三区四区五区| 欧美午夜一区| 亚洲九九九在线观看| 久久色在线播放| 亚洲电影下载| 国产精品99久久99久久久二8| 美女国产精品| 韩国av一区二区三区四区| 欧美一区成人| 99热精品在线观看| 欧美激情精品久久久久| 影音先锋久久久| 久久久之久亚州精品露出| 亚洲欧美日韩国产综合| 国产精品久久久一区麻豆最新章节 | 日韩视频永久免费观看| 久久综合伊人| 欧美在线视频免费| 国产一本一道久久香蕉| 欧美在线视频二区| 亚洲欧美经典视频| 国产精品永久免费在线| 亚洲一区二区三区精品在线| 久久久久久91香蕉国产| 亚洲女性裸体视频| 欧美午夜女人视频在线| 亚洲视频一区在线| 一区二区av在线| 国产精品黄色| 欧美在线啊v| 欧美专区18| 亚洲国产合集| 91久久综合| 欧美三级小说| 性8sex亚洲区入口|