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

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

游戲中時(shí)間的封裝

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

Windows API函數(shù)timeGetTime用來(lái)取得游戲開始后的時(shí)間,返回的時(shí)間值單位為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.

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

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.

這兩個(gè)函數(shù)都使用了結(jié)構(gòu)體LARGE_INTEGER,我們來(lái)看看它的結(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.

看的出來(lái),它實(shí)際上是1個(gè)聯(lián)合體。

提示:要正確編譯運(yùn)行,需要鏈接winmm.lib。
由于本人水平有限,可能存在錯(cuò)誤,敬請(qǐng)指出。

源碼下載

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

/*************************************************************************************
 [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

由于浮點(diǎn)數(shù)不能直接比較大小,所以定義了1個(gè)宏來(lái)比較浮點(diǎn)數(shù)的大小。

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

再來(lái)看看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)核的定時(shí)器讀取,因此要定義一個(gè)
_use_large_time來(lái)標(biāo)志是否使用這個(gè)高精度的定時(shí)器,否則將使用timeGetTime函數(shù)進(jìn)行時(shí)間計(jì)算。

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

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

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

}

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

再來(lái)看看
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來(lái)取得當(dāng)前的游戲時(shí)間,來(lái)看看它的定義:

//------------------------------------------------------------------------------------
// 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)行處理,如果使用高精度時(shí)鐘,將計(jì)算開始和結(jié)束時(shí)鐘計(jì)數(shù)之差,除以時(shí)鐘頻率,再乘以1000,即獲得時(shí)間片大小,單位為 ms。
否則直接利用timeGetTime函數(shù)計(jì)算時(shí)間片大小。

更新幀頻通過(guò)Update_FPS函數(shù)來(lái)進(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實(shí)現(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 閱讀(923) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ■ DirectX 9 Program

公告

導(dǎo)航

統(tǒng)計(jì)

常用鏈接

隨筆分類(178)

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

搜索

最新評(píng)論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            久久亚洲捆绑美女| 久久精品在线观看| 99视频有精品| 国产精品jizz在线观看美国| 夜夜嗨av一区二区三区网页| 亚洲日韩中文字幕在线播放| 欧美日韩成人在线| 性xx色xx综合久久久xx| 欧美影院久久久| 亚洲精品久久久久久下一站| 日韩亚洲欧美在线观看| 国产欧美激情| 亚洲国产日本| 国产性猛交xxxx免费看久久| 欧美顶级少妇做爰| 国产精品高清在线| 老鸭窝毛片一区二区三区 | 欧美在线啊v| 浪潮色综合久久天堂| 亚洲视频免费看| 久久精品123| 亚洲一区二区在线免费观看| 性欧美办公室18xxxxhd| 亚洲激情成人网| 亚洲免费在线看| 日韩一级大片在线| 欧美一区二区三区在线视频| 亚洲肉体裸体xxxx137| 欧美亚洲免费高清在线观看| 99国产精品99久久久久久| 午夜精品福利一区二区三区av | 欧美日韩一区三区| 美腿丝袜亚洲色图| 国产精品亚洲综合一区在线观看| 欧美大片免费久久精品三p | 蜜臀99久久精品久久久久久软件| 亚洲一区二区三区中文字幕| 久久五月天婷婷| 欧美在线一二三四区| 欧美网站在线| 亚洲黄色一区二区三区| 在线观看欧美激情| 欧美中文字幕久久| 午夜在线一区二区| 欧美体内she精视频| 91久久精品视频| 在线观看欧美黄色| 久久精品国产99精品国产亚洲性色| 中文在线一区| 欧美日韩精品综合| 亚洲高清一二三区| 在线看片欧美| 久久久久久综合| 麻豆精品国产91久久久久久| 国产一区白浆| 久久se精品一区精品二区| 校园春色国产精品| 国产欧美日韩激情| 亚洲视频碰碰| 午夜国产精品影院在线观看| 国产精品国产三级国产aⅴ入口| 99re这里只有精品6| 99视频精品免费观看| 欧美xxxx在线观看| 91久久夜色精品国产九色| 亚洲区一区二| 欧美片第1页综合| 亚洲最新中文字幕| 亚洲欧美日本国产专区一区| 国产精品人人爽人人做我的可爱| 中文欧美字幕免费| 欧美一级理论片| 国产综合精品一区| 久久天天狠狠| 亚洲国内高清视频| 亚洲午夜一二三区视频| 国产精品高潮视频| 欧美在线视频一区二区| 欧美高清日韩| 亚洲永久在线| 狠狠综合久久| 欧美久久久久久久| 亚洲影院免费| 免费在线成人| 中文欧美日韩| 国内精品久久久| 欧美成人激情在线| 亚洲网站视频| 欧美成人第一页| 亚洲一区精品视频| 黄色精品在线看| 欧美激情一区在线| 亚洲欧美三级伦理| 亚洲国产日本| 欧美资源在线观看| 亚洲精品一二| 国产午夜精品一区二区三区欧美 | 亚洲一区免费看| 国产一区二区av| 欧美激情一区二区三区不卡| 午夜激情久久久| 亚洲高清视频一区| 久久成人av少妇免费| 亚洲精品欧美在线| 国产亚洲欧美一区二区| 欧美日韩理论| 久久婷婷综合激情| 亚洲欧美国产三级| 亚洲精品国产日韩| 免费成人av| 欧美有码在线观看视频| 亚洲精品在线视频观看| 国内精品久久久久久久果冻传媒 | 亚洲精品视频免费观看| 国产亚洲精品资源在线26u| 欧美日韩成人一区二区| 老色批av在线精品| 欧美中文在线观看| 亚洲综合视频1区| 99伊人成综合| 亚洲人永久免费| 亚洲福利在线视频| 美脚丝袜一区二区三区在线观看| 性欧美激情精品| 亚洲先锋成人| 亚洲特级片在线| 99视频精品全国免费| 亚洲七七久久综合桃花剧情介绍| 国产主播在线一区| 国产日韩精品入口| 国产伦理一区| 国产精品一卡二| 国产麻豆精品视频| 国产麻豆9l精品三级站| 国产精品捆绑调教| 国产精品高潮呻吟久久av无限| 欧美人与性禽动交情品| 欧美精品一区二区三区在线播放| 美女免费视频一区| 欧美精品日韩精品| 欧美日本一道本| 欧美日韩中文在线| 国产精品久久久久久久久久免费看 | 亚洲大片在线| 亚洲国产日韩一区二区| 亚洲国产高清aⅴ视频| 亚洲二区视频在线| 亚洲精品视频在线| 一区二区三区产品免费精品久久75 | 一本色道久久99精品综合| 一本色道久久88亚洲综合88| 日韩天堂在线观看| 亚洲一区欧美激情| 久久九九精品99国产精品| 久久久www| 欧美国产一区二区三区激情无套| 亚洲第一黄色网| 一区二区免费在线观看| 午夜精品久久久久久99热| 久久精品亚洲精品| 欧美高清视频一区二区| 欧美三级视频在线观看| 国产欧美一区二区三区久久人妖| 黄色成人精品网站| 99国产精品久久久久久久成人热 | 红桃视频成人| 亚洲免费激情| 欧美一区二区三区喷汁尤物| 六月天综合网| 日韩视频一区| 久久av最新网址| 欧美经典一区二区| 国产日韩欧美视频在线| 亚洲经典自拍| 欧美专区福利在线| 亚洲老司机av| 欧美中文字幕精品| 欧美色中文字幕| 精品粉嫩aⅴ一区二区三区四区| 亚洲理论在线| 久热精品在线视频| 中国女人久久久| 欧美凹凸一区二区三区视频| 国产精品视频九色porn| 亚洲经典在线看| 久久天堂国产精品| 亚洲午夜一区二区三区| 欧美成人免费在线| 国产手机视频一区二区| 在线视频一区观看| 牛牛影视久久网| 亚洲欧美中文日韩在线| 欧美日韩视频一区二区三区| 亚洲大胆人体在线| 久久久精品国产99久久精品芒果| 99在线热播精品免费99热| 毛片基地黄久久久久久天堂| 国产综合香蕉五月婷在线| 亚洲欧美日产图| av不卡在线|