⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyboard.cpp

📁 这是一个屠宰场游戏,此游戏很休闲的请大家下载来看看.
💻 CPP
字号:
/*******************************************************************
 *         Advanced 3D Game Programming using DirectX 7.0
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   Title: Keyboard.cpp
 *    Desc: Wrapper of a DirectInput keyboard object
 *          
 * copyright (c) 1999 by Adrian Perez
 * See license.txt for modification and distribution information
 ******************************************************************/
#include <windows.h>
#include "GameErrors.h"
#include "InputLayer.h"

#include <stack>
using namespace std;
#include "Keyboard.h"


cKeyboard::cKeyboard( HWND hWnd )  //初始化DirectInput键盘输入
{
	m_pTarget = NULL;
	
	HRESULT hr;

	/**
	   首先调用全局内联函数Input()获取 static cInputLayer* m_pGlobalILayer 
       即cInputLayer类的实例的指针
	   然后通过该指针调用GetDInput()函数获取  LPDIRECTINPUT8 接口指针
	 */
	LPDIRECTINPUT8 pDI = Input()->GetDInput();

	/**
	 * Create the keyboard device
	   创建键盘设备
	 */
    hr = pDI->CreateDevice(
		GUID_SysKeyboard,
		&m_pDevice, 
		NULL); 
    if( FAILED(hr) )
    { 
        throw cGameError("Keyboard could not be created\n");
    } 
 
	/**
	 * Set the keyboard data format
	   设置键盘数据格式 
	 */
    hr = m_pDevice->SetDataFormat(&c_dfDIKeyboard); 
    if( FAILED(hr) )
    { 
		SafeRelease( m_pDevice );
        throw cGameError("Keyboard could not be created\n");
    } 
 
    /**
	 * Set the cooperative level 
	   设置协作等级
	 */
    hr = m_pDevice->SetCooperativeLevel(
		hWnd,
		DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 
    if( FAILED(hr) )
    { 
		SafeRelease( m_pDevice );
        throw cGameError("Keyboard coop level could not be changed\n");
    } 

	// CHANGE: we used to grab the keyboard here, but this would cause
	// a crash if the app didn't have focus when it started up.  This is,
	// of course, an undesirable trait :)

	memset( m_keyState, 0, 256*sizeof(bool) );	
}

cKeyboard::~cKeyboard()
{
	if( m_pDevice )
	{
		m_pDevice->Unacquire();
		SafeRelease( m_pDevice );
	}
}



void cKeyboard::SetReceiver( iKeyboardReceiver* pTarget )  //设置接收器
{
	// Set the new target.
	m_pTarget = pTarget;
}


bool cKeyboard::Poll( int key ) //判断某一个键是否被按下
{
	// stuff goes in here.
	if( m_keyState[key] & 0x80 )
		return true;
	return false;
}


eResult cKeyboard::Update()  //更新键盘输入
{
	char     newState[256]; 
    HRESULT  hr; 
 
	// CHANGE: Polling can't hurt, but it can help.
    hr = m_pDevice->Poll();   //轮流检测
    hr = m_pDevice->GetDeviceState(sizeof(newState),(LPVOID)&newState); //读键盘状态

    if( FAILED(hr) ) //失败处理
    { 
		hr = m_pDevice->Acquire();
		if( FAILED( hr ) )
		{
			return resFailed;
		}

	    hr = m_pDevice->Poll(); 
	    hr = m_pDevice->GetDeviceState(sizeof(newState),(LPVOID)&newState); 
		if( FAILED( hr ) )
		{
			return resFailed;
		}
    } 


	if( m_pTarget )  //定义了接收器
	{
		int i;
		for( i=0; i< 256; i++ )
		{
			
			//键盘弹起处理
			if( m_keyState[i] != newState[i] )  //如果前后状态不同,键盘有变动
			{
				// Something happened to this key since the last time we checked
				if( !(newState[i] & 0x80) )  //如果该键没有被按下,就是弹起了
				{
					// It was Released
					m_pTarget->KeyUp( i );   //调用接受器的KeyUp处理函数
				}
				else
				{
					// Do nothing; it was just pressed, it'll get a keydown 
					// in a bit, and we dont' want to send the signal to the 
					// input target twice
				}
			}

			// copy the state over (we could do a memcpy at the end, but this
			// will have better cache performance)
			m_keyState[i] = newState[i];   //更新键盘状态

			//键盘按下处理
			if( Poll( i ) )   //该键目前被按下 m_keyState[key] & 0x80
			{
				// It was pressed
				m_pTarget->KeyDown( i );   //调用接受器的KeyDown处理函数
			}
		}
	}
	else   //没有定义接收器
	{
		// copy the new states over.
		memcpy( m_keyState, newState, 256 );
	}
 
	return resAllGood;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -