Example commentary double buffering

Source: Internet
Author: User

Yesterday in the forum, someone asked about the implementation of double buffering, think of the online information is more messy, and many are DirectX-related, today specifically here to give you a brief introduction of the double-buffering technology and its implementation in the GDI + VC environment.

1. Principles of Windows drawing

We see various elements in the Windows environment, such as menus, buttons, windows, images, which are basically "painted". At this point, the screen is equivalent to a blackboard, and the various GDI elements under Windows, such as brushes, brush, etc., are equivalent to colored chalk. When we hand-draw on the blackboard, it is a stroke, and so is the computer. It's just that the speed of the computer is much faster than the manual, so it looks as if all the graphic text appears at the same time.

2. Limitations of general drawing methods

Let's call this drawing method a common drawing method. Although this approach can satisfy a considerable portion of the drawing needs, when the object to be drawn is too complex, especially if it contains bitmaps, the computer will be overwhelmed. At this time the screen will show very slow, for the movement of the picture, will give people "card" live feeling, in short a word: uncomfortable.

3. The solution: Double buffering

The principle of double buffering can be interpreted in such a way that the computer screen is viewed as a blackboard. First, we create a "virtual" blackboard in the memory environment, and then draw complex graphics on the blackboard, and when all the graphics are drawn, once again, the graphics in memory are "copied" onto the other Blackboard (screen). This method can improve the drawing speed and greatly improve the drawing effect. Here are the schematic diagrams:



Figure A double buffering principle

4. Related function Introduction

    1) .  create compatible memory for screen DC Dc:createcompatibledc ()

         if (!m_dcmemory.createcompatibledc (NULL))          // CDC m_dcmemory;
         {               
              &NBSP::P ostquitmessage (0);
         } 

    2) .    Create bitmap: CreateCompatibleBitmap ()

         m_ Bmp.createcompatiblebitmap (&m_dcmemory, Rt. Width (), Rt. Height ());         //CBitmap m_bmp;

3). Select the bitmap into the device environment: SelectObject (), which can be understood as selecting the canvas

:: SelectObject (M_dcmemory.getsafehdc (), m_bmp);

4). "Copy" The drawn graphic to the screen: BitBlt ()

Pdcview->bitblt (0, 0, Rt. Width (), Rt. Height (), &m_dcmemory, 0, 0, srccopy);

The specific usage of the function is described in MSDN. There is one sentence I repeat many times, say it again no harm: MSDN is the best teacher.


5. Here is an example of how the general drawing method and the benefits of the double buffering technique are explained by comparison of the effects.

This example in a view to draw a lot of radius gradient of the circle, you can find that the normal drawing method of the effect of the animation is poor, there is a noticeable flicker in the drawing process.




Dem and source code download:
Http://blog.vckbase.com/Files/HateMath/DBBTest.rar

ZZ from http://blog.vckbase.com/hatemath/archive/2006/02/18/17822.html

Example commentary double buffering

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.