[Visual c ++] Game Development Note 12 game input message processing (I) Keyboard Message Processing

Source: Internet
Author: User
Tags mail exchange

This series of articles was written by zhmxy555. Please indicate the source for reprinting. Http://blog.csdn.net/zhmxy555/article/details/7390624

Author: Mao yunxing mailbox: happylifemxy@qq.com welcome mail exchange programming experience



I believe everyone is familiar with the man-machine interaction method of "Legend of the legend. In a time when there was not enough material, a low-configuration computer, a game named "Legend of the legend of the fairy sword", was able to hold a generation of people to pursue their dreams. Although in the past decade, a variety of trendy games have emerged, but the legend of "Legend of the legend, it remains in the hearts of our generation forever. It is the most beautiful dream that can never be replaced.



Starting from this note, we will begin to explain how to process game input messages, start human-computer interaction, and start game development in the true sense.

This section describes how to process keyboard messages.


As a basic output device, the keyboard plays a crucial role in every excellent game R & D (of course we will not discuss iOS and Android platforms here for the time being ).

First, we will give a brief introduction to the basic concepts of the keyboard in windows and the handling methods of keyboard messages.

1. Virtual key code

Keys on all keyboards are defined as a set of general "virtual keys". That is to say, all keys in windows are considered as virtual keys (including mouse keys ), each virtual key has a virtual key code.


2. Keyboard Message

Windows is a message-driven environment. Once you input messages on the keyboard, The system receives the corresponding keyboard messages. The following lists the three most common keyboard messages:

Message for pressing wm_keydown

Wm_keyup release button message

Wm_char message

When a key is pressed, The wm_keydown and wm_keyup messages are sent as virtual key codes. When the program receives these messages. The information of the virtual key code is used to determine which key is pressed.

In addition, wm_char is sent when the pressed button is a printable character defined in ASC ⅱ.


3. System key

Windows systems define a set of "system Keys". These keys are usually a combination of [alt] and other keys. The system Keys have specific functions for Windows systems, in Windows, the following message is also specified for system Keys.

Wm_syskeydown press system key message

Wm_syskeyup Panasonic system key message

"Sys" is added to the message code to indicate that the system key presses the message. However, in fact, the program rarely processes the system key message, because when such a message occurs, Windows will process it and perform relevant work.

These are some basic concepts about keyboard definition and output processing in windows.




Next we will explain in detail the main character of this note-Keyboard Message Processing.

Keyboard messages are also added to the message processing function to define the processing. pressing a button event will surely be followed by an event that releases the button, therefore, wm_keydown and wm_keyup messages must be paired. However, wm_keydown messages are usually processed only in the program, while wm_keyup messages are ignored.

Observe the two parameters wparam and lparam entered in the message processing function:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 

When a Keyboard Message is triggered, the value of wparam is the virtual key code for pressing the key. The virtual key code defined in Windows starts with "VK _", and lparam stores the status information of the key, therefore, if the program processes users' keyboard input operations, the content of the message processing function can be defined as follows:


