Jump to content
  • Advertisement
Sign in to follow this  
Darrn

[.net] OptimizedDoubleBuffering VS custom doublebuffering

This topic is 4350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi! im writing a C# application and want to use double buffering to avoid flicker. i read the example at MSDN, but what confuses me is that it seems to use triple buffering. i reduced the example to this snippet:
using System;
using System.Drawing;
using System.Windows.Forms;

public class DoubleBufferTest : Form
{
    BufferedGraphicsContext context;
    BufferedGraphics backbuffer;

    public DoubleBufferTest()
    {
        this.Resize += new EventHandler( this.OnResize);

        // NOTE: no Controlstyles.OptimizedDoubleBuffer here!
        this.Setstyle( Controlstyles.AllPaintingInWmPaint | Controlstyles.UserPaint, true );
        
        // NOTE: remove comment to make flicker stop,
        // but it seems to use triple buffering then
        //this.Setstyle( Controlstyles.OptimizedDoubleBuffer | Controlstyles.AllPaintingInWmPaint | Controlstyles.UserPaint, true );

        context = BufferedGraphicsManager.Current;
        context.MaximumBuffer = new Size( this.Width + 1, this.Height + 1);

        backbuffer = context.Allocate( this.CreateGraphics(), new Rectangle( 0, 0, this.Width, this.Height));

        DrawToBuffer( backbuffer.Graphics);
    }

    private void OnResize( object sender, EventArgs e)
    {
        if ( backbuffer != null )
        {
            backbuffer.Dispose();
        }

        context.MaximumBuffer = new Size( this.Width + 1, this.Height + 1);
        backbuffer = context.Allocate( this.CreateGraphics(), new Rectangle( 0, 0, this.Width, this.Height));

        DrawToBuffer( backbuffer.Graphics);
        this.Refresh();
    }

    private void DrawToBuffer( Graphics g)
    {
        g.Clear( Color.Black);
        g.DrawString( "Resize me to make me flicker!", new Font( "Arial", 8), Brushes.White, 10, 10);
    }

    protected override void OnPaint( PaintEventArgs e)
    {
        // NOTE: Graphic backbuffer.Graphics and Graphic e.Graphics are two different Graphics objects,
        // so double buffering seems to be already implemented
        backbuffer.Render( e.Graphics);
    }

    [STAThread]
    public static void Main( string[] args)
    {
        Application.Run( new DoubleBufferTest());
    }
}
all this stuff with "BufferedGraphics" is used to implement "custom doublebuffering". corresponding to MSDN:
Quote:
MSDN Remarks The BufferedGraphics class allows you to implement custom double buffering for your graphics. ...
but when you resize it, it still flickers... (why?) i could use "OptimizedDoubleBuffering" but what do i need "custom doublebuffering" for then? if i just override the onPaint method i could save the "custom doublebuffering" and it still doesn't flicker. if i use "custom doubebuffering" and "OptimizedDoubleBuffer" it seems to use triple buffering. 1 = backbuffer.Graphics ("custom doublebuffering") 2 = e.Graphics 3 = The "OptimizedDoubleBuffer" could please someone explain me what is wrong about my consideration!

Share this post


Link to post
Share on other sites
Advertisement
i solved it:

this.Setstyle( Controlstyles.AllPaintingInWmPaint | Controlstyles.UserPaint | Controlstyles.Opaque, true );

if Opaque is omitted the control calls the PaintBackground method which causes the flicker.

thanks to: Jeremy Kuhne's Blog

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!