我第一次問一個問題,這裏這麼糾正我,如果I'm做錯了。優化我的棋局C#
圖片我的棋一套: http://img842.imageshack.us/img842/2695/65744343.png
我每次都進入了一塊它滯後約1秒鐘。每件作品和瓷磚都有一張圖片,正好有96張圖片。每當我移動一塊它清除黑色的一切,然後更新圖形。
在國際象棋的早期階段,我沒有任何圖像和使用不同的顏色來代替,只有幾件,沒有明顯的滯後和部分在瞬間移動。
public void updateGraphics(PaintEventArgs e, Graphics g, Bitmap frame)
{
g = Graphics.FromImage(frame);
g.Clear(Color.Black);
colorMap(g);
g.Dispose();
e.Graphics.DrawImageUnscaled(frame, 0, 0);
}
功能顏色表(G)是這樣的:
private void colorMap(Graphics g)
{
for (int y = 0; y < SomeInts.amount; y++)
{
for (int x = 0; x < SomeInts.amount; x++)
{
//Tiles
Bundle.tile[x, y].colorBody(g, x, y);
//Pieces
player1.colorAll(g);
player2.colorAll(g);
}
}
}
的colorAll函數執行它看起來像這樣每件colorBody(G)功能:
public void colorBody(Graphics g)
{
//base.colorBody() does the following: body = new Rectangle(x * SomeInts.size + SomeInts.size/4, y * SomeInts.size + SomeInts.size/4, size, size);
base.colorBody();
if (team == 1)
{
//If its a white queen
image = Image.FromFile("textures/piece/white/queen.png");
}
if (team == 2)
{
//If its a black queen
image = Image.FromFile("textures/piece/black/queen.png");
}
g.DrawImage(image, body);
}
和finaly的功能移動所述片:
public void movePiece(MouseEventArgs e)
{
for (int y = 0; y < SomeInts.amount; y++)
{
for (int x = 0; x < SomeInts.amount; x++)
{
if (Bundle.tile[x, y].body.Contains(e.Location))
{
//Ignore this
for (int i = 0; i < queens.Count; i++)
{
Queen temp = queens.ElementAt<Queen>(i);
temp.move(x, y);
}
//Relevant
player1.move(x, y);
player2.move(x, y);
}
}
}
}
感謝您閱讀這一切!如果我的編碼示例不夠,我可以鏈接到整個程序。
您的圖像有多大?我會猜測它們非常大。 – mydogisbox 2012-07-05 14:34:25
'Image.FromFile()' - 你是否爲每一幀重新加載每個方塊的圖像?你可以加載所有的圖像一次,只是重新使用這些副本? (也就是說,重新加載它們都應該相當便宜。)這是在OnPaint()嗎?您是否重繪了所有內容或只是改變了什麼/請求重繪了什麼?你究竟如何爲這些作品下方的方塊着色 - 效率如何? – Rup 2012-07-05 14:35:19
一個很好的舊雙緩衝技術將幫助你很多,並加上@rup建議以避免每次調用FromFile。 – 2012-07-05 14:36:57