3

I'm creating a 2D game engine that uses a tile system. When it is drawing these tiles, you can see each one be individually drawn on the screen. It's very fast with drawing but you can still see it.

When I tell the graphics to draw something, I do not want it to update immediately because it has other things to draw on the screen and is obviously very inefficient. I want it to still "draw" what I tell it, but then actually show it all in one big update...Or would that do the exact same thing? Probably not possible anyway.

//Creating graphics
public static Form GameForm; //Assigned by the form
public static Graphics GFX;

public static void Init()
{
  GFX = GameForm.CreateGraphics();
  GameForm.BackColor = Color.Black;

  // ----

  //Drawing the tiles
  while (X != 480)
  {
    while (Y != 352)
    {
      Tiles.Add(new Tile(X, Y, Num, 0));

       GFX.DrawImage(LoadedImages[4], new Point(X, Y));
       GFX.DrawRectangle(new Pen(Color.Red), X, Y, 32, 32);
       GFX.DrawString(Num.ToString(), new Font("System", 10.0F),
                                      new SolidBrush(Color.Black),
                                      new PointF((float)X, (float)Y));

        Y = Y + 32;
        Num++;
      }
      X = X + 32;
      Y = 0;
    }
    //UPDATE NOW!
4

1 回答 1

4

创建一个与屏幕大小相同的位图。然后调用Graphics.FromImage以获取Graphics可以绘制的对象实例。将所有内容绘制到该对象中。完成后,使用 .将临时位图复制到屏幕上Graphics.DrawImage

那是:

// Create bitmap
using (var bmp = new Bitmap(form_width, form_height, GFX))
{
    using (var g = Graphics.FromImage(bmp))
    {
        // do all your drawing to g
    }

    // draw bmp to GFX
    GFX.DrawImage(bmp, 0, 0);
}
于 2011-11-22T06:43:42.393 回答