DirectX 10 教程13:Direct Input
原文地址:Tutorial 13: Direct Input(http://www.rastertek.com/dx10tut13.html)。
源代码下载:dx10tut13.zip。
本教程介绍如何使用Direct Input。本教程是建立在上一个教程的基础上的。
Direct Input是DirectX API提供的与输入设备交互的高速方法。DirectX 10中的Direct Input版本并没有提高,仍是8。因为Direct Input一开始就设计得非常好(与direct sound类似),所以它无需进行任何升级。相对于windows输入系统,Direct Input的速度要快得多。任何需要高响应速度的输入设备的高性能程序都应使用Direct Input。
本教程主要介绍通过Direct Input使用键盘和鼠标。我们还会介绍使用TextClass在屏幕上显示当前的鼠标位置。前面的教程中我们已经有了一个InputClass,所以我们只需重写这个类,用Direct Input代替前面使用的Windows方法。
Inputclass.h
//////////////////////////////////////////////////////////////////////////////// // Filename: inputclass.h //////////////////////////////////////////////////////////////////////////////// #ifndef _INPUTCLASS_H_ #define _INPUTCLASS_H_
你需要定义Direct Input的版本,否则编译器会显示额外的信息表示默认为版本8。
/////////////////////////////// // PRE-PROCESSING DIRECTIVES // /////////////////////////////// #define DIRECTINPUT_VERSION 0x0800
下面链接的两个库用于Direct Input。
/////////////
// LINKING //
/////////////
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")
//////////////
// INCLUDES //
//////////////
#include <dinput.h>
////////////////////////////////////////////////////////////////////////////////
// Class name: InputClass
////////////////////////////////////////////////////////////////////////////////
class InputClass
{
public:
InputClass();
InputClass(const InputClass&);
~InputClass();
bool Initialize(HINSTANCE, HWND, int, int);
void Shutdown();
bool Frame();
bool IsEscapePressed();
void GetMouseLocation(int&, int&);
private:
bool ReadKeyboard();
bool ReadMouse();
void ProcessInput();
private:
下面三个私有变量是指向Direct Input,键盘设置和鼠标设备的指针。
IDirectInput8* m_directInput; IDirectInputDevice8* m_keyboard; IDirectInputDevice8* m_mouse;
下面两个私有变量用于记录当前键盘和鼠标的状态。
unsigned char m_keyboardState[256]; DIMOUSESTATE m_mouseState; int m_screenWidth, m_screenHeight; int m_mouseX, m_mouseY; }; #endif
Inputclass.cpp
//////////////////////////////////////////////////////////////////////////////// // Filename: inputclass.cpp //////////////////////////////////////////////////////////////////////////////// #include "inputclass.h"
构造函数中将Direct Input接口变量设置为null。
InputClass::InputClass()
{
m_directInput = 0;
m_keyboard = 0;
m_mouse = 0;
}
InputClass::InputClass(const InputClass& other)
{
}
InputClass::~InputClass()
{
}
bool InputClass::Initialize(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight)
{
HRESULT result;
// Store the screen size which will be used for positioning the mouse cursor.
m_screenWidth = screenWidth;
m_screenHeight = screenHeight;
// Initialize the location of the mouse on the screen.
m_mouseX = 0;
m_mouseY = 0;
下面的方法初始化Direct Input接口,有了这个接口对象才可以初始化其他输入设备。
// Initialize the main direct input interface.
result = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_directInput, NULL);
if(FAILED(result))
{
return false;
}
首先初始化键盘设备。
// Initialize the direct input interface for the keyboard.
result = m_directInput->CreateDevice(GUID_SysKeyboard, &m_keyboard, NULL);
if(FAILED(result))
{
return false;
}
// Set the data format. In this case since it is a keyboard we can use the predefined data format.
result = m_keyboard->SetDataFormat(&c_dfDIKeyboard);
if(FAILED(result))
{
return false;
}
设置键盘的cooperative级别对于你如何使用设备是非常重要的。本例中我们将它设置为不和其他程序共享(DISCL_EXCLUSIVE),如果你按下键盘,那么只有你的程序可以看到这个输入,而其他程序并不能访问这个输入。如果想让其他程序也能访问到你的程序中的键盘输入,则需要设置为non-exclusive (DISCL_NONEXCLUSIVE)。如果设置为non-exclusive且处于窗口模式,你就必须在设备丢失时加以检查,当窗口重新获得焦点后,你需要重新acquire这个设备使之能再次使用。焦点丢失往往发生在其他窗口在你的窗口之上获得了焦点或者你的窗口被最小化的时候。
// Set the cooperative level of the keyboard to not share with other programs.
result = m_keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
if(FAILED(result))
{
return false;
}
创建了键盘设备之后调用Acquire获得对键盘的访问。
// Now acquire the keyboard.
result = m_keyboard->Acquire();
if(FAILED(result))
{
return false;
}
然后初始化鼠标设备。
// Initialize the direct input interface for the mouse.
result = m_directInput->CreateDevice(GUID_SysMouse, &m_mouse, NULL);
if(FAILED(result))
{
return false;
}
// Set the data format for the mouse using the pre-defined mouse data format.
result = m_mouse->SetDataFormat(&c_dfDIMouse);
if(FAILED(result))
{
return false;
}
我们使用non-exclusive cooperative作为鼠标的设置。我们需要在窗口失去焦点和重新得到焦点时进行检查,并重新获得对鼠标设备的控制。
// Set the cooperative level of the mouse to share with other programs.
result = m_mouse->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
if(FAILED(result))
{
return false;
}
设置完鼠标设备后调用Acquire获取对鼠标的访问。
// Acquire the mouse.
result = m_mouse->Acquire();
if(FAILED(result))
{
return false;
}
return true;
}
Shutdown方法释放这两个设备和Direct Input接口。注意设备首先要Unacquire才能被释放。
void InputClass::Shutdown()
{
// Release the mouse.
if(m_mouse)
{
m_mouse->Unacquire();
m_mouse->Release();
m_mouse = 0;
}
// Release the keyboard.
if(m_keyboard)
{
m_keyboard->Unacquire();
m_keyboard->Release();
m_keyboard = 0;
}
// Release the main interface to direct input.
if(m_directInput)
{
m_directInput->Release();
m_directInput = 0;
}
return;
}
Frame方法将设备的当前状态读取到状态缓存中,然后进一步处理状态。
bool InputClass::Frame()
{
bool result;
// Read the current state of the keyboard.
result = ReadKeyboard();
if(!result)
{
return false;
}
// Read the current state of the mouse.
result = ReadMouse();
if(!result)
{
return false;
}
// Process the changes in the mouse and keyboard.
ProcessInput();
return true;
}
ReadKeyboard会将键盘状态读取到m_keyboardState变量中。这个状态会显示任意一个键是否处于按下状态。如果读取键盘失败,可能的原因有五个,但我们需要处理的是其中的两个:设备失去焦点或设备变为un-acquired,如果发生以上两种情况,我们就需要在每帧调用acquire直到我们重新获得对设备的控制。窗口在最小化时Acquire会失败,但是当窗口恢复时Acquire就会成功,此时我们就能重新获取键盘状态了。本教程不会讨论另外三种失败的情况。
bool InputClass::ReadKeyboard()
{
HRESULT result;
// Read the keyboard device.
result = m_keyboard->GetDeviceState(sizeof(m_keyboardState), (LPVOID)&m_keyboardState);
if(FAILED(result))
{
// If the keyboard lost focus or was not acquired then try to get control back.
if((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
{
m_keyboard->Acquire();
}
else
{
return false;
}
}
return true;
}
ReadMouse读取鼠标的状态,这个状态只包含自上一帧以来鼠标位置的变化信息,例如鼠标向右移动了5个单位,但你无法知道鼠标在屏幕上的确切位置。鼠标位置变化的信息非常重要,我们可以利用这个信息保存鼠标在屏幕上的位置。
bool InputClass::ReadMouse()
{
HRESULT result;
// Read the mouse device.
result = m_mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&m_mouseState);
if(FAILED(result))
{
// If the mouse lost focus or was not acquired then try to get control back.
if((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
{
m_mouse->Acquire();
}
else
{
return false;
}
}
return true;
}
ProcessInput方法用于处理上一帧以来设备的变化情况。在本教程中,我们只是做了一个鼠标位置更新的操作,这个操作类似于Windows处理鼠标位置的操作。我们使用了m_mouseX和m_mouseY变量,然后将鼠标位置的变化累加在这两个变量上。这样我们就可以基于用户的输入保存鼠标位置了。
注意我们还需确保鼠标位置不要超出屏幕范围。如果用户将鼠标一直向左移动,我们就将这个位置设置为0直至用户将鼠标向右移动为止。
void InputClass::ProcessInput()
{
// Update the location of the mouse cursor based on the change of the mouse location during the frame.
m_mouseX += m_mouseState.lX;
m_mouseY += m_mouseState.lY;
// Ensure the mouse location doesn't exceed the screen width or height.
if(m_mouseX < 0) { m_mouseX = 0; }
if(m_mouseY < 0) { m_mouseY = 0; }
if(m_mouseX > m_screenWidth) { m_mouseX = m_screenWidth; }
if(m_mouseY > m_screenHeight) { m_mouseY = m_screenHeight; }
return;
}
我还在InputClass中添加了一个叫做IsEscapePressed的方法,这个方法展示了如何检查在键盘按下了特定的键。你可以编写其他方法检查你所需要饿其他按键。
bool InputClass::IsEscapePressed()
{
// Do a bitwise and on the keyboard state to check if the escape key is currently being pressed.
if(m_keyboardState[DIK_ESCAPE] & 0x80)
{
return true;
}
return false;
}
GetMouseLocation是一个辅助函数,它返回鼠标的位置。GraphicsClass获取这个信息,然后使用TextClass将鼠标的X和Y坐标绘制在屏幕上。
void InputClass::GetMouseLocation(int& mouseX, int& mouseY)
{
mouseX = m_mouseX;
mouseY = m_mouseY;
return;
}
Systemclass.cpp
本教程中移除了Windows的输入系统,使用的是DirectX Input系统。
////////////////////////////////////////////////////////////////////////////////
// Filename: systemclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "systemclass.h"
SystemClass::SystemClass()
{
m_Input = 0;
m_Graphics = 0;
}
SystemClass::SystemClass(const SystemClass& other)
{
}
SystemClass::~SystemClass()
{
}
bool SystemClass::Initialize()
{
int screenWidth, screenHeight;
bool result;
// Initialize the width and height of the screen to zero before sending the variables into the function.
screenWidth = 0;
screenHeight = 0;
// Initialize the windows api.
InitializeWindows(screenWidth, screenHeight);
// Create the input object. This object will be used to handle reading the keyboard input from the user.
m_Input = new InputClass;
if(!m_Input)
{
return false;
}
Input对象的Initialize方法的参数与前面的教程不同,现在它需要window,instance和屏幕大小作为参数。这个方法还返回一个布尔值表示是否已经成功地初始化了Direct Input。
// Initialize the input object.
result = m_Input->Initialize(m_hinstance, m_hwnd, screenWidth, screenHeight);
if(!result)
{
MessageBox(m_hwnd, L"Could not initialize the input object.", L"Error", MB_OK);
return false;
}
// Create the graphics object. This object will handle rendering all the graphics for this application.
m_Graphics = new GraphicsClass;
if(!m_Graphics)
{
return false;
}
// Initialize the graphics object.
result = m_Graphics->Initialize(screenWidth, screenHeight, m_hwnd);
if(!result)
{
return false;
}
return true;
}
void SystemClass::Shutdown()
{
// Release the graphics object.
if(m_Graphics)
{
m_Graphics->Shutdown();
delete m_Graphics;
m_Graphics = 0;
}
释放Input对象在delete之前需要调用它的Shutdown方法。
m_Input->Shutdown();
delete m_Input;
m_Input = 0;
}
// Shutdown the window.
ShutdownWindows();
return;
}
void SystemClass::Run()
{
MSG msg;
bool done, result;
// Initialize the message structure.
ZeroMemory(&msg, sizeof(MSG));
// Loop until there is a quit message from the window or the user.
done = false;
while(!done)
{
// Handle the windows messages.
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// If windows signals to end the application then exit out.
if(msg.message == WM_QUIT)
{
done = true;
}
else
{
// Otherwise do the frame processing.
result = Frame();
if(!result)
{
done = true;
}
}
检测Escape键是否被按下的过程与前面的教程有所不同,现在使用的是InputClass中的一个辅助方法。
// Check if the user pressed escape and wants to quit.
if(m_Input->IsEscapePressed() == true)
{
done = true;
}
}
return;
}
bool SystemClass::Frame()
{
bool result;
int mouseX, mouseY;
在Frame方法中我们调用了Input对象的Frame方法更新键盘和鼠标的状态,由于这个调用可能失败,所以需要检查返回值。
// Do the input frame processing.
result = m_Input->Frame();
if(!result)
{
return false;
}
在读取了输入设备的更新状态之后,我们更新GraphicsClass中的鼠标位置,这样就可以将这些信息绘制在屏幕上了。
// Get the location of the mouse from the input object,
m_Input->GetMouseLocation(mouseX, mouseY);
// Do the frame processing for the graphics object.
result = m_Graphics->Frame(mouseX, mouseY);
if(!result)
{
return false;
}
// Finally render the graphics to the screen.
result = m_Graphics->Render();
if(!result)
{
return false;
}
return true;
}
现在可以从MessageHandler方法中移除Windows键盘的读取,Direct Input会处理上述操作。
LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
return DefWindowProc(hwnd, umsg, wparam, lparam);
}
Graphicsclass.h
////////////////////////////////////////////////////////////////////////////////
// Filename: graphicsclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _GRAPHICSCLASS_H_
#define _GRAPHICSCLASS_H_
/////////////
// GLOBALS //
/////////////
const bool FULL_SCREEN = true;
const bool VSYNC_ENABLED = true;
const float SCREEN_DEPTH = 1000.0f;
const float SCREEN_NEAR = 0.1f;
///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "d3dclass.h"
#include "cameraclass.h"
#include "textclass.h"
////////////////////////////////////////////////////////////////////////////////
// Class name: GraphicsClass
////////////////////////////////////////////////////////////////////////////////
class GraphicsClass
{
public:
GraphicsClass();
GraphicsClass(const GraphicsClass&);
~GraphicsClass();
bool Initialize(int, int, HWND);
void Shutdown();
Frame方法现在有两个参数对应鼠标的坐标。
bool Frame(int, int); bool Render(); private: D3DClass* m_D3D; CameraClass* m_Camera; TextClass* m_Text; }; #endif
Graphicsclass.cpp
下面的代码只列出了与上一个教程中不同的地方。
//////////////////////////////////////////////////////////////////////////////// // Filename: graphicsclass.cpp //////////////////////////////////////////////////////////////////////////////// #include "graphicsclass.h"
Frame方法的参数为鼠标的X和Y坐标,然后TextClass对象基于这两个坐标将鼠标坐标的信息显示在屏幕上。
bool GraphicsClass::Frame(int mouseX, int mouseY)
{
bool result;
// Set the location of the mouse.
result = m_Text->SetMousePosition(mouseX, mouseY);
if(!result)
{
return false;
}
// Set the position of the camera.
m_Camera->SetPosition(0.0f, 0.0f, -10.0f);
return true;
}
Textclass.h
////////////////////////////////////////////////////////////////////////////////
// Filename: textclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _TEXTCLASS_H_
#define _TEXTCLASS_H_
///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "fontclass.h"
#include "fontshaderclass.h"
////////////////////////////////////////////////////////////////////////////////
// Class name: TextClass
////////////////////////////////////////////////////////////////////////////////
class TextClass
{
private:
struct SentenceType
{
ID3D10Buffer *vertexBuffer, *indexBuffer;
int vertexCount, indexCount, maxLength;
float red, green, blue;
};
struct VertexType
{
D3DXVECTOR3 position;
D3DXVECTOR2 texture;
};
public:
TextClass();
TextClass(const TextClass&);
~TextClass();
bool Initialize(ID3D10Device*, HWND, int, int, D3DXMATRIX);
void Shutdown();
void Render(ID3D10Device*, D3DXMATRIX, D3DXMATRIX);
TextClass现在包含了一个新方法用于设置鼠标的位置。
bool SetMousePosition(int, int); private: bool InitializeSentence(SentenceType**, int, ID3D10Device*); bool UpdateSentence(SentenceType*, char*, int, int, float, float, float); void ReleaseSentence(SentenceType**); void RenderSentence(ID3D10Device*, SentenceType*, D3DXMATRIX, D3DXMATRIX); private: FontClass* m_Font; FontShaderClass* m_FontShader; int m_screenWidth, m_screenHeight; D3DXMATRIX m_baseViewMatrix; SentenceType* m_sentence1; SentenceType* m_sentence2; }; #endif
Textclass.cpp
下面的代码只包含与上一个教程不同的部分。
/////////////////////////////////////////////////////////////////////////////// // Filename: textclass.cpp /////////////////////////////////////////////////////////////////////////////// #include "textclass.h"
TextClass类中的新方法SetMousePosition用于将鼠标的X和Y坐标转换为两个字符串,然后将字符串显示在屏幕上。
bool TextClass::SetMousePosition(int mouseX, int mouseY)
{
char tempString[16];
char mouseString[16];
bool result;
// Convert the mouseX integer to string format.
_itoa_s(mouseX, tempString, 10);
// Setup the mouseX string.
strcpy_s(mouseString, "Mouse X: ");
strcat_s(mouseString, tempString);
// Update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence1, mouseString, 20, 20, 1.0f, 1.0f, 1.0f);
if(!result)
{
return false;
}
// Convert the mouseY integer to string format.
_itoa_s(mouseY, tempString, 10);
// Setup the mouseY string.
strcpy_s(mouseString, "Mouse Y: ");
strcat_s(mouseString, tempString);
// Update the sentence vertex buffer with the new string information.
result = UpdateSentence(m_sentence2, mouseString, 20, 40, 1.0f, 1.0f, 1.0f);
if(!result)
{
return false;
}
return true;
}
总结
如你所见,在DirectX 10中设置Direct Input非常简单,它提供了对输入设备的高速访问。

练习
1.编译程序并移动鼠标,观察鼠标位置文本的变化。
2.使用教程——绘制2D的代码创建你自己的光标,使之能随着你的鼠标的移动而移动。
3.实现一个功能,可以读取键盘状态并将它们显示在屏幕上。
文件下载(已下载 1489 次)发布时间:2012/7/29 上午12:59:02 阅读次数:9323
