On Tue, 24 Jul 2007 14:50:05 -0700, hstagni <(E-Mail Removed)> wrote:
> Im starting to code C# and I tried to make a simple animation of a
> ball moving in the screen(i will transform it later in a pong game).
> My program worked fine, but the ball was blinking while moving. So, I
> tried to put this lines of code into form constructor, cause I saw on
> a forum that this would prevent the background to be redrawed every
> time:
> /**************TESTING...************************/
> this.SetStyle(ControlStyles.UserPaint, true);
> this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
> this.SetStyle(ControlStyles.DoubleBuffer, true);
> /********************************************************/
>
>
> My ball stopped blinking, but the animation is slower now. WHY???????
Because everything has to be drawn at least twice: once to the off-screen
buffer, and then again when that buffer is copied to the screen.
Double-buffering may or may not be faster than not double-buffering. It
really just depends on what you're drawing. The main point is that the
point of double-buffering is to improve the visual appearance of your
drawing; it's not about making things faster, and in many cases drawing
will in fact be slower.
By the way, double-buffering does _not_ prevent the background from being
redrawn every time. It just affects where the background is redrawn
(along with everything else). With DoubleBuffer set to true, the
on-screen representation of the window is never erased; the off-screen
version is, which is then copied to the on-screen version. But the
erasing still happens.
In some cases, you may be able to do a better job that the default
double-buffering implementation is doing. It depends on what exactly
you're drawing. The basic requirement to avoid flickering is to only
every touch any given pixel on the screen once per update. You get
flicker when you draw one thing to somewhere in your window, and then
immediately draw something else over what you just drew. The most common
example of this is erasing the background before drawing the contents, but
any sort of z-ordered content will cause flickering if all of the
composition is done on-screen.
So, if you can ensure that every part of your window that needs to be
drawn can be drawn exactly once, and without having to erase the
background before drawing, you can avoid flicker without
double-buffering. If not, then you probably do need double-buffering, and
the performance hit is probably unavoidable.
Pete
|