游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1347|回复: 1

关于directinput的问题。

[复制链接]

1

主题

3

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2005-3-24 14:24:00 | 显示全部楼层 |阅读模式
我做了个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]

20

主题

398

帖子

398

积分

中级会员

Rank: 3Rank: 3

积分
398
发表于 2005-3-24 20:12:00 | 显示全部楼层

Re:关于directinput的问题。

很复杂的一个问题,有本书《高性能Windows图形应用程序设计》(好像是叫这个名字),那里面的处理方法挺好,你可以参考一下!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-24 23:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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