[Visual c ++] Game Development Note 11 basic animation display (4) sorting texture

Source: Internet
Author: User

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

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



"Sort Textures" is a texture concept derived from the near-distance rendering of objects. Recalling our previous Paster ideas, we can first map objects that are far away from each other, and then map objects that are close to each other. Once the Paster order is determined, we can no longer change it.


However, this method is not applicable when the objects on the screen are masked. The following objects may appear instead of the uncoordinated pictures of the objects in front. To avoid this incorrect image generated by the fixed texture order, the texture sequence of each object on the screen must be dynamically re-determined when each window is re-displayed.

So how can we dynamically determine the texture sequence? We can use the sort method.



To demonstrate how sorting is used in textures, let's give an example. Assume that there are now 10 mavericks that only need to map, first put it in an array, from the perspective of 2D plane distance, the Y axis coordinate is relatively small is relatively far object. If we use the Y axis coordinates of the calf (the value to be sorted is called the key value) to sort the calf array from small to large, at last, the small steak with a small Y axis coordinate is placed in front of the array, while the Image Textures are processed by the array from small to large, in this way, you can achieve the goal of "Creating a distant object first.



Here we use bubble sort as our sorting method, because this method has the characteristics of simple program code and moderate sorting efficiency, which is a stable sorting method. The stable sorting method makes it unnecessary to consider the sorting of objects with the same Y axis coordinates.



Below we paste the code for the Bubble sorting method written in C/C ++, and sort the values of Y of each data member in the "Pop []" array as the key value, the output parameter "N" indicates the size of the array to be sorted:


