Discussion on code reuse of dual cache for VC Graphics Rendering

Source: Internet
Author: User

For more information, see the original article link.

Link: http://www.cnblogs.com/xianyunhe/archive/2011/11/27/2265165.html

 

The previous article discussed how to implement dual-Cache rendering on the interface. The previous article is as follows:

Http://www.cnblogs.com/xianyunhe/archive/2011/11/20/2255811.html

 

The main idea of Dual-cache is to first draw the image into the memory DC, and then copy all the images in the memory DC to the screen DC at a time. In the preceding implementation example, we encapsulate the function of drawing images directly to DC as drawdirect, and encapsulate the function of drawing through dual cache as drawwithbufferefficient, drawwithbufferefficient calls the drawdirect function. This implementation ensures that the view class can achieve dual cache rendering.

If there are other pop-up windows for dual-cache in this project, the drawwithbufferefficient can only be repeated once in the pop-up window class according to the previous implementation method, you may only need to simply copy and modify the corresponding class name. However, this repetitive work is not a pleasant task, especially when the drawwithbufferefficient function needs to be changed. If you can encapsulate functions that draw dual-Cache independently, other classes can implement dual-Cache by calling or implementing the same interface, this will effectively improve code reuse.

 

When the dual cache is not used, various windows directly write the graphic data to the screen DC. The writing method varies depending on the window graphics, that is, the content of the drawdirect function is different. After dual-cache is used, the same actions are taken to copy data from the memory DC to the screen DC, that is, the drawwithbufferefficient function can be identical. Considering the need to call different drawdirect, you can use the drawdirect pointer as an input parameter of drawwithbufferefficent to encapsulate the drawwithbufferefficent function.

 

The following describes the implementation methods:

 

1. function pointer

First, we need to understand the basic concepts and usage methods of function pointers. Because drawdirect is a member function of the class, we should also understand the usage of class member function pointers.

For more information about function pointers, see the following link:

Http://www.cnblogs.com/xianyunhe/archive/2011/11/26/2264709.html

 

2. function pointer Template

Because drawdirect may be a member function of different classes, you can only use a function template to call drawdirect of different classes.

For more information about the function pointer template, see the following link:

Http://www.cnblogs.com/xianyunhe/archive/2011/11/27/2265148.html

 

3. Reusable dual-Cache implementation

Add a class to the project to implement the interface function template of Double Cache. The input of this function includes the pointer of the window class to be drawn and the pointer of the function to be drawn, the DC pointer of the window class screen.

For ease of calling, you can set the function module as a static function, so you do not need to instantiate this class during the call.

The source code of this class is as follows:

Template <typename T>
Class cdoublebuffertemplate
{
Public:
Typedef void (T: * drawfun) (CDC *);

Static void drawwithbuffer (T * PT, drawfun fun, CDC * PDC)
{
Assert_valid (PT );
Assert_valid (PDC );

/* Create a memory DC */
CDC dcmemory;
Dcmemory. createcompatibledc (PDC );
Dcmemory. setbkcolor (PDC-> getbkcolor ());

/* Set the memory DC canvas, which is the same size as the input DC's Cropping Area */
/* Only redraw the cropping area */
Crect rectclip (0, 0, 0 );
PDC-> getclipbox (& rectclip );
Cbitmap BMP memory;
BMP memory. createcompatiblebitmap (PDC,
Rectclip. Width (), rectclip. Height ());
Dcmemory. SelectObject (& BMP memory );

/* Set the memory DC start point */
Dcmemory. setviewportorg (-1 * rectclip. Left,-1 * rectclip. Top );

/* Paint the background */
Dcmemory. fillsolidrect (& rectclip, PDC-> getbkcolor ());

(Pt-> * Fun) (& dcmemory );

/* Copy the memory DC to the input DC */
PDC-> bitblt (rectclip. Left, rectclip. Top, rectclip. Width (), rectclip. Height (),
& Dcmemory, rectclip. Left, rectclip. Top, srccopy );

/* Release resources */
BMP memory. deleteobject ();
Dcmemory. deletedc ();
}
};

 

 

The View class can call the Double Cache template function in ondraw or onpaint. The call example is as follows:

 

DoubleBufferTemplate<CDoubleBufferView>::DrawWithBuffer(
this, DrawDirect, pDC);

 

Here, this is a pointer to the View class, drawdirect is a function that draws graphics directly in DC, and PDC is a pointer to the screen DC.

 

4. Download the project source code:

Pudn download: http://www.pudn.com/downloads401/sourcecode/windows/bitmap/detail1712524.html

Csdn download: http://download.csdn.net/detail/xianyunhe1234/3849212

 

5. References

Http://www.cnblogs.com/xianyunhe/archive/2011/11/20/2255811.html

Http://www.cnblogs.com/xianyunhe/archive/2011/11/26/2264709.html

Http://www.cnblogs.com/xianyunhe/archive/2011/11/27/2265148.html


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.