Game UI Design (1.3) - KEYBOARD & MOUSE Package

xiaoxiao2021-03-06  63

1.3 KEYBOARD & MOUSE package CXKEYBOARD & CXMOUSE

[http://blog.9cbs.net/mythma]

1, what is Keyboard & Mouse

"well known by all people". DX9 provides interface idirectInputDevice8.

2, the function of both

Keyboard: Read keyboard button information

MOUSE: Read the button, location information, set the cursor property (such as using the picture).

3, use process

Create IDirectInput8 Object DirectInput8CREATE

Create IDirectInput8 devices (keyboard, mouse, etc.) CreateDevice

Set device properties setcooperativeelevel

SetDataFormat

Get equipment use rights acquire

Read the data incoming data getDeviceState

Release equipment and IDirectinput8 Release

Set the mouse cursor process:

Create Cursor Image Resources: Refer to Surfaces

Set the cursor as specified picture setCursorproperties

Set the initial position of the cursor setCursorPosition

Show cursor showcursor

The segment is as follows:

LPDIRECTINPUT8 G_LPDI;

LPDIRECTINPUTDEVICE8 G_KEYBOARD;

LPDIRECTINPUTDEVICE8 G_MOUSE

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

HRESULT RESULT = DirectInput8Create (g_hinst, directinput_version,

IID_IDirectInput8, (void **) & g_lpdi, null;

"succeeded)

{

// Create a keyboard device

Result = g_lpdi-> createDevice (guid_syskeyboard, & g_lpdidevice, null);

"succeeded)

{

g_lpdidevice-> setcooperativeelevel (g_hwnd, discl_foreground |

DISCL_NONEXCLUSIVE;

g_lpdidevice-> setDataFormat (& c_dfdikeyboard);

}

}

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

// Get button information

IF (succeeded (g_keyboard-> acquire ())) // acquire the Device

{

Char KeyState [256]; g_keyboard-> getDeviceState (sizeof (keystate), (lpvoid) & keyState;

/ / If the data returned according to KeyState can be judged to press

}

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

// Create a mouse

g_lpdi-> createDevice (guid_sysmouse, & g_mouse, null);

g_mouse-> setDataFormat;

g_mouse-> setcooperativeelevel (g_hwnd, discl_exclusive | discl_Foreground);

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

// Set the mouse cursor

// Get the cursor picture

D3DXIMAGE_INFO imageInfo;

D3DxGetImageInfofromFile ("c: //cursor.jpg", & imageinfo);

g_pd3ddevice-> createoffscreenPlainsurface (imageinfo.width,

ImageInfo.Height,

ImageInfo.format,

D3DPOOL_DEFAULT,

& g_mousecursor,

NULL);

D3DXLOADSURFAMFROMFILE (G_Mousecursor, Null, NULL, ("C: //cursor.jpg",

NULL, D3DX_FILTER_NONE, 0xFF000000, NULL;

/ / Set to the specified cursor

g_pd3ddevice-> setcursorproperties (0,0, g_mousecursor);

// Initial position

g_pd3ddevice-> setCursorPosition (0, 0, D3DCURSOR_IMMEDITIATE_UPDATE);

// Show cursor

g_pd3ddevice-> showcursor (TRUE);

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

// Get mouse information

IF (succeeded (g_mouse-> acquire ()))

{

Dimousestate State;

g_mouse-> getDeviceState (SizeOf (DimouState), (LPVOID) & State;

// Information is saved in State.

}

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

// Release device

IF (g_lpdi! = NULL)

g_lpdi-> release (); 4, package CXKEYBOARD package

Class CxKeyboard

{

Private:

LPDIRECTINPUTDEVICE8 M_PDEVICE;

Char M_KeyState [256];

PUBLIC:

CxKeyboard (LPDirectInput8 Pinput, HWND HWND);

~ CxKeyboard ();

Bool iskeypressed (int key);

HRESULT UPDATE ();

}

CXMOUSURFACE package

In order to make the cursor of the animation effect, a SURFCES list is required, so it inherits a CXMOUSESURFACE class from cxsurface so that multiple pictures can be operated.

Class CXMOUSURFACE: PUBLIC CXSURFACE

{

Private:

CXMOUSESURFACE * M_PNEXT;

Int mousesurfacety;

Uint HotspotX;

Uint Hotspoty;

PUBLIC:

CXMOUSURFACE * getNext () {return m_pnext;}

Void setNext (CXMOUSESURFACE * SURF) {m_pnext = SURF;}

INT getsurfacetype () {return mousesurfacepe;}

Void setsurfacetype (int type) {mousess} {mousesurfacetype = type;}

Uint gethspotX () {return hotspotX;}

Uint gethspoty () {return hotspoty;}

Void setspotX (uint x) {hotspotx = x;}

Void setspoty (uint y) {hotspoty = y;}

CXMOUSURFACE (LPDIRECT3DDEVICE9 PDEvice): cxsurface (PDEvice)

{m_pnext = null;}

CXMOUSURFACE (): cxsurface () {m_pnext = NULL;}

}

CXMOUSE package

Class CXMOUSE

{

Private:

CXMOUSURFACE * m_surface; // Point to the cursor list

CXMOUSURFACE * M_CURRENTCURSORSURFACE; // Current Cursor

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);

}

转载请注明原文地址:https://www.9cbs.com/read-110914.html

New Post(0)