Why should I do manual double buffering?

I am working on a game in C # (either 2.0 or 3.5 havn't decided yet). The game will play on a map with a hexagonal grid. I appreciate that the user interface for this map should use double buffering (many layers, so slow drawing). I know that I can enable double buffering via style or create my own buffer and process it myself. Most of the recommendations that I find on the Internet are to handle this myself. I wonder why? Obviously, this allows me to avoid the assumptions inherent in double control buffer processing, but I do not know what these assumptions are.

Again, I am not looking for code to explain how to double the buffer of my control, but why would I like to build it myself, instead of using the double buffering style and letting the CLR / Control class handle it?

+7
design c # winforms gdi + doublebuffered
source share
2 answers

In WFA, double buffering slows performance without completely eliminating flickering in custom graphics areas. For built-in GUI elements, for example, if you create a game created from ImageButtons and Labels, the double-buffered built-in mode perfectly hides the redrawing of the control tree. However, there are several serious issues with using it for a custom drawing area:

  • The drawing buffer created when you set up the application to create double buffering is used to draw the entire window and all child controls, not just your custom drawing area, so you add the overhead of redrawing each GUI element to the back buffer before dragging the page.
  • If something invalidates the control, the Paint method is called. You cannot finish drawing when this happens, and you will get an incomplete image shown to the user (not very good in real time).

By preserving the GUI base window with one buffer, but creating an area in which you control buffering, both of these problems are minimized.

Double buffering methods can be as simple as creating a Bitmap object as the back buffer and drawing it in the drawing area when you are good and ready, or creating a separate BufferedGraphicsContext to control the buffering of your custom drawing area.

+3
source share

From MSDN :

For graphically intensive applications such as animation, you can sometimes improve performance by using the dedicated BufferedGraphicsContext instead of the BufferedGraphicsContext provided by the BufferedGraphicsManager. This allows you to create and manage graphics buffers individually, without the overhead of managing all the other buffered graphics associated with your application, although the memory consumed by the application will be larger.

EDIT : I also found this article from Bob Powell, which may be helpful

Manual double buffering can be useful if you do not want the assumption system for you, such as: the background is opaque or transparent, or perhaps if you want to create a more complex buffering system. There are a few simple rules that you need to follow in order to get a dual buffer right guide.

First, do not create a new back buffer every draw cycle. Only create or destroy a bitmap when the window is resized by the client. Secondly, just create a raster image of the desired size. Cleaning pixels takes time, so if there are more pixels than you need, you just lose processor cycles. Finally, use the simplest drawing method to copy a bitmap onto the screen. DrawImageUnscaled is the way to go here.

EDIT . Another reason is that you might want the application to control buffering rather than the controls themselves.

Source: Pro.NET 2.0 Windows Forms and C # User Controls

+4
source share

All Articles