VCUse GDIFunction library high-speed smooth animation Abstract:In the development of many game software, high-speed and smooth animation requires many deep technologies, such as OpenGL and DirectX, and developers may have a deep mathematical skills. However, if we are developing some games or implementing some animation effects on the application interface, we may not need the above technologies, we use the GDI plotting functions provided by Windows APIs or encapsulated by MFC. For this reason, can we use GDI to achieve high-speed and smooth animation? The answer is yes. This article teaches you how to use the GDI function to develop smooth and non-flashing animations and describes these usage using an application instance. Keywords:GDI, MFC, bitmap, memory device Environment I,Animation principle. Everyone knows the principle of playing a movie: 24 consecutive pictures are played at the specified time (generally 1 second). Because of the human vision, when people watch a movie, what you see is not a picture, but a rich and wonderful scene. As a result, we also use the film playing principle to implement smooth and non-flashing animation in programming. In fact, this principle has been implemented in today's animation technology, but we are discussing the technology that uses the GDI function in VC ++ to achieve the same effect. I have used this technology to develop a card game: poker mahjong ". You can download the software code 9175 at http: // www.csdn.net programmer base camp. The animation technologies used in poker Mahjong are all text introductions. From the game, you can see that, whether it is licensing, licensing, card selection, card eating, and other operations, the game screen cannot see a flash, and the speed is extremely fast. II,Implementation Method. In fact, this technology is also very simple. The key is to create a bitmap with the same size as the window area of the display animation in the memory. Use the GDI function to draw the bitmap, and then display it from the memory when appropriate. Because the bitmap has been drawn, it is not as easy as the usual programming side to draw and display, so when displaying a frame of graphics, the slowdown is less flickering, so as to achieve smooth animation; and, graphics are directly displayed in the memory to the current form, so the speed is also very fast, so as to achieve high-speed animation. The following describes how to implement these technologies: 1,Start VC ++ and create a single-document application supported by MFC. 2,Select the menu item inertnew class to create a class inherited from the cbitmap class, named cmembitmap. We have created a bitmap class to simulate a frame of a movie as an image to be displayed in the window area (movie screen. In the future, all plotting operations will be performed on this bitmap class, which can be implemented using member functions, such: displays a bitmap, a piece of text, and all the drawing functions of the GDI function. 3,After creating a bitmap class, you can use the CDC memory device environment pointer of the form to create a bitmap that is as big as the customer area of the form. Therefore, in the Bitmap header file membitmap. H, you can declare a cwnd pointer member variable m_pwnd to point to the form, as shown in the following code: PRIVATE: Cwnd * pwnd; Declare a member function to create a bitmap. The declaration code is as follows: Public: Void Init (cwnd * pwnd ); The implementation code in membitmap. cpp is as follows: //Initialize bitmap class Void cmembitmap: Init (cwnd * pwnd) { Rect RT ;//Variable of the rectangle type that saves the size of the form's customer Region Pwnd = pwnd ;//Get form pointer Pwnd-> getclientrect (& RT );//Returns the size of the customer region of the form. //Use the Form class CDCCreates a bitmap for the pointer in the memory. Createcompatiblebitmap (pwnd-> getdc (), Rt. Right;, Rt. Bottom ); } The createcompatiblebitmap function initializes the bitmap class. Its prototype is as follows: Bool Createcompatiblebitmap ( CDC * PDC, Int Nwidth, Int Nheight ); PDC is the device environment pointer. In this example, the device environment pointer of the form is used. Nwidth and nheight specify the height and width of the bitmap, in pixels. 4,Add a member function to complete the drawing. To display text information in an animation, we add a member change to display text information. The prototype code is as follows: // Membitmap. hFile Public: Void textout (int x, int y, int isize, lpcstr strtext, colorref color ); // Membitmap. cppFile Void cmembitmap: textout (int x, int y, int isize, lpcstr strtext, colorref color) { CDC * PDC = pwnd-> getdc ();//Obtain the form pointer Cfont newfont ;//Text font object Cfont * oldfont ;//Save the previous font pointer CDC dcmem ;//DC in memoryPointer to call the GDIPlotting in the in-place graph of a function Dcmem. createcompatibledc (PDC );//Create a DC of the same size as the form device Environment Dcmem. SelectObject (this );//Convert the DC in the memorySelect bitmap objects of this class Newfont. createpointfont (isize ,"");//Create a font for displaying text Oldfont = dcmem. SelectObject (& newfont );//Select new font Dcmem. settextcolor (color ); Dcmem. textout (X, Y, message );//Display text at a specified position Dcmem. SelectObject (oldfont ); //Release Newfont. deleteobject (); Dcmem. deletedc (); Pwnd-> releasedc (PDC ); } The textout function is used to display text in color at a specified position. Parameters X and Y are the positions of the displayed text. isize specifies the text font size, color specifies the text color, and strtext specifies the content to be displayed. From the code above, a memory device environment dcmem is used to display text: first, the form device environment is created, and then the bitmap class is selected. Then, you can use the drawing function of the CDC class to plot. Similarly, the reader can use the environment variable dcmem of the memory device to draw a bitmap (from a file or resource), draw a straight line, and other operations on all GDI functions, the function we add is to encapsulate these GDI functions for convenient calling. This is also the idea of object-oriented programming. 5,We then implement a bitmap clearing function to fill the bitmap with the specified color when appropriate to achieve the effect of clearing the image. The Code is as follows: // Clear a bitmap Area Void cmembitmap: clear (INT X1, int Y1, int X2, int Y2, colorref color) { CDC * PDC = m_pwnd-> getdc (); CDC dcmem ;//DC in memoryPointer Dcmem. createcompatibledc (PDC ); Dcmem. SelectObject (this ); Cbrush * oldbrush, blbrush (color ); Dcmem. setbkmode (transparent ); Dcmem. setbkcolor (color ); Oldbrush = dcmem. SelectObject (& blbrush ); Dcmem. rectangle (x1, Y1, X2, Y2 ); Dcmem. SelectObject (oldbrush ); Dcmem. deletedc (); M_pwnd-> releasedc (PDC ); } Parameters X1, Y1, X2, Y2 specify the size of the rectangle area, and color specifies the fill color. The implementation method is the same as described in section 4. You do not need to describe it here. 6,After adding the plotting function, we will introduce how to use the cmembitmap class to achieve the animation effect: First, declare a cmembitmap member variable m_membitmap in the View class (or other window classes). The Code is as follows: PRIVATE: Cmembitmap m_membitmap; Then, we reload the cview function oninitialupdate () so that the bitmap object is initialized after the view initialization ends and the view pointer is passed. The implementation code is as follows: Void ctestbitmapview: oninitialupdate () { Cview: oninitialupdate ();
// Todo: add your specialized code here and/or call the base class M_membitmap.init (this ); Settimer (1,100, null ); } At the end of the function, a timer is started. We will use timing to implement the animation function. Next, we reload the timer message function ontimer to implement the animation function. The implementation code is as follows: Void ctestbitmapview: ontimer (uint nidevent) { Int X, Y ;//Text display position Crect rect ;//Customer Region CDC * PDC = getdc ();//View DC CDC dccomp; //Obtain the size of the customer Zone Getclientrect (& rect ); //Randomly obtain the location of the text to be displayed Srand (unsigned) Time (null )); //Control text display location within Customer Zone X = rand () % rect. Width ()/2; Y = rand () % rect. Height (); //Display text in memory M_membitmap.clear (rect. Left, rect. Top, rect. Right, rect. Bottom, RGB (0, 0 )); M_membitmap.textout (10, 10, 500 ,"Fixed text ", RGB (255,255,255 )); M_membitmap.textout (X, Y, 400, "GDIFunction implementation high-speed animation demonstration ", RGB (255,255, 0 )); //In the memory device environment, select the bitmap object Dccomp. createcompatibledc (PDC ); Dccomp. SelectObject (& m_membitmap ); //Display with bit Transfer Function PDC-> bitblt (0, 0, rect. Width (), rect. Height (), & dccomp, 0, 0, srccopy ); Dccomp. deletedc (); Releasedc (PDC ); Cview: ontimer (nidevent ); } Readers can understand the meaning of the Code according to the annotations. It should be noted that when a bitmap is drawn in the memory, a static display text and a random dynamic display text are used in this example, from the running status, we can see that the dynamically displayed text moves randomly once every 0.1 seconds. Although the clear function is used for every bitmap draw, the display of static text does not flash, and the animation is very smooth, fast. How are you doing? If you create another image in the background of a thread, there will be many animations with special effects. We can use this technology on the user interface or elsewhere, you will receive unexpected animation effects. III,Conclusion For example, it is easy to use a GDI function to achieve high-speed and smooth animation. You can add bitmap, line, rectangle, and other member functions to perform various operations on the GDI drawing function. If you are still interested, you can optimize the display of the bitmap, for example: instead of displaying all bitmaps, it shows part of the animation, because bitblt function is very slow in BIT transfer. I developed the card game "Poker Mahjong" is optimized, its animation speed is very fast, if the reader is interested, please download to the programmer base camp (http://www.csdn.net) sharing software. Welcome to discuss with me VC ++ programming skills, my email: Highersoft@yeah.net. |