SFML community forums

Help => Graphics => Topic started by: Glenn on December 03, 2008, 10:16:27 am

Title: Problem with tiles and performance
Post by: Glenn on December 03, 2008, 10:16:27 am
Ok so I use this function to draw tiles to my screen:

Code: [Select]
for(i = 0; i < 40; i++)
{
for(j = 0; j < 20; j++)
{
int tileId = 0;
currentTile.SetImage(tilesImage);
tileId = gameMap[i][j].tileId;

currentRect.Left =  32 * tileId;
currentRect.Right = 32 + 32 * tileId;
currentRect.Top = 0;
currentRect.Bottom = 32;
currentTile.SetSubRect(currentRect);


currentTile.SetPosition(32 * i, 32 * j);

App.Draw(currentTile);
}
}


On my laptop when I run the game it runs fine, but on my desktop, which is considerably better than my laptop, I get about 1/3 of the fps.

I've narrowed it down to this bit of code right here.
Why does it run so slow on my desktop? and is there a better way of drawing tiles to the screen?

Edit: Upon further inspection the App.Draw(currentTile); is whats slowing everything down, is there any way to fix it?
Title: Problem with tiles and performance
Post by: bullno1 on December 03, 2008, 01:56:43 pm
40*20=800 tiles which is 1600 tris. This is not a huge number. It even runs fine on your laptop so I think maybe your desktop has a poor OpenGL driver.

Quote
is there a better way of drawing tiles to the screen?

Your implementation is quite OK. There's a faster way though: You can use hardware vertex buffer and hardware index. But that's an overkill in this case.
Title: Problem with tiles and performance
Post by: efeXor on December 04, 2008, 06:44:33 am
Try updating your opengl driver  :wink:
Title: Problem with tiles and performance
Post by: Glenn on December 04, 2008, 07:16:23 am
I just updated it and its still the same. I found a fix that's working for me though.

 
Code: [Select]
 for(i = 0; i < 40; i++)
      {
         for(j = 0; j < 20; j++)
         {
            if(!gameMap.tileId == 0)
           {
            int tileId = 0;
            currentTile.SetImage(tilesImage);
            tileId = gameMap[i][j].tileId;

            currentRect.Left =  32 * tileId;
            currentRect.Right = 32 + 32 * tileId;
            currentRect.Top = 0;
            currentRect.Bottom = 32;
            currentTile.SetSubRect(currentRect);
           
           
            currentTile.SetPosition(32 * i, 32 * j);
           
            App.Draw(currentTile);  
           }
         }
      }


That if statement prevents invisible tiles from being drawn[/code]
Title: Problem with tiles and performance
Post by: Wizzard on December 04, 2008, 09:40:11 am
You can try using multiple sprites to draw each tile, rather than using one sprite to draw each tile, in order to further increase execution speed, at the cost of memory.

This can be accomplished by either using separate sprites for each tile, or, optimally, by using separate sprites for each frame in the sprite sheet.
Title: Problem with tiles and performance
Post by: prchakal on December 14, 2008, 02:48:36 pm
I have the same performance problem here:

http://www.sfml-dev.org/forum/viewtopic.php?t=816&highlight=performance

In my game in SDL i do it easy , with the same logic and with floors, and this problem dont exists.

The problem is on DRAW method, too many DRAW call cause it.

Where is the problem? SFML ? Me and the author of this post? The creator?

I try the same with on a new pc, on SVN release, and the problem is the same.