Thread: How Best To Handle 2D Graphics

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    4

    How Best To Handle 2D Graphics

    I recently finished making a basic Tetris clone and it works fine. Only problem is there is an annoying flicker every time the screen is updated and it's given me cause to revise my paint() method which draws to the screen.
    Is it perhaps purely because my paint method is too inefficient? How would others choose to output 2D graphics for simple arcade games such as Tetris or Breakout?

    Currently I have a picturebox which I've set up a graphics object within to which I draw upon. I chose to do this purely 'cause this is the only form of specific graphic output I've come across in the short year that I've been learning C#. Was this wise or are there other, better methods of drawing to the screen?

    Also I've been using nothing but filled rectangles to represent the squares of the game, would things maybe work more smoothly if I used small bitmaps or something instead?

    Here's my current paint() method:

    Code:
    public void paint()
            {
                lock (this)
                {
    
                    SolidBrush squarepen = null;
    
                    grphPicBoxGameArea.Clear(Color.White);
                    grphPicBoxNextBlock.Clear(Color.White);
    
                    //reads in the array of the pile and fills the corresponding squares of the grid
                    for (int i = 0; i < Pile.pile.GetLength(0); i++)
                    {
                        for (int j = 0; j < Pile.pile.GetLength(1); j++)
                        {
                            if (Pile.pile[i, j] != null)
                            {
                                squarepen = new SolidBrush(Pile.pile[i, j].getColour());
                                grphPicBoxGameArea.FillRectangle(squarepen, i * 30, (j * 30) - 120, 30, 30);
                            }
                        }
                    }
                    //reads in the array of the current block and fills the corresponding squares of the grid
                    Square[] currentblock = blockinuse.getBlockSquares();
                    for (int i = 0; i < currentblock.GetLength(0); i++)
                    {
                        int currentxpos = currentblock[i].getXpos(), currentypos = currentblock[i].getYpos();
                        squarepen = new SolidBrush(currentblock[i].getColour());
    
                        grphPicBoxGameArea.FillRectangle(squarepen, currentxpos * 30, (currentypos * 30) - 120, 30, 30);
                    }
    }
    }
    Thanks for any help you can give.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    In C# you are limited to what is available in .NET as opposed to what is in the Win32 API. You can do everything in C# that you can in Win32 but your approach is far different and sometimes a bit hard to find documentation for. I find the .NET docs sorely wanting and often confusing.

    If you google graphics in C# you will probably find some examples on Codeguru or some other site that will address double buffering in C#. I do not remember how to do this correctly but I have done this in C# before.

    In C++ under MFC or in Win32 you would create an offscreen DC and do all your writes to it. In the paint you would then blit the offscreen DC to the window DC. There has to be a similar path in C# although I suspect it's much easier than that. By thinking about what you would do in pure Win32 you might be able to figure out what you need to do in C#. Then it becomes how do I do A in B language which is basically a simple porting process.

    Alternatively you could try WPF or use XNA to get the job done. WPF has the benefit that it uses Direct3D/2D and as of .NET 3.5 plays nice with it as well. I know nothing about XNA but I'm sure it is well suited for your particular task.

    And last, but not least, you could write a C DLL to communicate with C/C++ Direct3D or OpenGL graphics code. If you do not like the C DLL you can make the wrapper a C++/CLI class and then call it from C# just as you would any other C# object or reference. There is information at Codeguru on creating a C++/CLI class to interface between C# and C++. The benefit of C++/CLI is that C# knows how to do the 'this' call for it and yet the C++ side of your object also knows how to talk to pure C++.
    Last edited by VirtualAce; 08-09-2009 at 11:32 AM.

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Well if your using C# anyway, you might as well look into XNA. Its a really advanced framework with lots of great features and there are no flickering issues whatsoever and I'm sure you could easily port your code over. Here is the link if your interested.
    Spidey out!

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    For automatic double buffering in a Windows Forms application, you can use this:

    Code:
    public Form1()
    {
         InitializeComponent();
         this.SetStyle(ControlStyles.AllPaintingInWmPaint
             | ControlStyles.OptimizedDoubleBuffer
             | ControlStyles.UserPaint, true);
    }
    If that doesn't seem to make any noticeable difference, you might have to call UpdateStyles() afterwards:

    Code:
    public Form1()
    {
         InitializeComponent();
         this.SetStyle(ControlStyles.AllPaintingInWmPaint
             | ControlStyles.OptimizedDoubleBuffer
             | ControlStyles.UserPaint, true);
         this.UpdateStyles();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turtle Graphics, how does it work?
    By freddyvorhees in forum C++ Programming
    Replies: 15
    Last Post: 08-28-2009, 09:57 AM
  2. Invalid Handle Error
    By smarta_982002 in forum Windows Programming
    Replies: 2
    Last Post: 03-24-2008, 10:48 AM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. Graphics Devices and Cprintf clash
    By etnies in forum C Programming
    Replies: 6
    Last Post: 05-09-2002, 11:14 AM