2016-07-17 12 views
1

현재 게임용 타일 맵을 작성하고 있습니다. 그것을하기 위해서 나는 타일로 채워진 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 }; 
     } 
    } 
} 

내가 각이 1ms를 다시 그리기를

은 정말 내가 할 수 없기 때문에 너무 일이 하나의 도움이 필요합니다 이상 타일 및 스프라이트 :

 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

당신은 어떤 최적화를 시도 할 수 있습니다 : –

+0

1. 이미지의 PixelFormat을 확인합니다. Draw에서 그림 변환을 피하려면 Format32bppARGB 여야합니다. 2. 도면 컨텍스트 설정을 조정하십시오. 키워드 찾기 sourceOver, SourceCopy, NearestNeighbour, AntiAliased 3. 크기 조정되지 않은 그림 그리기를 사용하십시오. 이것은 drawimage보다 빠릅니다. 하지만 gdi로 1ms 새로 고침을하지 않을까 걱정됩니다. –

+0

@ 토마스 Voß Allright 고맙습니다. 그러나 나는 이미 그것의 모두를 시도하고 unscaled는 그것을 나를 위해 매우 더 나쁘게 만듭니다. 그래서 나는 창문 양식과 함께이 문제에 아무것도 할 수 없다고 생각해? –

답변

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; 
}