遊戲手柄(JoyStick)的延時處理

如果做過遊戲開發的朋友應該都知道,手柄的按鍵事件是非常敏感的,如果不對其進行延時處理,可能會讓玩家覺得很難掌握和控制遊戲,也就是所謂的沒有“手感”,由於自己在開發項目中要用到這樣的處理,所以寫了延時功能的代碼,倉促編寫,可能有很多地方不合理,望大家批評指正,代碼如下(vc6):

 

 

#include <Dinput.h>
#include <mmsystem.h>

#pragma comment(lib,"Winmm.lib ")

//時間差
class DELTATIME
{
private:
	DWORD tmpTime;
	
public:
	DWORD DeltaTimes;
	DELTATIME()
	{
		DeltaTimes=0;
		tmpTime=timeGetTime();
	}
	void ClearTime()
	{
		DeltaTimes=0;
		tmpTime=timeGetTime();
	}
	DWORD getDtTime()
	{
		DeltaTimes=timeGetTime()-tmpTime;
		return DeltaTimes;
	}

};




//調節手柄靈敏度
class BTNDELTA
{
private:
	long btnCount;
	long btnMaxPressCount;//默認爲連續觸發10次算一次
public:
	BTNDELTA()
	{
		btnCount=0;
		btnMaxPressCount=6;
	}
	void SetMaxCount(long nMax)
	{
		btnMaxPressCount=nMax;
		btnCount=0;
	}
	bool isGotOnePress()
	{
		if(++btnCount>btnMaxPressCount)
		{
			return true;
		}
		return false;
			
	}
	void ClearCount()
	{
		btnCount=0;
	}

};



class JOYBUTTON
{
	UINT btnID;		//要使用的按鍵,可以是方向鍵也可以是普通按鍵
	bool isStartPress;//狀態標記
	bool isPressed;//狀態標記
	DWORD firstTime;
	DWORD otherCountDt;
	DELTATIME btnDtTime;//時間差
	BTNDELTA  btnContDt;
	DIJOYSTATE *js;//dx8.1中的手柄狀態信息結構體
	bool isDirectionBtn;//是否是方向鍵,判斷方向鍵是比較特殊的
	bool isGoNext;//連續按住是否會不停觸發

public:
	JOYBUTTON()
	{
	/*
		isPressed=false;
		isStartPress=false;
		js=NULL;
		firstTime=500;
		btnContDt.ClearCount();*/
		
	}
	void SetIsCanGoNext(bool isGoNext=true)
	{
		this->isGoNext=isGoNext;
	}
	
	void SetJoyButtonInfo(DWORD bIDorDirection,DIJOYSTATE *btnjs,bool isDirection=false,bool isGoNext=true,long firstWaitTime=500,long otherWaitTime=5)
	{
		isPressed=false;
		isStartPress=false;
		js=NULL;
		firstTime=500;
		btnContDt.ClearCount();
		otherCountDt=otherWaitTime;

		btnID=bIDorDirection;
		js=btnjs;
		firstTime=firstWaitTime;
		btnContDt.SetMaxCount(otherWaitTime);

		isDirectionBtn=isDirection;
		this->isGoNext=isGoNext;

	}
	bool isCanGoNext()
	{
		bool ret=false;
		if(isDirectionBtn==true)
		{
			if(getJoyDirection(js)==btnID)
			{
				ret=true;
			}
		}
		else
		{
			ret=isJoyPressed(js,btnID);
		}

		if(ret==false&&isPressed==true)
		{
			//如果是現在的狀態沒按下,但是確實是被按過,那麼就要復位
			isPressed=false;
			isStartPress=false;
			btnDtTime.ClearTime();
			btnContDt.ClearCount();
		}

		
		

		if(ret==true)
		{ 
			if(isStartPress==false)
			{
				if(isPressed==false)
				{
					btnDtTime.ClearTime();//如果是剛開始就要清除一下時間
				}
				if(btnDtTime.getDtTime()<firstTime)
				{	
					if(isPressed==false)
					{
						isPressed=true;
						return true;
					}
					
				}
				else
				{
					btnContDt.ClearCount();
					isStartPress=true;//第一次完畢啦
				}
			}
			else
			{
				if(isGoNext==true)
				{
					if(btnContDt.isGotOnePress())
					{
						btnContDt.ClearCount();
						return true;
					}
				}
				
			}
			
		}

		return false;
	}

};

 

一下是判斷按鍵狀態的代碼:

 

#define JOY_DIC_UP			 1
#define JOY_DIC_RIGHT		 2
#define JOY_DIC_DOWN		 4
#define JOY_DIC_LEFT		 8

#define JOY_DIC_UPRIGHT		 3
#define JOY_DIC_DOWNRIGHT	 6
#define JOY_DIC_LEFTUP	     9
#define JOY_DIC_LEFTDOWN	12


bool isJoyPressed(DIJOYSTATE *js,UINT btnID)
{
	if(js->rgbButtons[btnID]&0x80)
	{
		return true;
	}
	return false;
}

DWORD getJoyDirection(DIJOYSTATE *js)
{
	DWORD dwDirect=0;
	if(js->lY<0)
		dwDirect|=JOY_DIC_UP;
	
	if(js->lY>0)
		dwDirect|=JOY_DIC_DOWN;
	
	if(js->lX<0)
		dwDirect|=JOY_DIC_LEFT;
	
	if(js->lX>0)
		dwDirect|=JOY_DIC_RIGHT;
	
	
	return dwDirect;
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章