2016-07-17 6 views
1

私は現在ゲーム用の2dタイルマップを作成中です。それを行うために、私はタイルでいっぱいの2dArrayを作った。これらのタイルは、画像が格納されたクラスです。Graphics.DrawImage 2dtilemapパフォーマンスの問題

私はマップ上にスプライトを移動するたびに、マップは1msごとにそれ自身を再描画する必要があります(私はそれを行う理由があります)。しかし、それは遅すぎる...今は30fpsで動いているし、何をすべきかわからない。私は多くのことを試しましたが、結果は同じです。/

これは私がマップを作る方法です:ここ

 public void makeBoard() 
{ 
    for (int i = 0; i < tileArray.GetLength(0); i++) 
    { 
     for (int j = 0; j < tileArray.GetLength(1); j++) 
     { 
      tileArray[i, j] = new Tile() { xPos = j * 50, yPos = i * 50 }; 
     } 
    } 
} 

私はそれぞれ1ミリ秒を再描画、私はそれがあまりにも仕事を得ることができないので、私は本当に、このいずれかで助けを必要と

これはティムです

 private void Wereld_Paint_1(object sender, PaintEventArgs e) 
{ 
    //label1.Text = k++.ToString(); 
    using (Graphics grap = Graphics.FromImage(bmp)) 
    { 
     for (int i = 0; i < tileArray.GetLength(0); i++) 
     { 
      for (int j = 0; j < tileArray.GetLength(1); j++) 
      { 
       grap.DrawImage(tileArray[i, j].tileImage, j * 50, i * 50, 50, 50); 
      } 
     } 
     grap.DrawImage(player.movingObjectImage, player.xPos, player.yPos, 50, 50); 
     grap.DrawImage(enemyGoblin.movingObjectImage, enemyGoblin.xPos, enemyGoblin.yPos, 50, 50); 

     groundPictureBox.Image = bmp; 
     // grap.Dispose(); 

    } 
} 

:タイルとスプライト以上

 private void UpdateTimer_Tick(object sender, EventArgs e) 
{ 
    if(player.Update()==true) // true keydown event is fired 
    { 
     this.Invalidate(); 
    } 
    label1.Text = lastFrameRate.ToString(); // for fps rate show 
    CalculateFrameRate(); // for fps rate show 
} 
+0

あなたは、いくつかの最適化を試みることができます。特定の間隔でER –

+0

1.あなたのイメージのピクセルフォーマットを確認してください。描画での画像変換を避けるには、Format32bppARGBである必要があります。 2.描画コンテキストの設定を調整します。キーワードを探します。sourceOver、SourceCopy、NearestNeighbour、AntiAliased 3. Draw Image unscaledを使用します。これは描画イメージより高速です。しかし、gdiを使って1msのリフレッシュを達成できないのではないかと心配しています。 –

+0

@ThomasVoßAllrightありがとうございました。しかし、私はすでにそのすべてを試しているとスケールされていないので、それは私にとってはるかに悪くなります。だから私はWindowsフォームでこの問題に何もできないと思いますか? –

答えて

0

私のコメントに関して:

private void Wereld_Paint_1(object sender, PaintEventArgs e) 
{ 
    if(backgroundRedrawRequired) 
    { 
     Bitmap newBackground = new Bitmap(this.backGroundBitmap.Width, this.backGroundBitmap.Height, PixelFormat.Format32bppARGB); 
     using (Graphics grap2 = Graphics.FromImage(newBackground)) 
     { 
      // Draw the old background to the new image moved by the offset in X/Y 
      // MoveByNumberOfTilesX/Y can be negative depending on the direction you move the background. 
      grap2.DrawImage(this.BackgroundImage, this.moveByNumberOfTilesX * 50, this.moveByNumberOfTilesY, this.BackgroundImage.Width, this.BackgroundImage.Height); 

      // ToDo: Set the start i/j or the upper Bound of both for loops, so that only the tiles of the "white" area in the new background drawing get drawn. 
      for (int i = 0; i < tileArray.GetLength(0); i++) 
      { 
       for (int j = 0; j < tileArray.GetLength(1); j++) 
       { 
        grap2.DrawImage(tileArray[i, j].tileImage, j * 50, i * 50, 50, 50); 
       } 
      } 
     } 
     this.BackgroundImage = newBackground; 
    } 

    using (Graphics grap = Graphics.FromImage(bmp)) 
    { 
     // Reuse the background as long as it doesn't change! 
     grap.DrawImage(this.backGroundBitmap,0,0, groundPictureBox.Width, groundPictureBox.Height); 
     grap.DrawImage(player.movingObjectImage, player.xPos, player.yPos, 50, 50); 
     grap.DrawImage(enemyGoblin.movingObjectImage, enemyGoblin.xPos, enemyGoblin.yPos, 50, 50); 
    } 

    groundPictureBox.Image =bmp; 
}