Lresult callback wndproc (hwnd, uint message, wparam, lparam) {Switch (Message) {Case wm_keydown: // press the keyboard message switch (wparam) {Case vk_escape: // press the [ESC] key // define the message processing program break; Case vk_up: // press the [strong] key // define the message processing program break; Case wm_destroy: // window End message postquitmessage (0); break; default: // other messages return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}


The key description of the Keyboard Message Processing Program in this message processing function is as follows:

<1> Row 3: defines the processing of the "wm_keydown" message.

<2> Row 3: Use the "Switch" statement to determine the value of "wparam" to determine which button is pressed and run the key message processing program in the corresponding "case.



Similarly, we use an instance to familiarize everyone with and practice the knowledge in this section.

This example allows players to enter the [strong] [strong] [strong] [→] key to control the movement of Characters in the screen, the following uses a continuous pattern of a person walking in four different directions.








I will not talk much about it. I will directly comment out the code in detail:

# Include "stdafx. H "# include <stdio. h> // global variable declaration hinstance hinst; hbitmap girl [4], BG; hdchdc, MDC, bufdc; hwndhwnd; dwordtpre, tnow; intnum, Dir, X, Y; // The X and Y variables are the texture coordinates of the characters, and the Dir is the movement direction of the characters. Here, we use 0, 1, 2, and 3 to represent the top, bottom, left, and right of the characters: num is the cell number in the continuous texture. // The global function declaration atommyregisterclass (hinstance); boolinitinstance (hinstance, INT); lresult callbackwndproc (hwnd, uint, wparam, lparam ); voidmypaint (HDC); // *** winmain function, program entry point function *************** * ****** Int apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {MSG; myregisterclass (hinstance); // initialize if (! Initinstance (hinstance, ncmdshow) {return false;} getmessage (& MSG, null); // initialize MSG // message loop while (msg. message! = Wm_quit) {If (peekmessage (& MSG, null, 0,0, pm_remove) {translatemessage (& MSG); dispatchmessage (& MSG) ;} else {tnow = gettickcount (); if (tnow-tpre> = 40) mypaint (HDC) ;}} return MSG. wparam;} // ***** design a window class, which is similar to a blank question, use the window struct ******************* atom myregisterclass (hinstance) {wndclassex wcex; wcex. cbsize = sizeof (wndclassex); wcex. style = cs_hredraw | cs_vredraw; wcex. lpfnwndproc = (wndproc) wndpr OC; wcex. cbclsextra = 0; wcex. cbwndextra = 0; wcex. hinstance = hinstance; wcex. hicon = NULL; wcex. hcursor = NULL; wcex. hcursor = loadcursor (null, idc_arrow); wcex. hbrbackground = (hbrush) (color_window + 1); wcex. lpszmenuname = NULL; wcex. lpszclassname = "canvas"; wcex. hiconsm = NULL; return registerclassex (& wcex );} ******************************** ***** // load the bitmap and set various initial values bool initinstance (hinstance, int n Cmdshow) {hbitmap BMP; hinst = hinstance; hwnd = createwindow ("canvas", "Drawing window", ws_overlappedwindow, cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null); If (! Hwnd) {return false;} movewindow (hwnd, 10, 10, 640,480, true); showwindow (hwnd, ncmdshow); updatewindow (hwnd); HDC = getdc (hwnd ); MDC = createcompatibledc (HDC); bufdc = createcompatibledc (HDC); // create an empty bitmap and place it in MDC. BMP = createcompatiblebitmap (HDC, 640,480); SelectObject (MDC, BMP ); // set the initial position and movement direction of the Character Map x = 300; y = 250; Dir = 0; num = 0; // load the continuous moving bitmap and background image girl [0] = (hbitmap) LoadImage (null, "girl0.bmp", image_bitmap, 440,148, lr_loadfromfile); Girl [1] = (hbitmap) loadImage (null, "girl1.bmp", image_bitmap, 424,154, lr_loadfromfile); Girl [2] = (hbitmap) LoadImage (null, "girl2.bmp", image_bitmap, 480,148, lr_loadfromfile ); girl [3] = (hbitmap) LoadImage (null, "girl3.bmp", image_bitmap, 480,148, lr_loadfromfile); BG = (hbitmap) LoadImage (null, "bg.bmp", image_bitmap, 640,480, lr_loadfromfile); mypaint (HDC); Return true ;} ****************************** * ** // coordinate correction of character textures and void mypaint (HDC) of Window Textures) {int W, H; // first paste the background image SelectObject (bufdc, BG) in MDC; bitblt (MDC, 640,480, bufdc, srccopy ); // extract the consecutive walk chart of the corresponding character in the current direction, and determine the width and height of the screenshot. SelectObject (bufdc, girl [dir]); Switch (DIR) {Case 0: W = 55; H = 74; break; Case 1: W = 53; H = 77; break; Case 2: W = 60; H = 74; break; case 3: W = 60; H = 74; break;} // perform transparent texture based on the current x and y values on MDC, and then display bitblt (MDC, x, Y, W, H, bufdc, num * w, H, srcand); bitblt (MDC, X, Y, W, H, bufdc, num * w, 0, srcpaint); bitblt (HDC, 640,480, MDC, srccopy); tpre = gettickcount (); // record the drawing time num ++; if (num = 8) num = 0 ;} ******************************* * *** // 1. press ESC to end the program // 2. press the arrow key to reset the texture coordinate lresult callback wndproc (hwnd, uint message, wparam, lparam) {Switch (Message) {Case wm_keydown: // press the Keyboard Message // The virtual key code switch (wparam) {Case vk_escape: // press the [ESC] key postquitmessage (0); // terminate the program break; case vk_up: // press the [rotate] key // first adjust the texture coordinates based on the current movement direction, add the number of characters to move up (10 units are moved by pressing the button each time) to determine the x and y values of the character texture coordinates, and then determine whether the coordinates exceed the window area, if yes, modify switch (DIR) {Case 0: Y-= 10; break; Case 1: X-= 1; y-= 8; break; Case 2: X + = 2; y-= 10; break; Case 3: x + = 2; y-= 10; break;} If (Y <0) y = 0; dir = 0; break; Case vk_down: // press the [Keys] key switch (DIR) {Case 0: x + = 1; y + = 8; break; Case 1: Y + = 10; break; Case 2: x + = 3; y + = 6; break; Case 3: x + = 3; y + = 6; break ;} if (Y> 375) y = 375; Dir = 1; break; Case vk_left: // press switch (DIR) {Case 0: X-= 12; break; Case 1: X-= 13; y + = 4; break; Case 2: X-= 10; break; Case 3: X-= 10; break ;} if (x <0) x = 0; Dir = 2; break; Case vk_right: // press [→] key switch (DIR) {Case 0: x + = 8; break; Case 1: x + = 7; y + = 4; break; Case 2: x + = 10; break; Case 3: x + = 10; break ;} if (x> 575) x = 575; Dir = 3; break;} break; Case wm_destroy: // window End message int I; deletedc (MDC); deletedc (bufdc ); for (I = 0; I <4; I ++) deleteobject (GIRL [I]); deleteobject (BG); releasedc (hwnd, HDC); postquitmessage (0 ); break; default: // other messages return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}

The program running result is as follows. We can use the keyboard to move the villain up, down, left, and right, and exit with ESC:

In this way, a simple game is complete.

We can also obtain the key-virtual key code in the message processing function to easily process the keyboard input operation.





Note 12 ends here.


For the source code of this section, click here to download: [visual c ++] code_note_12


I would like to thank my friends who have been supporting the [visual c ++] Game Development notes series and ask everyone to continue to follow my blog. When I have time, I will learn my experience, I think it is better to write out the knowledge points and share them with you.

It is still a long journey to be proficient in Game Development. I hope to communicate with you and learn and make progress together.

If you have read this article, you can refer to it to give more friends a chance to see it. I also hope you can leave a message to discuss programming-related issues. Finally, thank you for your support ~~~


The end


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.