【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2863|回复: 0

1.3 Keyboard & Mouse之封装CXKeyboard & CXMouse

[复制链接]
发表于 2006-1-26 09:55:14 | 显示全部楼层 |阅读模式

1、 何为Keyboard & Mouse

“地球人都知道”。DX9提供的接口 IDirectInputDevice8。

2、 二者的功能

Keyboard:读取键盘的按键信息

Mouse:读取鼠标的按键、位置信息,设置光标属性(如用图片表示光标)。

3、 使用过程

创建 IDirectInput8 对象 DirectInput8Create

创建 IDirectInput8 设备(键盘、鼠标等) CreateDevice

设置设备属性 SetCooperativeLevel

SetDataFormat

获取设备使用权 Acquire

读取设备传入的数据 GetDeviceState

释放设备及 IDirectInput8 Release

设置鼠标光标过程:

创建光标图片资源: 参照surfaces

设置光标为指定的图片 SetCursorProperties

设置光标的初始位置 SetCursorPosition

显示光标 ShowCursor

程序段如下:

LPDIRECTINPUT8 g_lpDI;

LPDIRECTINPUTDEVICE8 g_Keyboard;

LPDIRECTINPUTDEVICE8 g_Mouse

//========================================================

HRESULT Result = DirectInput8Create(g_hInst, DIRECTINPUT_VERSION,

IID_IDirectInput8, (void**)&g_lpDI, NULL);

if(SUCCEEDED(Result))

{

//创建键盘设备

Result = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL);

if(SUCCEEDED(Result))

{

g_lpDIDevice->SetCooperativeLevel(g_hWnd, DISCL_FOREGROUND |

DISCL_NONEXCLUSIVE);

g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard);

}

}

//-------------------------------------------------------------------------------------------

//获取按键信息

if(SUCCEEDED(g_Keyboard->Acquire())) //Acquire the device

{

char KeyState[256];

g_Keyboard->GetDeviceState(sizeof(KeyState),(LPVOID)&KeyState);

//根据KeyState返回的数据就可以判断按下何键

}

//====================================================

//创建鼠标

g_lpDI->CreateDevice(GUID_SysMouse, &g_Mouse, NULL);

g_Mouse->SetDataFormat(&c_dfDIMouse);

g_Mouse->SetCooperativeLevel(g_hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);

//------------------------------------------------------------------------

//设置鼠标光标

//获取光标图片

D3DXIMAGE_INFO ImageInfo;

D3DXGetImageInfoFromFile("C:\\Cursor.jpg”, &ImageInfo);

g_pd3dDevice->CreateOffscreenPlainSurface(ImageInfo.Width,

ImageInfo.Height,

ImageInfo.Format,

D3DPOOL_DEFAULT,

&g_MouseCursor,

NULL);

D3DXLoadSurfaceFromFile(g_MouseCursor, NULL, NULL, ("C:\\Cursor.jpg”,

NULL, D3DX_FILTER_NONE, 0xFF000000, NULL);

//设置成指定的光标

g_pd3dDevice->SetCursorProperties(0,0, g_MouseCursor);

//初试位置

g_pd3dDevice->SetCursorPosition(0,0,D3DCURSOR_IMMEDIATE_UPDATE);

//显示光标

g_pd3dDevice->ShowCursor(true);

//-------------------------------------------------------------------------

//获取鼠标信息

if(SUCCEEDED(g_Mouse->Acquire()))

{

DIMOUSESTATE State;

g_Mouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&State);

//信息保存在 State中。

}

//==============================================

//释放设备

if(g_lpDI != NULL)

g_lpDI->Release();

4、 封装
CXKeyboard的封装

class CXKeyboard

{

private:

LPDIRECTINPUTDEVICE8 m_pDevice;

char m_KeyState[256];

public:

CXKeyboard (LPDIRECTINPUT8 pInput, HWND hWnd);

~ CXKeyboard();

bool IsKeyPressed(int Key);

HRESULT Update();

};

CXMouseSurface的封装

为了能制作出动画效果的光标,因此需要一个surfaces列表,因此从CXSurface继承一个CXMouseSurface类,使之能够操作多个图片。

class CXMouseSurface : public CXSurface

{

private:

CXMouseSurface* m_pNext;

int MouseSurfaceType;

UINT HotSpotX;

UINT HotSpotY;

public:

CXMouseSurface* GetNext() {return m_pNext;}

void SetNext(CXMouseSurface* Surf) {m_pNext = Surf;}

int GetSurfaceType() {return MouseSurfaceType;}

void SetSurfaceType(int Type) {MouseSurfaceType = Type;}

UINT GetHotSpotX() {return HotSpotX;}

UINT GetHotSpotY() {return HotSpotY;}

void SetHotSpotX (UINT X) {HotSpotX = X;}

void SetHotSpotY (UINT Y) {HotSpotY = Y;}

CXMouseSurface(LPDIRECT3DDEVICE9 pDevice) : CXSurface(pDevice)

{m_pNext = NULL;}

CXMouseSurface() : CXSurface() {m_pNext = NULL;}

};

CXMouse的封装

class CXMouse

{

private:

CXMouseSurface* m_Surface; //指向光标列表

CXMouseSurface* m_CurrentCursorSurface; //当前光标

LPDIRECTINPUTDEVICE8 m_pDevice;

LPDIRECT3DDEVICE9 m_p3DDevice;

DIMOUSESTATE m_State;

LONG m_iX;

LONG m_iY;

public:

CXMouse (LPDIRECT3DDEVICE9 pDevice, LPDIRECTINPUT8 pInput, HWND hWnd,

bool Exclusive);

~ CXMouse();

HRESULT Update();

LONG GetXPos();

LONG GetYPos();

bool IsButtonPressed(int Button);

HRESULT SetCursorImage();

HRESULT SetMouseCursor(char* FilePath, UINT HotSpotX, UINT HotSpotY,

int Type);

void AddCursorSurface(CXMouseSurface* Surface);

CXMouseSurface* GetFirstSurface() {return m_Surface;}

bool SetCursor(int Type);

CXMouseSurface* GetCurrentCursor() {return m_CurrentCursorSurface;}

void SetCurrentCursor(CXMouseSurface* Surface)

{m_CurrentCursorSurface = Surface;}

void SetCursorPosition(int X, int Y);

HRESULT SetCursorVisible(bool Show);

};

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-2-6 04:29

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表