Low game performance

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
DrGoldberg
Posts: 2
Joined: February 12th, 2017, 5:16 pm

Low game performance

Post by DrGoldberg » February 12th, 2017, 5:34 pm

Yo.

I'm trying to make a small tower defense game just to learn the basics, but I ran into this problem. I made a function to draw background tiles, each tile is 60x80 pixels, so the whole screen consists of 10x10 tiles. The function that draws all the tiles is in ComposeFrame so the whole map gets redrawn every frame even though it's static. This has made it so that my game is down to like 30 fps. The draw tile function looks kinda like this:

Code: Select all

void Game::DrawTile(int x, int y, int tileType)
{
	for (int i = 0; i < tileHeight; i++)
	{
		for (int j = 0; j < tileWidth; j++)
		{
			gfx.PutPixel(x * tileWidth + j, y * tileHeight + i, 178, 255, 102);
		}
	}
}
And then there is a draw map function which just creates 10x10 tiles so the whole screen is filled.

Code: Select all

for (int i = 0; i < mapHeight; i++)
	{
		for (int j = 0; j < mapWidth; j++)
		{
			DrawTile(j, i);
		}
	}
Is there a more efficient way to do this? Thanks in advance.

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Low game performance

Post by MrGodin » February 12th, 2017, 7:02 pm

Off the top of my head, it looks like you are drawing every pixel a lot of times.

Code: Select all

for (int i = 0; i <mapHeight; i += tileHeight)
	{
		for (int j = 0; j < mapWidth; j += tileWidth)
		{
			DrawTile(j, i);
		}
	}
then go

Code: Select all

void Game::DrawTile(int x, int y)
{
	int tileHeight = 60;// for testing
	int tileWidth = 80;
		for (int i = y; i < y+tileHeight; i++)
		{
			for (int j = x; j < x+tileWidth; j++)
			{
				gfx.PutPixel(j , i , 178, 255, 102);
			}
		}
	
}
I did some testing and this works. Assuming mapWidth = 800,mapHeight = 600
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Low game performance

Post by albinopapa » February 12th, 2017, 8:58 pm

@DrGoldberg, Are you running in Release instead of Debug?

As MrGodin points out, one thing that I see is that you are doing a lot of calculations in the densest part of the loops where you can be doing it earlier and less often.

Code: Select all

for (int y = 0; i < mapHeight; y += tileHeight)
{
     for (int x = 0; j < mapWidth; x += tileWidth)
     {
          // Precalculate the starting pixel positions here
          DrawTile(x, y);
     }
}

void Game::DrawTile(int x, int y, int tileType)
{
     for (int iy = y; iy < y + tileHeight; ++iy)
     {
          for (int ix = x; x < x + tileWidth; ++ix)
          {
               gfx.PutPixel(ix, iy, 178, 255, 102);
          }
     }
}

If you move some of the calculations out of the inner most loop AND you are compiling in Release mode AND you still are only getting 30fps, upload your cleaned project here or on GitHub and we'll take a look. Perhaps there is something more causing the slow down.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Low game performance

Post by chili » February 13th, 2017, 1:38 am

Yeah, I personally like to move the calculations out of the loops as much as possible, though even then it shouldn't be a big issue until you get into a lot of overdraw. My first instinct is check your build config (Release/Debug).
Chili

DrGoldberg
Posts: 2
Joined: February 12th, 2017, 5:16 pm

Re: Low game performance

Post by DrGoldberg » February 13th, 2017, 11:30 pm

Turns out it was indeed because I was running in debug mode, didn't really think it was a big deal until I got to episode 13. Thanks for the loop advice too.

Post Reply