Sign in to follow this  

D'Draw(C#) does flipping another classes surface cause a performance hit ?

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

On my tile engine i have tried to make it as modular as possibale so i can switch the DirectDraw out for Direct3D or OpenGL for future projects or take the logic part of the engine and just port it to another language easily. At the moment i have 4 classes the 1)main body of the program (initialises and launches everything) 2)tileEngine (Holds all the tile based info and updates the current position within the level) 3)Graphics(sets up DirectDraw, the device and surfaces, etc) 4)Input(Handles user input) if i pull data from the tileEngine(tile coords, level boudries, etc)to the main class and use them to generate the rectangles that i use when drawing then still in the main class i draw to Graphics() backBuffer and then flip it primary. like so (te. is an instance of tileEngine and gr. is an instance of Graphics):
for(int i = 0; i<te.TilesInHeight; i++)
			{
				for (int j = 0; j<te.TilesInWidth; j++)
				{
					gr.backBuffer.Draw
						(
						new Rectangle
						(
						te.TileTopLeftX+(j*te.TileSize),
						te.TileTopLeftY+(i*te.TileSize),
						te.TileSize,
						te.TileSize
						),
						gr.sprite,
						new Rectangle
						(
						te.Level[te.ScreenStartX+i,te.ScreenStartY+j].X*te.TileSize,
						te.Level[te.ScreenStartX+i,te.ScreenStartY+j].Y*te.TileSize,
						te.TileSize,
						te.TileSize
						),
						DrawFlags.KeySource|DrawFlags.DoNotWait
						);
				}
			}

Would doing all this out side of both classes be slower then taking the data out of te. generating 2 rectangles with that data then passing those rectangles to Graphics to do its own internal tileDraw method? OR is their no difference at all? To simplify the question: Does moving data around a bunch of classes have any significantly greater lag that passing it around inline code? and secondly does flipping a classes surface from outside the class cause any kind of performance hit as opposed to passing it drawing data and letting it flip its surfaces internaly. thnks

Share this post


Link to post
Share on other sites
Quote:
Would doing all this out side of both classes be slower then taking the data out of te. generating 2 rectangles with that data then passing those rectangles to Graphics to do its own internal tileDraw method?

An ideal design would probably allow 'graphics' to manage all tile drawing and rectangle management. This way you could let graphics do batching optimizations or "on change" rendering.

Currently you have a nested loop of class function calls. That is, to draw the entire tile map you're going to be making (potentially) 1000's of calls. If you slimline/optimiza the ownership of the tile data then you could reduce your whole draw routine (from main's point of view) down to a single call.

Not only would this be cleaner, it would be faster.

Quote:
any significantly greater lag that passing it around inline code?

Yes, but depending on the scale you are looking at. A function call, particularly a class member function call is expensive by comparison to inline code. Without going too much into runtime organisational theory... your program/compiler will be jumping execution backwards and forwards for each function call - and each time it jumps it may need to rest various registers/stacks/local variables. You don't have to do any of this if you've got inline code.

But, as I said, it depends on scale - you wont notice the optimization much for 1 or 2 function calls, but in your nested loop example you could well save yourself several thousand of these jumps/stack/register manipulations. 100's of small improvements adds up to a nice healthy improvement in performance.


The way I'd probably do things, is have the "tileEngine" class maintain the precise data, but have an "hasChanged()" member and a "getTileInfo(x,y)" type setup. When tileEngine changes (obviously when its loaded, but maybe also as the game progresses) it sets "hasChanged()==true".

The graphics engine can then query "tileEngine::hasChanged()", if it's 'true' then it updates its internal storage via 'getTileInfo(x,y)'.

So, the initial frame transfers a lot of data, and maybe the occasional subsequent frame transfers a lot of data. But 90%+ of the frames you render 'graphics' has the data to hand and can just blit it directly to the screen without too much hassle.

I've used this (albeit with a more optimized/complicated data transferal system) to great effect in tile engines.

hth
Jack

Share this post


Link to post
Share on other sites
Great thanks :) optimising would have took more code (making more methods in tile engine and graphics) so it was a toss up as to whether more code or more streamlined was the way to go.... streamlined it is.

Thanks again for such informative advice with a great explination.

p.s
Just to clear up somthing to any one reading that code out of context it doesnt loop through each tile in the tile map only those that fall direclty or paritaly into the screens veiw and the tileEngine dynamicaly calculates exaclty how many tile fall into the screen eatch draw. (in case you where thiking is was totaly crappy code :P )

Share this post


Link to post
Share on other sites

This topic is 4748 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this