Hello,
I'm writing an application that should render simple tile-based 'world', built from System.Drawing.Bitmap tiles, sized 32x32 pixels onto a Form. Here are parts of the code:
Class is derived from Form:
class Renderer : Form {
private Device device;
private Sprite sprite;
Initialization:
PresentParameters parameters = new PresentParameters();
parameters.Windowed = true;
parameters.SwapEffect = SwapEffect.Discard;
CreateFlags createflags = CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice;
device = new Device(0, DeviceType.Hardware, this, createflags, parameters);
sprite = new Sprite(device);
Textures are created from bitmaps:
[...] = new Texture(device, bitmap, 0, Pool.Managed);
And the actual drawing:
device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);
device.BeginScene();
sprite.Begin(SpriteFlags.AlphaBlend);
for (int h = 7; h >= z; h--) {
for (int ay = 0; ay < 600 / 32 + 2; ay ++) {
for (int ax = 0; ax < 800 / 32 + 2 + 2; ax ++) {
int[] items_array = map.getData(ax, ay, h);
foreach (int it in items_array) {
Texture texture = map.getTexture(it);
sprite.Draw2D(texture, new Point(0, 0), 0, new Point(ax * 32, ay * 32), Color.White);
}
}
}
}
sprite.End();
device.EndScene();
device.Present();
Pretty self explaining, it fills the screen area with tiles, but the problem is - with all those up to 8 floors (though not all of them are always completely filled), and single tiles composed of many textures it means a lot of work - in worst cases, around 5000 calls to sprite.Draw2D to redraw the whole scene, which unfortunately takes noticable time.
I would like to ask for your advice - is it possible to achieve the effect this code does (by some modifications to it, or maybe with completly diffrent approach?), but with performance that allows for smooth animation and scrolling? How?