Reputation: 7994
I am using SFML and I can't find any explanation on how the library is working and I have performance issues. I am trying to make a tile map made of 50x50 tiles that are each 20x20 pixels wide. 1) Each tile is a sf::sprite whose graphic image is simply a big image (my tileset), on which I set a 20x20 subrectangle. 2) I am simply looping over total number of tiles and using App.Draw(Map[i][j]); to draw each tile, and I am using "view" objects so I can move the view around
Now I have a very low FPS (1 image / second) and I am wondering 2 things in relation to the previous points. 1) Is it that each sf::sprite takes time to draw because their image is coming from a huge image that was cropped? 2) Am I right to loop over the whole set of tiles, even the ones I am not seeing? I am assuming that the view object makes it so that the tiles that are out of view are not re-computed in vain
thanks
Upvotes: 2
Views: 1521
Reputation: 13973
This is generally considered the wrong way to render a tile map.
The more correct way is to create a large texture, or sf::Image
in this case, and then rendering your tiles onto this texture, and then rendering this large texture to the screen. There are cases where this approach isn't feasible, but for most cases, it's much better.
This way, you only render each tile once when the tile map is loaded, and then you only need to render the large texture once for each frame, as opposed to rendering 2500 individual tiles for each frame.
The way SFML uses OpenGL isn't friendly to large numbers of Draw()
calls, so it helps to find ways to call it as few times as possible.
Upvotes: 2