Instance explanation double buffering

Source: Internet
Author: User

This article supporting source code

Yesterday in the forum, someone asked the realization of double buffering, think of the Internet this aspect of the data is more messy, and many are related to DirectX, today is here to give you a brief introduction of double buffering technology and its implementation in the GDI + graphics environment.

1, the principle of Windows drawing

We see a variety of elements in a Windows environment, such as menus, buttons, windows, and images, which are fundamentally "painted". At this point, the screen is equivalent to a blackboard, and windows of various GDI elements, such as brushes, brushes, etc., is equivalent to colored chalk. When we draw on the blackboard by hand, it is a stroke, 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 of the graphic text appears at the same time.

2. Limitations of general drawing methods

The above drawing way we call it the ordinary drawing way. Although this way can meet a considerable part of the drawing needs, but when the object to be drawn is too complex, especially with bitmaps, the computer will be powerless. At this time the screen will show very slowly, for the motion picture, will give the person "card" to live the feeling, in short a word: unhappy.

3, the solution: double buffer

The principle of double buffering can be understood in such a way that the computer screen is considered a blackboard. First of all, we create a "virtual" blackboard in the memory environment, and then on this blackboard to draw complex graphics, and so on, and so on, once again, the plot of the memory of the graphic "copy" to another blackboard (screen). This method can improve the drawing speed and greatly improve the drawing effect. Here are the schematics:

Schematic diagram of a double buffering principle

4, related function introduction

1), create compatible memory Dc:createcompatibledc for screen DC ()if(!m_dcMemory.CreateCompatibleDC(NULL)) // CDC m_dcMemory;
{
  ::PostQuitMessage(0);
}

2), create bitmap: CreateCompatibleBitmap ()

m_Bmp.CreateCompatibleBitmap(&m_dcMemory, rt.Width(), rt.Height()); // CBitmap m_Bmp;

3, the bitmap selected into the equipment environment: SelectObject (), can be understood as the selection of canvas

::SelectObject(m_dcMemory.GetSafeHdc(), m_Bmp);

4), the drawing of the graphic "copy" to the screen: BitBlt ()

pdcView->BitBlt(0, 0, rt.Width(), rt.Height(), &m_dcMemory, 0, 0, SRCCOPY);

The specific use of the function is described in MSDN. There's one more thing I repeat, it's OK to say it again: MSDN is the best teacher.

5. In this paper, an example is given to illustrate the limitations of common drawing methods and the benefits of double buffering technology by means of comparison of effects.

This example draws a lot of radius gradient circles on a view, and you can pay attention to the effects of animations in two different drawing modes:

For specific implementation please refer to the source code example.

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.