Getting Started with DirectX game Programming-Part II (Game programming Toolbox)-Keyboard and mouse input

Source: Internet
Author: User
Tags win32

This series of articles by Net_assassin finishing, reproduced please indicate the source.

http://blog.csdn.net/net_assassin/article/category/1100363


Author: Net_assassin e-mail: net_assassin@hotmail.com look forward to interacting with like-minded friends

In this chapter we will learn how to use DirectInput for keyboard and mouse programming to provide support for the most common input devices in the game. The keyboard input Keyboard is the standard input device for all games, even those that are not particularly suitable for keyboard games, so it is a fact that the game uses the keyboard more or less.
So what's the advantage of using DirectInput than using standard API calls?
1. Win32 API is not designed for gaming or speed
2. Win32 can only support that simple rocker, if it is a complex rocker, such as the kind with a few axes, 8 to 10 buttons, etc., WIN32 will not support.
3. Support for the mouse is limited to 3 buttons, 2 axes, and one scroll wheel. But today on the market, many mice have 4, 5 buttons, and even more.
4. Win32 support for keyboards is primarily designed to support keyboard input applications. It has a lot of functions that can automatically handle duplicate keys, can convert keys into ASCII characters, and so on, but these functions are not needed by the game, and this leads to a waste of more precious processor cycles.
5. WIN32 Keyboard Processing code Although you can catch some keys (such as ALT) for you, but this requires special message processing to be able to get the correct operation.
6. Message processing is not the fastest thing in the world. The application gets a lot of mouse messages, but you have to wait until these message queues are empty before you can render the frame, which will cause the entire application to become slow.

DirectInput objects and devices first define the main DirectInput object and the device object to be used by the program:

LPDIRECTINPUT8 dinput;
LPDIRECTINPUTDEVICE8 Dinputdev;

Initialize the DirectInput object:
HRESULT result = Directinput8create (
getmodulehandle (NULL),
directinput_version,
iid_idirectinput8,
(void**) &dinput,
NULL
);

After initializing the object, you can use this object to create a new DirectInput device:
result = Dinput->createdevice (guid_syskeyboard,&dikeyboard,null);

The following two functions are described below:
Directinput8create (
		hinstance hinst,            //instance handle of the current program. If this current instance cannot be obtained directly, then the convenient way to get it is to use the GetModuleHandle function
		DWORD dwversion,            //directinput version is  always Direct_input_ Version.
		refiid RIIDLTF,             //The reference identifier of the DirectInput version that you want to use. Iid_idirectinput8
                lpvoid *ppvout,             //Pointer to main DirectInput object pointer
		lpunknown punkouter         //always null
);
HRESULT CreateDevice (
 refguid rguid,                              //object type to be created, Guid_syskeyboard,guid_sysmouse
 Lpdirectinputdevice  *lplpdirectinputdevice,//The device pointer that receives the address of the DirectInput device handle
 lpunknown  punkouter                        //always null
);
initializing the keyboard and mouseOnce you have a keyboard and mouse DirectInput object and device object, you can initialize the keyboard and mouse handles. Prepare for input. Set data format
HRESULT setDataFormat (
 lpcdidataformat  lpdf         //For keyboards, C_dfdikeyboard should be passed as a parameter; for the mouse, C_dfdimouse should be passed as a parameter.
);
2. Setting the collaboration level determines the degree to which the keyboard or mouse input is passed to the program by priority DirectInput.
HRESULT SetCooperativeLevel (
 hwnd hwnd,    //Window handle
 DWORD dwFlags//Specifies the program's priority on the keyboard and mouse. Keyboard Common Discl_nonexclusive | Discl_foreground.
);
3. Get the device the last step is to get the keyboard or mouse device with the acquire function.
HRESULT acquire (VOID);
	If the return value is positive (DI_OK), the device is successfully acquired. For the keyboard, you can start checking for keyboard keys at this point, and for the mouse, you can start checking for mouse movements and keystrokes.
	//Note: You must get the device back before the game is over, otherwise the DirectInput will be in an unstable state.           
 HRESULT Unacquire (VOID);
reading keyboard keys and mouse Reading the keyboard key requires polling the keyboard somewhere in the game loop to update the key values. First we need to define an array of key values to accept the keyboard device state
Char  keys[256];//We have to populate this character array by polling the keyboard, which calls for the Getdevicestate function to achieve this goal.
HRESULT getdevicestate (
         DWORD  cbdata,          //size of the device state buffer used to populate the data.
         lpvoid   lpvdata         //pointer to data.
);
Example of polling and checking keys:
dikeyboard->getdevicestate (sizeof), (LPVOID) &keys);

if (keys[dik_escape]&0x80)
{
      //escape key is Pressed,so do  something!
}

Reading the mouse
We need to use a dimousestate-type structure to hold the read data. Structure:
typedef struct  dimousestate{
          LONG  lx;
          LONG  ly;
          LONG  LZ;
          BYTE rgbbuttons[4];//The result of saving the mouse button press.
} Dimousestate;
Note: LX ly is a mouse move, not an absolute position, so if you want to use the mouse to position the value to draw your own mouse pointer, you must keep the old position value.

If you want to check a specific button, you can write code like this: button_1 = obj.rgbbuttons[0]&0x80;
Using define is a more convenient way to probe the state of a button: #define Button_down (Obj,button) (obj.rgbbuttons[button]&0x80)
button_1 = Button_down (mouse_state,0);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.