|
|
我做了个directinput鼠标控制的类,并通过CurrentState(HWND hWnd)得到一个state结构,依据此结构对我另外的directdraw来进行人物,地图控制。比如,当state的结构里的verge的值为1,2,3,4时
就说明鼠标在地屏幕边缘,需要对地图进行卷动。若为0则不必。
还有就是,看本版有人说,用初始坐标跟踪当前坐标的方法会有误差。那么怎么定义个结构,强制让它返回当前坐标,而不是滑动量?,本人因找工作一直在写,很想进游戏编程这行,但知道这很难,我也很菜。希望大家帮帮我!
声明如下
#pragma once
#include <windows.h>
#include <mmsystem.h>
#include <dinput.h>
#include <windef.h>
#define LEFTVERGE 1
#define TOPVERGE 2
#define RIGHTVERGE 3
#define BOTTOMVERGE 4
#define INCLIENT 0
struct state
{
POINT point;
bool left;
bool right;
int verge;
};
class Dxinput
{
POINT GetCurrentXY(DIMOUSESTATE2 MouseState);//得到当前鼠标坐标
DIMOUSESTATE2 GetMouseState(HWND hWnd);//得到一个DIMOUSESTATE2的当前状态
public:
Dxinput(void);
~Dxinput(void);
HRESULT DxInputInit(HWND hWnd,HINSTANCE hinstance);//初始化
void SetCurrentXY(HWND hWnd,POINT point);//设置初始坐标
state CurrentState(HWND hWnd);//取得当前状态
};
实现:
#include "Dxinput.h"
LPDIRECTINPUT8 pDI;
LPDIRECTINPUTDEVICE8 pDMO;
HRESULT resultInput;
DIMOUSESTATE2 MState;
//---------------------------------
RECT rect;
int cx ,cy ;
//---------------------------------
//---------------------------------
Dxinput: xinput(void)
{
pDI=NULL;
pDMO=NULL;
}
Dxinput::~Dxinput(void)
{
if(pDI) {pDI->Release();pDI=NULL;}
if(pDMO) pDMO->Unacquire();
if(pDMO) {pDMO->Release();pDMO=NULL;}
}
HRESULT Dxinput::DxInputInit(HWND hWnd,HINSTANCE hinstance)
{
//HINSTANCE hinst =::AfxGetInstanceHandle();//这个上网查了好久才知道用下面的函数替代
resultInput = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&pDI, NULL );
//DirectInput8Create(hinstance,DIRECTINPUT_VERSION ,
//IID_IDirectInput8A ,(void**)&pDI, NULL);
if(resultInput != DI_OK)
MessageBox(hWnd,"建立 DirectInput 对象失败!","error box",MB_OK);
//-----------------------------------------------------
resultInput = pDI->CreateDevice(GUID_SysMouse, &pDMO, NULL);
if(resultInput != DI_OK)
MessageBox(hWnd,"建立鼠标输入装置失败!","error box",MB_OK);
//-----------------------------------------------------
resultInput = pDMO->SetDataFormat(&c_dfDIMouse2);
if(resultInput != DI_OK)
MessageBox(hWnd,"设定数据格式失败!","error box",MB_OK);
//------------------------------------------------------
resultInput = pDMO->SetCooperativeLevel(hWnd,
DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
if(resultInput != DI_OK)
MessageBox(hWnd,"设定程序协调层级失败!","error box",MB_OK);
//-----------------------------------------------------
resultInput = pDMO->Acquire();
if(resultInput != DI_OK)
MessageBox(hWnd,"取用输入设备失败!","error box",MB_OK);
return resultInput;
}
void Dxinput::SetCurrentXY(HWND hWnd,POINT point)
{
ClientToScreen(hWnd,&point);
::SetCursorPos(point.x,point.y);
ScreenToClient(hWnd,&point);
}
POINT Dxinput::GetCurrentXY(DIMOUSESTATE2 MouseState)
{
POINT point;
cx+=MouseState.lX;
cy+=MouseState.lY;
point.x=cx;
point.y=cy;
return point;
}
DIMOUSESTATE2 Dxinput::GetMouseState(HWND hWnd)
{
DIMOUSESTATE2 MouseState;
resultInput=pDMO->GetDeviceState(sizeof(MouseState),(LPVOID)&MouseState);
if(resultInput !=DI_OK)
MessageBox(hWnd,"取得鼠标状态失败","error box",MB_OK);
return MouseState;
}
state Dxinput::CurrentState(HWND hWnd)
{
POINT currentpoint;
DIMOUSESTATE2 MouseState;
GetClientRect(hWnd,&rect);
::ClipCursor(&rect);
MouseState=GetMouseState(hWnd);
currentpoint=GetCurrentXY(MouseState);
//---------------------------------
state currentstate;
currentstate.point.x=currentpoint.x;
currentstate.point.y=currentpoint.y;
if(MouseState.rgbButtons[0]&0x80)
{
currentstate.left=true;
}
else if(MouseState.rgbButtons[1]&0x80)
{
currentstate.right=true;
}
//------------------------------
if(currentpoint.x<=rect.left)
currentstate.verge= LEFTVERGE;
else if(currentpoint.x>=rect.right)
currentstate.verge= RIGHTVERGE;
else if(currentpoint.y<=rect.top)
currentstate.verge= TOPVERGE;
else if(currentpoint.y>=rect.bottom)
currentstate.verge= BOTTOMVERGE;
else
currentstate.verge= INCLIENT;
//---------------------------------
return currentstate;
[em4] |
|