|
回顧“被動(dòng)方式”開(kāi)發(fā)
在C#對(duì)游戲手柄的編程開(kāi)發(fā)-API篇(1)這篇文章中我們介紹了“被動(dòng)方式”的開(kāi)發(fā)。在此方式下,我們的程序只扮演一個(gè)消息接收者。系統(tǒng)會(huì)定時(shí)告訴我們某個(gè)游戲手柄當(dāng)前的狀態(tài),我們的程序接收到后再按實(shí)際需要進(jìn)行處理即可。但如果你是一個(gè)細(xì)心的人,你會(huì)發(fā)現(xiàn)如果直接按消息事件處理的話會(huì)存在一個(gè)問(wèn)題,如我們按下某個(gè)鍵(比如向上的方向鍵)然后放開(kāi)時(shí),對(duì)于我們“人”來(lái)說(shuō),我們按下與彈起的這兩個(gè)動(dòng)作應(yīng)該只是說(shuō)明我們只點(diǎn)擊這個(gè)按鈕一次。但對(duì)于系統(tǒng)來(lái)說(shuō),它只是機(jī)械地定時(shí)通知我們的程序在某個(gè)時(shí)間內(nèi)游戲手柄的各個(gè)按鈕的狀態(tài),而在我們按下到彈起這段時(shí)間內(nèi),系統(tǒng)有可能已經(jīng)傳遞了N次的消息通知(N值根據(jù)捕捉時(shí)設(shè)置的uPeriod值與你的按鍵速度來(lái)決定),通知手柄有按鈕處于被按下?tīng)顟B(tài),而如果我們就根據(jù)消息包直接處理點(diǎn)擊事件的話,就會(huì)導(dǎo)致問(wèn)題出現(xiàn)(比如在某個(gè)游戲中,我們?cè)O(shè)計(jì)的是當(dāng)點(diǎn)擊一次手柄的右鍵,就將角色向前移動(dòng)一步。但從我們按下按鈕到彈開(kāi)此按鈕這段時(shí)間,由于人的反應(yīng)速度遠(yuǎn)遠(yuǎn)慢于電腦的處理速度,所以這段很短的時(shí)間內(nèi),系統(tǒng)可能已通知了10次以上的消息包表明游戲手柄右鍵已被按下,這就導(dǎo)致我們按一次右鍵,游戲中的角色卻有可能已移動(dòng)了十步之多,這可不是我們想要的結(jié)果)。那我們要怎樣處理這個(gè)“點(diǎn)擊”事件才可以避免重復(fù)通知呢?這就是本篇最后要重點(diǎn)講解的內(nèi)容了……
在講解這個(gè)問(wèn)題的解決方法之前我們?cè)賮?lái)講解一下上文還提到的一種開(kāi)發(fā)方式。
“主動(dòng)方式”的開(kāi)發(fā)
主動(dòng)方式即我們不需要向系統(tǒng)申請(qǐng)注冊(cè)捕捉某個(gè)游戲手柄,我們只是根據(jù)自己的需要按時(shí)去獲取游戲手柄的狀態(tài)信息。
這時(shí)我們就要用到以下的API函數(shù)。
/// /// 獲取操縱桿位置和按鈕狀態(tài) /// /// /// /// [DllImport("winmm.dll")] public static extern int joyGetPos(int uJoyID, ref JOYINFO pji); /// /// 獲取操縱桿位置和按鈕狀態(tài) /// /// /// /// [DllImport("winmm.dll")] public static extern int joyGetPosEx(int uJoyID, ref JOYINFOEX pji); |
上面的兩個(gè)API函數(shù),我們可以從中任選一個(gè),但joyGetPos函數(shù)只能取得1,2,3,4號(hào)四個(gè)按鈕的狀態(tài)。所以建議不用,下面只重講解joyGetPosEx函數(shù)!
JOYINFO 與 JOYINFOEX 是屬于結(jié)構(gòu)體,它們的定義如下:
Code
#region 游戲手柄的位置與按鈕狀態(tài)
/// <summary>
/// 游戲手柄的位置與按鈕狀態(tài)
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct JOYINFO
{
public int wXpos;
public int wYpos;
public int wZpos;
public int wButtons;
}
/// <summary>
/// 游戲手柄的位置與按鈕狀態(tài)
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct JOYINFOEX
{
/// <summary>
/// Size, in bytes, of this structure.
/// </summary>
public int dwSize;
/// <summary>
/// Flags indicating the valid information returned in this structure. Members that do not contain valid information are set to zero.
/// </summary>
public int dwFlags;
/// <summary>
/// Current X-coordinate.
/// </summary>
public int dwXpos;
/// <summary>
/// Current Y-coordinate.
/// </summary>
public int dwYpos;
/// <summary>
/// Current Z-coordinate.
/// </summary>
public int dwZpos;
/// <summary>
/// Current position of the rudder or fourth joystick axis.
/// </summary>
public int dwRpos;
/// <summary>
/// Current fifth axis position.
/// </summary>
public int dwUpos;
/// <summary>
/// Current sixth axis position.
/// </summary>
public int dwVpos;
/// <summary>
/// Current state of the 32 joystick buttons. The value of this member can be set to any combination of JOY_BUTTONn flags, where n is a value in the range of 1 through 32 corresponding to the button that is pressed.
/// </summary>
public int dwButtons;
/// <summary>
/// Current button number that is pressed.
/// </summary>
public int dwButtonNumber;
/// <summary>
/// Current position of the point-of-view control. Values for this member are in the range 0 through 35,900. These values represent the angle, in degrees, of each view multiplied by 100.
/// </summary>
public int dwPOV;
/// <summary>
/// Reserved; do not use.
/// </summary>
public int dwReserved1;
/// <summary>
/// Reserved; do not use.
/// </summary>
public int dwReserved2;
}
#endregion
NET技術(shù):C#對(duì)游戲手柄的編程開(kāi)發(fā)-API篇(2),轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。