[Stick to the top] Game Development Technology Summary (Classic) the third set allows images to work-quickly switch graphics to achieve Animation

Source: Internet
Author: User
Tags bmp image textout
Game Development Technology Summary (Classic) the third set allows images to be moved ---- Quickly switching graphics to achieve Animation

Reprinted please indicate the source

Author: Sun Guangdong personal homepage: http://blog.csdn.net/u010019717

For more highlights, see:Http://passport.baidu.com/business&un=a1224708372&fr=prin#7

 

 

 

3-1 Task

Here we will use the VC clock message function to display the transformed image on the screen, thus forming an animated
Effect.

3-2 create a clock message

In the VC programming environment, select "classwizard" under the menu view to go To the MFC class wizard (
Classwizard ).

Figure 3-1
In the Class Wizard, select wm_timer and double-click it. In the member function bar (member functions), you can see that
The on_wm_timer function. Then press edit code to enter the clock message function.
Ontimer () is in progress.

Void cmydlg: ontimer (uint nidevent) // clock function, [Definition generation in Class Wizard] {cdialog: ontimer (nidevent );}

In the program, we only need to execute the command settimer (1,150, null), every 150 milliseconds during the program running,
The program in the ontimer () function of the clock message will be executed once.
Settimer parameter description:
Settimer (1,150, null );
Set a timer (1st timers, interval (MS), and null values );

 

3-3 get the role up

We write a program in the clock message to display the program, and the graph can be changed quickly. Of course you want
The imported graphics are changed. To change the imported graphics, you only need to change the imported graphics file name.

3-3-1 variable file name

If the graphic file is stored in the game directory on drive C and the file name is paib0p.bmp
The file name format should be "C:/GAME/b0p.bmp ". Where p changes sequentially.

