views:

71

answers:

3

I'm writing a 2d tile-based engine. Currently, my drawing routine uses the C# Drawing library to redraw every visible tile every time the screen refreshes. I got scrolling and zooming to work, and everything looks the way I want it to. But the routine is slow. Now I'm trying to improve it. I've a couple of questions:

First, I think redrawing the tiles at every refresh is unnecessary (since they never change). Right now I'm trying to change the algorithm so that it writes the whole map to a single bitmap at initialization, and then cuts the correct portion of the bitmap when it's time to draw. Do you think this is the right way to go? (I also considered leaving the image in the background and just scrolling over it. But then I decided that I don't want to draw stuff that's outside of the field-of-view. However, perhaps that is cheaper than cutting/pasting? A memory vs time issue?)

Second, as far as I understand the C# Drawing routines do not use the full power of the GPU. I think I should try to do the drawing in OpenGL (or DirectX, but I prefer the former, since it is multiplatform). Will that help? Do you know any tiling (or general pixel-drawing) tutorial for OpenGL? A book reference could also help.

I also don't do multi-threading at the moment (in fact I only have a vague idea of what that is). Should I try to multi-thread the drawer? Or would OpenGL make multi-threading for graphics redundant?

Thanks.

+1  A: 

If you want to use OpenGL your best bet for 2d would be SDL. Using OpenGL with C# will never be that portable simply due to the fact it would use .NET wrappers.

XNA is a great tool for writing games in C#, it should provide a lot more speed and flexibility then SDL does (especially the .net port) plus more features (however more bulk).

For your cutting or scrolling question, the best route would be scrolling. Memory is much less of an issue than CPU when you're drawing using GDI+ (what System.Drawing uses). You could always split the map up into sections and scroll those then load when necessary if it's that big.

Blam
You are right! I managed to get the cutting algorithm to work, and it was even slower than the previous one. Then I got the scrolling one to work (it was easier than I thought), and it seems faster (though it screwed up zooming - now I redraw the big bitmap at zoom, which causes the action to hang-up for a second).Thanks for the SDL link, I'll look into it.
JackKane
I've heard about XNA. I tried to install it a few days ago, the install failed, and I forgot about it. Is it really worth it? Looks like another 'gamemaker'-type program to me.
JackKane
Nononono, XNA *is* more of a user friendly type of framework but that doesn't mean it's anything alike gamemaker.Check out some tutorials so you can get a feel of how it works and then decide for yourself. As I said, it should be much more faster than SDL and provides a lot more features. Plus XNA is made for C# :)
Blam
I see! Tried to install it again yesterday - realized the problem is that I'm using VS2010 and XNA3.1 is for VS2008. I think I have VS2008 installed on another machine, though. I'll look at XNA there.
JackKane
+1  A: 

What application framework are you planning to use? Techniques for efficient drawing are very different between WinForms (Win32) and WPF.

You are correct that .NET drawing routines do not take full advantage of the GPU. Using DirectX or OpenGL, one immediate optimization would be to preload all of your image tiles (or at least, all of the tiles you need for the immediate view area plus a little more) into GPU memory using image lists or display lists. You would then draw the tiles on a surface by index - draw tile N at x,y. This is usually much faster than drawing on a GPU surface using bitmaps stored in main system memory, since the bitmap pixels have to be copied to the GPU for each tile drawn and that uses up a lot of time. Drawing by index also uses a lot less GPU memory whenever you can use the same image tile in multiple places in the output.

OpenGL vs DirectX is your choice, but IMO DirectX has been evolving at a faster rate providing more hardware accelerated functions than OpenGL. OpenGL drivers on Windows also have a reputation for being neglected by hardware vendors. Their primary focus is on their DirectX drivers.

Give some thought to whether you really need OpenGL or DirectX for your 2D tile application. Requiring OpenGL or DirectX will reduce the number of machines, particularly older machines, that can run your app. OpenGL and DirectX may be overkill. You can do a lot with plain old GDI if you're smart about it.

