Sign in to follow this  
MoBaT

Drawing map takes all CPU

Recommended Posts

I'm using tmx files for my map and finally got a tmx loader to work. The map drawing code is lagging my game because of the CPU usage. To draw my map, im using code which is:

 

for (int i = 0; i < map->GetNumLayers(); ++i) 
{
		// Get a layer.
		const Tmx::Layer *layer = map->GetLayer(i);

		for (int y = 0; y < 20; ++y) 
		{
			for (int x = 0; x < 20; ++x) 
			{
				// Get a test tile which is 32 x 32.
				Sprite* Suf2 = new Sprite(Surf_Tileset->getRect(TILE_SIZE, TILE_SIZE, TILE_SIZE, TILE_SIZE), 1, 0);
				// Draw tile to position on map
				Suf2->draw(Surf_Display, x * TILE_SIZE, y * TILE_SIZE);
			}
		}
}

 

So this code is on my Map::OnRender(SDL_Surface* Display). What can I do to keep my cpu from maxing?

Edited by MoBaT

Share this post


Link to post
Share on other sites

Don't use new to create new Sprites every time (this is a memory leak BTW, because you're not deleting them...).

 

Worked perfectly. Thank you, I should've known.

Share this post


Link to post
Share on other sites

I got another question relating to this. How would I make it where I can use my Sprite class and use it in the loops? My loop loops through the different tiles and each of those tiles has a tileID from my bmp file. If the getTileID returns 23, it means it's the 23rd block in my bmp so I have to change my Surface for the new rectangle. I tried doing it but kept on getting the lagging problem because I frequently change the sprite for the different tiles.

Share this post


Link to post
Share on other sites

SDL_Surface has built in ability to "draw" just portions of a larger image (like your tilesheet).

By not creating entirely new surfaces, you'll automatically gain enormous speedups (The fastest code is the code that never runs).

See Lesson 6, of Lazyfoo's SDL tutorials for a demonstration.

 

Not everything needs to be wrapped in classes - sometimes in an attempt to be "object oriented", we tend to over-use classes in our code and accidentally discard the other types of programming architectures or, like in this case, eliminate important functionality that used to exist before we wrapped the libraries.

 

SDL_Surface is perfectly good as it is, without being wrapped. wink.png

 

Not all OOP objects need to be C++-style classes, and not all code needs to be OOP.

If you are trying to code in OOP,  I'd recommend not worrying about it, and instead focusing on the RAII, KISS, DRY, and SRP principles.

If you don't know what OOP is, or why I keep on yacking on about it, just focus on those last four links instead and forget I ever mentioned OOP.

Share this post


Link to post
Share on other sites

I got another question relating to this. How would I make it where I can use my Sprite class and use it in the loops? My loop loops through the different tiles and each of those tiles has a tileID from my bmp file. If the getTileID returns 23, it means it's the 23rd block in my bmp so I have to change my Surface for the new rectangle. I tried doing it but kept on getting the lagging problem because I frequently change the sprite for the different tiles.

What you need to do in your initialisation part of your program is to build a collection of sprites, one per tile of your BMP file (and obviously, the n-th sprite in your collection is the sprite to display if the tileId is n).

 

Then, in your drawing loop, retrieve the sprite from your collection using the tileId, and draw it.

 

By the way, this principle is appliable to other game elements as well (game element, animation of these game elements and so on)

Share this post


Link to post
Share on other sites

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