Ich mache gerade ein Kachel-basiertes Spiel in C#, aber jedes Mal, wenn ich die Kacheln zeichne, verbraucht es viel CPU, und wenn die Kacheln größer werden (wenn ich das Spiel im Vollbildmodus mache), verbraucht es noch mehr. Das ist meine Tile-Klasse:Wie man Kacheln mit Graphics.Draw effizient rendert?
public class Tiles
{
//PRIVATE :
//variabiles
private int XPosition, YPosition;
private Image Texture;
private bool Colidable;
private int SizeW = 32;
private int SizeH = 32;
private Resizer resizer = new Resizer();
//methods
//PUBLIC :
//variabiles
//methods
//CONSTRUCTOR
public Tiles(int _x,int _y,Image _i, int _sW = 32, int _sH = 32, bool _c = false)
{
XPosition = _x;//set position X
YPosition = _y;//set position Y
SizeW = _sW;
SizeH = _sH;
Texture = resizer.ResizeImage(_i, SizeW, SizeH) ;// set texture
Colidable = _c;//set if the tile is colidable,default : false
resizer = null;
}
//DRAW METHOD
//gets graphics object to draw on, adn draws at the position of the tile
public void Draw(Graphics _g)
{
_g.DrawImage(this.Texture, this.XPosition, this.YPosition);
}
//GET PRIVATE MEBERS
//returns if the tile is colidable
public bool getColidable()
{
return this.Colidable;
}
}
und das ist, wie ich die Fliesen zeichnen:
private void DrawMap(Graphics _g)
{
//CALLS THE DRAW METHOD OF EACH TILE
for (int i = 0; i < MAP_WIDTH; i++)
{
for (int j = 0; j < MAP_HEIGHT; j++)
{
Tile[i, j].Draw(_g);
}
}
}
bool TilesUpdate = false;
private void _Window_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.Black);
if (isGameRunning)
{
DrawMap(e.Graphics);
}
else
{
FullRezolutionBtn.Draw(e.Graphics);
BigRezolutionBtn.Draw(e.Graphics);
NormalRezolutionBtn.Draw(e.Graphics);
}
}
private void Update_Tick(object sender, EventArgs e)
{
Invalidate();
}
ich, dass die Karte 20 x 20 Fliesen sind erwähnen will, und es ist cosuming rund 50% der CPU, wenn es Vollbild ist.
Der Code, den Sie geschrieben haben, sieht gut aus, ich sehe nicht, wie es beschleunigt werden kann. Versuchen Sie, Smart Invaliding und Smart Drawing auszuführen (z. B. nur ungültige Teile zu zeichnen) –
Wie kann ich prüfen, ob ein Teil ungültig ist? –
'e.ClipRectangle.IntersectsWith (part.Rectangle)' –