Figure 3-2
In the C ++ language, we can use sprintf (CC, "C:/GAME/bw.02d.bmp", P); To set the image file name. CC
It is a string variable, and springf is a character formatting function. It can convert values of other data types to strings.
Note: % 02d is written in quotation marks. "%" indicates that the number of converted P is the integer number, where "2" indicates
It indicates that the converted string is two digits, and "0" indicates that when there are less than two digits, the prefix is "0.
For example, P = 2, after executing sprintf (CC, "C:/GAME/bw.02d.bmp", P.
The CC value is "C:/GAME/b02.bmp ".
For example, P = 12, after executing sprintf (CC, "C:/GAME/bw.02d.bmp", P.
The CC value is "C:/GAME/b12.bmp ".
For ease of use, we will dynamically obtain and write the called graphic file name as a function.
Getpic (response ).

3-3-2 getpic (callback) calls the image to the corresponding bitmap

//************************************** * *********** // Getpic (cstring CC, int p) Call the image to the corresponding bitmap // P to get the image file name to be called. // Import the image to the corresponding bitmap in the specified directory //************************** * ********************** bool getpic (cstring CC, int p) // call the image to the related bitmap [Chapter 2] {char name [256]; setcurrentdirectory (appdir); // set the current directory sprintf (name, "% S/c000005d.bmp", Dir, CC, P); // generate the file name loadbmp (name) for calling the image; // call the BMP image return true ;}

This function generates the graphic text
Component name.
Example: Dir = "Graph/", Cc = "person", P = 15;
Then: Name = "image/person/00015.bmp ";
Next, we will call the loadbmp ("image/person/00015.bmp") image to the bit.
Now, we can write the code for calling the image file and displaying the image into the clock message ontimer ()
(The row number in the program is to facilitate annotation ).

3-3-3.

Set: P = 0; m0 = 0; M1 = 400; Dir = "C:/GAME /";
Now, the program in the ontimer () Clock message function is executed every 150 milliseconds.

Void cmydlg: ontimer (uint nidevent) // clock function, [Class Wizard definition generation] 1 {cclientdc DC (this ); // customer zone device environment 2 if (getpic ("person", P) = false) // The dimmed color picture 3 {afxmessagebox (CC + "not found! "); Return;} 4 SelectObject (memdc, bit); // device-related bitmap associated with the temporary storage device scenario 5 bitblt (DC. m_hdc, 200,160, W, H, memdc, srccopy); // display game role 6 p ++; // next action 7 if (P> m1) P = M0; // if the action is completed, it must be repeated. Cdialog: ontimer (nidevent );}

When the first execution, P = 0;
Line 3: transfer "C:/GAME/person/c00000.bmp" to bit.
Line 3: Call the directory bit and associate it with memdc.
Line 3: copy the image in memdc to the current display area and display it on the screen.
Rows 1, P plus 1, are changed to 1.
For the second execution, P = 1;
The program displays the content of the "c00001.bmp" image on the screen.
P = n-1;
The program displays the content of the "c0000p.bmp" image under the directory c:/GAME/person/on the screen.
Determine whether P reaches the M1 (= 7th) value in Row 3 each time. If P reaches the value, P = m0 (= 0); start from scratch.
So that 400 different images are displayed repeatedly on the screen, a living character will appear.

3-3-4 onok () Start the clock

Now we have another thing: the Startup Program that writes the clock message function in onok.

Void cmydlg: onok () // OK key, [Define generation in Class Wizard] {getdlgitem (idc_edit1) → showwindow (sw_hide); // hide the text box cclientdc (this ); // getwindowrect (rect) in the device environment in the customer zone; // obtain the current window size bitblt (DC. m_hdc, 0, 0, rect. width (), rect. height (), memdc, 1,150, srccopy); // copy the background to the current screen // start the clock settimer (150, null); // set the timer to milliseconds}

After the program is running, just press "OK", the program first copies the background to the current screen, and then starts
When the clock is triggered, the active person appears on the screen.

3-4 basic operations on Windows and controls

In the interface design of the program, we often need to control the size and position of the window and various controls.
VC ++ provides the following basic operation commands for Windows and various controls:
Getdlgitem (idc_edit1) → enablewindow (false); // The control is invalid and valid. True
Getdlgitem (idc_list1) → showwindow (sw_hide); // hide the control and display sw_show
Getdlgitem (idc_edit1) → setwindowtext ("cc"); // The CC string is displayed on the control.
Getdlgitem (idcancel) → movewindow (X, Y, W, H, true); // control size, positioning
Setdlgitemtext (idc_static0, "cc"); // The CC string is displayed on the control.
Getdlgitemtext (idc_static0, "cc"); // retrieves the control text to the string CC
Movewindow (X, Y, W, H); // locate the current window
Centerwindow (); // center the current window
For example, setdlgitemtext (idc_edit1, CC) displays the string CC in the "idc_edit1" edit box.
For example: movewindow (640,480,); current window size, positioning.
For example, centerwindow (); the current window is centered.
Example: getdlgitem (idok) → movewindow (580,0, 55,18, true)
Determine the size (W = 55, H = 18) and positioning (x = 580, y = 0) of the "idok" button control ).
In order to give readers a complete understanding of the program, we will give all the programs and comments in this chapter.
The program we compiled in this chapter is only in the file "let me move DLG. cpp.

3-5 "let me move DLG. cpp" complete program and comments 3-5-1 global definition

Note: The gray part is automatically generated by MFC.

# Include "stdafx. H "# include. H "# include" let me move DLG. H "# ifdef _ debug # define new debug_new # UNDEF this_filestatic char this_file [] = _ file __; # endif ////////////////////////////////////// /////// global variable definition //////////////////////////// /////////////// hbitmap bit; // device-related bitmap HDC memdc; // role device scenario int W, H; // image size cstring dir; // define the PATH variable cstring cc; // public variable char appdir [256]; // the current directory crect rect; // defines the window size variable int JS; // role [0 male, 1 female] int FW; // orientation [0 South 1 southwest 2 West 3 Northwest 4 north 5 northeast 6 East 7 southeast] int M0; // animation Initial Value int M1; // animation final value int P; // current graphic serial number /////////////////////////////////// /// // Function Definition /////////////////////////// ////////////// bool getpic (cstring CC, int P); // call the image to the relevant bitmap bool loadbmp (cstring CC); // call the BMP Image

 

 

3-5-2 initial entry of the program

 

Cmydlg: cmydlg (cwnd * pparent/* = NULL */) // [automatically generated by MFC]: cdialog (cmydlg: IDD, pparent) {commandid} void cmydlg :: dodataexchange (cdataexchange * PDX) // [automatically generated by MFC] {⋯} begin_message_map (cmydlg, cdialog) // [automatically generated by MFC] ⋯ end_message_map () bool cmydlg :: oninitdialog () // The initial entry of the dialog box program, [automatically generated by MFC] {cdialog: oninitdialog (); seticon (m_hicon, true); // set big iconseticon (m_hicon, false); // set small icon // todo: add extra initializati On here //. display description cstring cc; CC = "\ r \ n this is the second example of" Learning VC game ": \ r \ n "; CC + = "in this chapter, we use the following knowledge, technology \ r \ n"; CC + = "1. the basic knowledge and implementation methods of computer animation \ r \ n "; CC ++ =" 2. create a clock message in VC ++ and use the clock message to generate an animation. \ R \ n "; CC + =" 3. describes the usage of the Formatting Function springf. The image file name is obtained dynamically. \ R \ n "; CC + =" 4. describes how to control and locate the window and Widget size in VC ++. \ R \ n "; setdlgitemtext (idc_edit1, CC); // B. movewindow (640,480, 640); // centerwindow (); // center window getdlgitem (idok) → movewindow (-60, 0, 55, 18, true ); // determine the button control position // C. create a graphical environment memdc = createcompatibledc (0); // create a device scenario // D. set the main data JS = 0; // The role, the character fw = 0; // The position, 0 South m0 = js * 400 + FW * 4; // initial value, position M1 = (JS + 1) * 400-1; // final value; position P = M0 at the beginning of the first character; // current image no. // E. set the path to getcurrentdirectory (256, appdir); // obtain the current directory dir = appdir; If (dir. right (8) = "running program") dir = "Image/"; else dir = ".. /run the program/image/"; // image path // F. call to display the background loadbmp (DIR + "ground. BMP "); // adjust the background image SelectObject (memdc, bit); // call the bitmap to associate it with the map device scenario // G. display the text settextcolor (memdc, RGB (255,255,255) on the background; // set the color of the map device scenario setbkmode (memdc, transparent ); // The word is transparent cc = "! I can do it !! Walking and running are wonderful in the world !!! "; // Set the text content textout (memdc, 150,100, CC, lstrlen (CC); // display the text settextcolor (memdc, RGB (255,255,255) in memdc )); // set the color of the map device scene. Cc = "No, get a box to cage me up? Let me go !!! "; // Textout (memdc, 150,220, CC, lstrlen (CC); // display the text cc =" BMP image itself is a rectangle, and the background color of the image is white. "; Textout (memdc, 151,250, CC, lstrlen (CC); // display the text return true in memdc ;}

 

 

 

3-5-3 onok () OK key

Void cmydlg: onpaint () // [automatically generated by MFC] {automatically generated} hcursor cmydlg: onquerydragicon () // [automatically generated by MFC] {automatically generated} void cmydlg :: onok () // OK key, [Define generation in Class Wizard] {getdlgitem (idc_edit1) → showwindow (sw_hide); // hide the text box cclientdc (this ); // getwindowrect (rect) in the device environment in the customer zone; // obtain the current window size bitblt (DC. m_hdc, 0, 0, rect. width (), rect. height (), memdc, 1,150, srccopy); // start the clock settimer (150, null); // set the timer to milliseconds}

3-5-4 oncancel () Exit the program

Void cmydlg: oncancel () // exit, [Define generation in the Class Wizard] {deletedc (memdc); // Delete eobject (BIT) in the scenario of temporary storage devices ); // Delete the bitmap cdialog: oncancel ();}

 

 

3-5-5 clock functions

Void cmydlg: ontimer (uint nidevent) // clock function, [Define generation in Class Wizard] {cclientdc DC (this ); // If (getpic ("person", P) = false) in the customer zone device environment // The Color Picture {afxmessagebox (CC + "not found! "); Return;} SelectObject (memdc, bit); // The bitblt (DC. m_hdc, 200,160, W, H, memdc, srccopy); // displays the game role P ++; // The next action if (P> m1) P = M0; // if the action is completed, it must be repeated. Cdialog: ontimer (nidevent );}

3-5-6 adjust the image to the corresponding bitmap

//************************************** * *********** // Getpic (cstring CC, int p) Call the image to the corresponding bitmap // P to get the image file name to be called. // Import the image to the corresponding bitmap in the specified directory //************************** * ********************** bool getpic (cstring CC, int p) // call the image to the corresponding bitmap {char name [256]; setcurrentdirectory (appdir); // set the current directory sprintf (name, "% S/c12705d.bmp ", dir, CC, P); // generate the file name loadbmp (name) for calling the image; // call the BMP image return true ;}

3-5-7 tune BMP Image

//************************************** * *********** // Loadbmp (cstring CC) // tune the BMP image // tune the image specified by CC; the obtained image is stored in the bit of the device-related bitmap. // the width and height of the image are stored in the global variable W, in H //************************************ * ************* bool loadbmp (cstring CC) // call the BMP image {deleteobject (BIT); // Delete the last bitmap memory. Bit = (hbitmap) LoadImage // call the image specified by CC (AfxGetInstanceHandle (), // CC, // file name image_bitmap, // bitmap method 0, // image width 0, // Image Height lr_loadfromfile | lr_createdibsection // method); If (bit = NULL) return false; // dibsection ds for image call failure; // bitmapinfoheader & Bm = Ds. dsbmih; // GetObject (bit, sizeof (DS), & Ds); // retrieves bitmap information → bminfow = BM. biwidth; // obtain the bitmap width value H = BM. biheight; // obtain the bitmap height value return true ;}

 

3-6 directory for running the program

Note: In oninitdialog (), set the function of a program in the path section (the row number is added to the description ).

// E. set Path 1 getcurrentdirectory (256, appdir); // obtain the current directory 2 dir = appdir; 3 if (dir. right (8) = "running program") 4 dir = "image/"; // image Path 5 else dir = ".. /run the program/image/"; // image path

Row 3 is a Windows API function call that retrieves the character array from the directory where the current program is running.
In the appdir variable.
Row 3: Assign the current directory to the string variable dir. Because string variables support what we will use below
Some flexible operations.
Line 3. If the eight characters (four Chinese characters) behind the Dir are "running programs ",
Then the 4th rows dir = "image /";
Otherwise, line 2 dir = "../run the program/image /";
3rd ~ The significance of the Five-line algorithm is that the running program of our entire teaching example is focused on
In the same directory as the source program, all animation images are under the "running program" directory.
Directory "image", that is, under the "running program/image/" directory at the same level as the source program. (See Figure 3-3)


Figure 3-3
When the program is directly compiled and run in VC, the current directory is the sample source directory (for example, "02. Let me
This directory ). It is determined by row 3 that it is not the "running program" directory, so the directory for retrieving images should be
Dir = "../run the program/picture/", that is, "run the program/picture/" under the upper-level directory /".
If you execute program under "running program", let me use .exe ", the current directory is" running program ",
The directory for retrieving images should be dir = "image /". That is, the "image/" under the current directory /".
The purpose of this operation is to compile and run the program directly in VC or under the "running program" directory.
The running program can accurately retrieve the directory where the image is located.
Now you can compile and run this program.

3-7 Flowchart

The program process is divided into three parts: initialization when the program starts to execute, and parameters and startup clock letter are set after the buttons
Number, which consists of the clock function and the graphic function. Note: We will further edit the game.
The main process is carried out in this main loop.

Figure 3-4 main Flowchart
For specific programs, see the instance program in this chapter: "Let me use it ".

3-8 Summary

In this chapter, we learned the following knowledge and methods.
1. Introduce the basic knowledge and implementation methods of computer animation
2. Create a clock message in VC ++ and use the clock message to generate an animation.
3. describes the usage of the formatting function sprintf. The image file name is obtained dynamically.
4. This article introduces how to control and locate the window and Widget size in VC ++.
5. Directory locating method when the program is running.

 

 

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.