Void bubsort (int n) {int I, j; bool F; pop TMP; for (I = 0; I <n-1; I ++) {f = false; for (j = 0; j <n-i-1; j ++) {If (POP [J + 1]. Y <pop [J]. y) {// exchange array elements TMP = pop [J + 1]; pop [J + 1] = pop [J]; pop [J] = TMP; F = true ;}} if (! F) // The loop break ends if there is no switching operation ;}}

Various sorting methods are the core knowledge points in C/C ++. If you are not familiar with them, you can refer to various C ++ and data structure tutorials for further study. I will not introduce it more here.



Next, we will use an example to demonstrate the use of the Bubble sorting method on the screen texture, so that the animation can show near-real near-distance effect. This example is interesting. Multiple dinosaurs run randomly. The sorting operation is completed before each screen texture, and the Paster coordinates are corrected, presents smooth and realistic animations.


I will not talk about it here. I have directly commented out the code in detail (this time the amount of code is a little large, but I have commented it out in more detail, in fact, it is better to understand than the previous Code ):

# Include "stdafx. H "# include <stdio. h> // define a struct dragon // define the dragon structure, which represents the dinosaur on the screen. The structure members X and Y are texture coordinates, DIR is the moving direction of the current dinosaur {int X, Y; int dir ;}; // defines the constant const int dranum = 12; // defines the constant dranum, represents the number of dinosaurs that the program will appear on the screen. Here it is set to 12 // The global variable defines hinstance hinst; hbitmap drapic [4], BG; // drapic [4] stores the continuous pattern of the upper and lower sides of the dinosaur. bg stores the background image hdchdc, MDC, bufdc, hwndhwnd, dwordtpre, tnow; intpicnum; dragon DRA [dranum]; // create the array DRA [] based on the dranum value to generate the dinosaur on the screen. // Global function declaration atommyregisterclass (hinstance); boolinitinstance (hinstance, INT); lresult callbackwndproc (hwnd, uint, wparam, lparam); voidpaint (HDC ); // *** the 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 >=100) 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 = (wndpro C) wndproc; 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 the initial values bool initinstance (hinstance, Int ncmdshow) {hbitmap BMP; hinst = hinstance; int I; 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); BMP = createcompatiblebitmap (HDC, 640,480); // create a blank map and put it in MDC SelectObject (MDC, BMP ); // load the running and background images of each dinosaur. The top, bottom, left, and right of the dinosaur are represented by 0, 1, and 2, and drapic [0] = (hbitmap) LoadImage (null, & quot; dra0.bmp & quot;, image_bitmap, 528,188, lr_loadfromfile); drapic [1] = (Hbitmap) LoadImage (null, "dra1.bmp", image_bitmap, 544,164, lr_loadfromfile); drapic [2] = (hbitmap) LoadImage (null, "dra2.bmp", image_bitmap, 760,198, lr_loadfromfile); drapic [3] = (hbitmap) LoadImage (null, "dra3.bmp", image_bitmap, 760,198, lr_loadfromfile); BG = (hbitmap) LoadImage (null, "bg.bmp ", image_bitmap, 640,480, lr_loadfromfile); // you can specify that the initial Paster coordinates of all dinosaurs are (200,200) and that the initial orientation is left. For (I = 0; I <dranum; I ++) {DRA [I]. dir = 3; // start direction DRA [I]. X = 200; // start X coordinate of the texture DRA [I]. y = 200; // start y coordinate of the texture} mypaint (HDC); Return true;} // void bubsort (int n) {int I, j; bool F; dragon TMP; for (I = 0; I <n-1; I ++) {f = false; For (j = 0; j <n-i-1; j ++) {If (DRA [J + 1]. Y <DRA [J]. y) {TMP = DRA [J + 1]; DRA [J + 1] = DRA [J]; DRA [J] = TMP; F = true ;}} if (! F) break ;}} ****************************** * ** // 1. sort and map the running dinosaurs in the window. // 2. dinosaur Paster coordinate correction void mypaint (HDC) {int W, H, I; If (picnum = 8) picnum = 0; // first paste the background image SelectObject (bufdc, BG); bitblt (MDC, 640,480, bufdc, srccopy); bubsort (dranum); // call bubsort () before Pasting a dinosaur Diagram () sort by function // The for loop below, according to the current direction of the dinosaur DRA [I]. dir, select the corresponding bitmap to bufdc, and set the cut size. Each dinosaur pattern to appear in the window is first transparent Paster on MDC. For (I = 0; I <dranum; I ++) {SelectObject (bufdc, drapic [DRA [I]. dir]); Switch (DRA [I]. DIR) {Case 0: W = 66; H = 94; break; Case 1: W = 68; H = 82; break; Case 2: W = 95; H = 99; break; Case 3: W = 95; H = 99; break;} bitblt (MDC, Dra [I]. x, Dra [I]. y, W, H, bufdc, picnum * w, H, srcand); bitblt (MDC, Dra [I]. x, Dra [I]. y, W, H, bufdc, picnum * w, 0, srcpaint);} // display the last image in the window bitblt (HDC, 640,480, MDC, srccopy); tpre = gettickcount (); // record the plotting time picnum ++; // The for loop below, determine the next movement direction and texture coordinates of each dinosaur for (I = 0; I <dranum; I ++) {Switch (RAND () % 4) // The random number is divided by the remainder of 4 to determine the next moving direction. The remainder 0, 1, and 2 represent the code in top, bottom, left, and right {// case 0, respectively, based on the current movement direction, the texture coordinate error caused by inconsistent pattern sizes in different directions is corrected, and the unit of each movement of the dinosaur is added (top, bottom, left, right each time 20 units) and get the next new texture coordinate case 0: // switch (DRA [I]. DIR) {Case 0: DRA [I]. y-= 20; break; Case 1: DRA [I]. X + = 2; DRA [I]. y-= 31; break; Case 2: DRA [I]. X + = 14; DRA [I]. y-= 20; break; Case 3: DRA [I]. X + = 14; DRA [I]. y-= 20; break;} // After calculating the new texture coordinate, you must determine whether the new coordinate will make the dinosaur texture exceed the window boundary. If it exceeds, then, set the coordinates in this direction to exactly equal to the critical value if (DRA [I]. Y <0) DRA [I]. y = 0; DRA [I]. dir = 0; break; // other directions calculate case 1 according to the same method as above: // switch (DRA [I]. DIR) {Case 0: DRA [I]. x-= 2; DRA [I]. Y + = 31; break; Case 1: DRA [I]. Y + = 20; break; Case 2: DRA [I]. X + = 15; DRA [I]. Y + = 29; break; Case 3: DRA [I]. X + = 15; DRA [I]. Y + = 29; break;} If (DRA [I]. y> 370) DRA [I]. y = 370; DRA [I]. dir = 1; break; Case 2: // left switch (DRA [I]. DIR) {Case 0: DRA [I]. x-= 34; break; Case 1: DRA [I]. x-= 34; DRA [I]. y-= 9; break; Case 2: DRA [I]. x-= 20; break; Case 3: DRA [I]. x-= 20; break;} If (DRA [I]. x <0) DRA [I]. X = 0; DRA [I]. dir = 2; break; Case 3: // right switch (DRA [I]. DIR) {Case 0: DRA [I]. X + = 6; break; Case 1: DRA [I]. X + = 6; DRA [I]. y-= 10; break; Case 2: DRA [I]. X + = 20; break; Case 3: DRA [I]. X + = 20; break;} If (DRA [I]. x> 535) DRA [I]. X = 535; DRA [I]. dir = 3; break ;}}} ******************************* * *** lresult callback wndproc (hwnd, uint message, wparam, lparam) {Switch (Message) {int I; Case wm_destroy: // window End message, undo various DC deletedc (MDC); deletedc (bufdc ); for (I = 0; I <4; I ++) deleteobject (drapic [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:

As we can see, because the sorting operation is performed before the texture, there is no false concealment between the dinosaurs.


We can also change the number of dinosaurs displayed on the screen by setting the dranum constant value defined in the program as per our preferences.





Note 11 ends here.


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


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



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.