Reputation: 449
I'm creating a basic level editor for a game which is tile based, so I'm looking for a good/optimal way of drawing a bunch of tiles which equal a typical screen resolution at minimum (~1080p); in a way that allows me to redraw things quickly when necessary (including an overlaying grid).
I have been trying to make it work using a PictureBox control by drawing in the OnPaint event. I thought this might be a good idea at first, until I realized how slow redrawing became when I added a simple grid drawing algorithm - it was taking a few seconds to complete redraws at high resolutions (<1080p still, though).
I can't see this solution scaling very well to allow me to create a latency free editor. Does anybody have any suggestions for a component or method of making this kind of semi-intense graphics drawing/redrawing possible?
Note: Ideally I would like to use a control that can be placed in a panel whereby the autoscroll of the panel works for panning around the control if it should be too large.
Drawing code:
public void DrawLevel(Graphics graphics, Rectangle viewport)
{
SolidBrush b = new SolidBrush(Color.DarkGray);
graphics.FillRectangle(b, 0, 0, viewport.Width, viewport.Height);
}
public void DrawGrid(Graphics graphics, Rectangle viewport)
{
Pen p = new Pen(Color.Black, 1);
for (int x = 0; x < viewport.Width; x++)
{
for (int y = 0; y < viewport.Height; y++)
{
if (x == 0 || (x % 32) == 0)
graphics.DrawLine(p, x, 0, x, viewport.Height - 1);
if (y == 0 || (y % 32) == 0)
graphics.DrawLine(p, 0, y, viewport.Width - 1, y);
}
}
}
Upvotes: 1
Views: 316
Reputation: 16981
This code is strange with O(viewport.Width * viewport.Height)
for (int x = 0; x < viewport.Width; x++)
{
for (int y = 0; y < viewport.Height; y++)
{
if (x == 0 || (x % 32) == 0)
graphics.DrawLine(p, x, 0, x, viewport.Height - 1);
if (y == 0 || (y % 32) == 0)
graphics.DrawLine(p, 0, y, viewport.Width - 1, y);
}
}
Use next code with O((viewport.Width + viewport.Height)/32)
for (int x = 0; x < viewport.Width; x+=32)
{
graphics.DrawLine(p, x, 0, x, viewport.Height - 1);
}
for (int y = 0; y < viewport.Height; y+=32)
{
graphics.DrawLine(p, 0, y, viewport.Width - 1, y);
}
Upvotes: 3