Stay away from multithreading until you have a really good reason to go there and you have some experience with threading. Multithreading offers the reward of some performance boosts for some computing situations, but also brings with it new headaches and new performance problems. Make sure the benefit is significant before you sign up for all these new headaches.

In general, moving pixels around on the screen is usually not a good match for multithreading. You've got only one display device (in most cases) so hitting it with multiple threads trying to change pixels at the same time doesn't work well. To borrow an expression from project management: If a woman can create a baby in 9 months, can you use 9 women to create 1 baby in 1 month? ;>

Work on identifying parts of your system that do not need to access the same resources or devices. Those are better candidates for running in parallel threads than blitting tiles to the screen.

The best optimization is to discover work that does not need to be done - reducing the number of times tiles are redrawn for example, or changing to an indexed model so the tiles can be drawn from GPU memory instead of system memory.

dthorpe
I'm using Windows Forms. I didn't even know WPF existed until you mentioned it. I see it is similar to Forms, except it uses DirectX. Perhaps I'll try to port my app to WPF to see what happens.***I do load the bitmaps into main memory with GDI - I'll read up and try to load them into the GPU. Between yours and Blam's replies, I think I'll go with DirectX, unless memory loading is possible in GDI. I'll avoid multi-threading for now.***Thank you for responding so well and so quickly!
JackKane
Because WinForms apps are simply Win32 apps, they will be prompted to redraw their window area whenever it is obscured and then uncovered again. Most window paint code takes the simple approach and just redraws everything in the window. For better performance, particularly with large drawing loads, you should pay attention to the clipping rect and only redraw the portion of your window that is actually invalid. This will serve you well for scrolling, too, since scrolling up by 10 pixels only invalidates the 10 pixels across the bottom of the window, not the entire window.
dthorpe
WPF uses a completely different drawing paradigm and requires very different optimization techniques. WPF caches the drawn image so that redrawing when uncovered happens quickly and without calling your code. There is no WM_PAINT loop (that you are involved with) in a WPF app. WPF may use DirectX under the hood for its drawing, so you may have a better chance of taking advantage of GPU resources using normal WPF controls and objects as compared to WinForms.
dthorpe
"For better performance, particularly with large drawing loads, you should pay attention to the clipping rect and only redraw the portion of your window that is actually invalid." <- This is what I do. In fact, it runs reasonably fast. Just fixed two issues: before, I was rescaling all the bitmaps at every draw - now I only use DrawUnscaled; and I fixed a bug where I forgot to clear a list and over time certain sprites would be drawn hundreds of times. With these fixes, it runs fast. "You can do a lot with plain old GDI if you're smart about it." <- You're right about that.
JackKane
+1  A: 

I'm not familiar with OpenGL, but I've written a tile based engine in ManagedDX (later ported to XNA). ManagedDX is depricated, but there's the SlimDX project which is still under active development.

With DX, you can load each individual tile into a Texture. (Using Texture.FromFile() or Texture.FromStream() for example), and have a single Sprite instance draw them. This performs pretty well. I group the textures in a simple class with a Point or Vector2 for their locations, set them only when the location changes rather than every time the draw method is called. I cache tiles in memory only for the immediate screen and one or two tiles beyond, there's no need for more than that as the file IO is quick enough to fetch new tiles as it's scrolled.

struct Tile
{
    public Point Location;
    public Texture Texture;
}

Device device;
Sprite sprite;
List<Tile> tiles = new List<Tile>();

.ctor() {
    this.device = new Device(...)
    this.sprite = new Sprite(this.device);
}

void Draw() {
    this.device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);
    this.device.BeginScene();
    this.sprite.Begin(SpriteFlags.AlphaBlend);
    foreach (Tile tile in this.tiles) {
        this.sprite.Draw2D(tile.Texture, 
            new Point(0, 0), 0.0f, tile.Location, Color.White);
    }
    this.sprite.End();
    this.device.EndScene();
    this.device.Present();
}
Mark H