0

I am making a game and I have to redraw about 40 objects on each Timer tick. I have about 7 classes with different Draw(Picturebox ^ pictureBox) methods. As you see I pass pictureBox pointer for each object draw method. As the objects are so many and maybe will be a little more, the pictureBox is flickering, because it draws each object after object. Is there a simple way to fix the flickering? Maybe somehow prepare the image and then show it on the PictureBox?

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
va.
  • 848
  • 3
  • 17
  • 39

1 Answers1

1

7 classes with different Draw(Picturebox ^ pictureBox) methods

That signature is very likely to create flicker. Because in order to take advantage of the double-buffering built into PictureBox, you have to also pass a Graphics object. The one you got from the Paint event. You are probably using CreateGraphics() now, a serious flicker bug.

The proper signature is Draw(Graphics^ graphics) and used like this:

private: 
    void pictureBox1_Paint(Object^ sender, PaintEventArgs^ e) {
        for each (GameObject^ obj in gameObjects) {
            obj->Draw(e->Graphics);
        }
    }

    void timer1_Tick(Object^sender, EventArgs^ e) {
        updateGame();               // move stuff around
        pictureBox1->Invalidate();  // redraw scene
    }

With the assumption that you added the event handlers for the PictureBox and Timer control.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536