[Visual c ++] 10 basic animation display of Game Development Notes (3) Implementation of transparent Animation

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/7376281

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

"Transparent Animation" is a basic technique that will be used in the game. It uses the continuous display of the pattern and the transparent processing of the background of the pattern to produce a vivid animation effect on the background image.


Those who have read the previous notes should know that in note 6, we introduced the method to make the bitmap background transparent. in note 8, we explained how to use the game loop display animation, the content of this note is just a combination of the two.


If you have not read the previous notes series, you can take a look at the previous notes 6 and 8 in order to better understand the content of this section. I will provide a link below.


[Visual c ++] Game Development NOTE 6-creating transparent effects for game graphics (3)


Visual c ++: 8 Game Development notes-basic animation display (2) game loop usage



To achieve the effect of transparent animation, we use a continuous dinosaur running graph, as shown in. The width and height of each running graph are 95*99. We know that the premise of transparent animation production is that each running image must be transparent on a temporary memory DC and then pasted to the window, in this way, the image will not flash during the transparent texture process when the screen is updated.





The size of each small dinosaur in IS 95*99, which is critical in code writing.



The key point for implementing this program is, of course, writing the mypaint function.

In the mypaint function, we mainly implement two functions:

1. Transparent background of dinosaur running patterns

2. Update the coordinates of the texture to achieve the animation effect.


The following describes how to write the mypaint function:

Num = 0; // display image number x = 640; // texture start X coordinate Y = 360; // texture start y coordinate void mypaint (HDC) {If (num = 8) num = 0; // paste the background image SelectObject (bufdc, BG); bitblt (MDC, 640,480, bufdc, srccopy); // SelectObject (bufdc, DRA) for transparent processing in MDC; bitblt (MDC, X, Y, 95,99, bufdc, num * 95,99, srcand ); bitblt (MDC, X, Y, 640,480, 99, bufdc, num *, 0, srcpaint); // display the last image in the window bitblt (HDC, MDC, 0, 0, srccopy); tpre = gettickcount (); // record the drawing time num ++; X-= 20; // calculate the coordinates of the next texture if (x <=-95) x = 640 ;}

Key points:

▲7 ~ Line 17: perform transparent operations on MDC and display the final result in the window.

▲13 ~ 14 rows. When the image is transparent, use the current image number to retrieve the corresponding running pattern or shielded image.

▲22 ~ In 24 rows, calculate the coordinates of the next dinosaur map. Because our program sets the animation to run from right to left, the coordinates on the Y axis do not change, while the X axis coordinates decrease to the left by 20 at a time, the coordinates are set back to the rightmost side when the pattern runs out of the left window, so that the dinosaur continuously runs from right to left.




We also use a complete example to understand the specific process of transparent Animation:

# Include "stdafx. H "// global variable declaration hinstance hinst; hbitmap DRA, BG; hdchdc, MDC, bufdc; hwndhwnd; dwordtpre, tnow; intnum, X, Y; // 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 lp Using line, int ncmdshow) {MSG; myregisterclass (hinstance); // initialize if (! Initinstance (hinstance, ncmdshow) {return false;} // message loop getmessage (& MSG, null); // initialize MSG 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 = (wndpr OC) 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 value of each object bool initinstance (hinstance hinsta NCE, int ncmdshow) {char filename [20] = ""; hbitmap BMP; hinst = hinstance; hwnd = createwindow ("canvas", "animation demo", 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); SelectObject (MDC, BMP); DRA = (hbitmap) LoadImage (null, "dra.bmp", image_bitmap, 760,198, lr_loadfromfile); BG = (hbitmap) LoadImage (null, "bg.bmp", image_bitmap, 640,480, lr_loadfromfile); num = 0; // display the image number x = 640; // The starting X coordinate of the texture y = 360; // The starting y coordinate of the texture mypaint (HDC); Return true ;} ****************************** * ** // 1. transparent background of dinosaur running pattern // 2. update the texture coordinates to implement the animation effect void mypaint (HDC) {If (num = 8) num = 0; // paste the background image SelectObject (bufdc, BG) in MDC ); bitblt (MDC, 640,480, bufdc, srccopy); // SelectObject (bufdc, DRA) for transparent processing in MDC; bitblt (MDC, X, Y, 95,99, bufdc, num *, 99, srcand); bitblt (MDC, X, Y, 99, bufdc, num *, srcpaint ); // display the final image in the window: bitblt (HDC, 640,480, MDC, srccopy); tpre = gettickcount (); // record the drawing time num ++; X-= 20; // calculate the coordinates of the next texture if (x <=-95) x = 640 ;} ******************************* * *** lresult callback wndproc (hwnd, uint message, wparam, lparam) {Switch (Message) {Case wm_destroy: // window End message, undo various dcdeletedc (MDC); deletedc (bufdc ); deleteobject (DRA); deleteobject (BG); releasedc (hwnd, HDC); postquitmessage (0); break; default: // other messages return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}

The running result of this program is:





Note 10 is over here.


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


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.