简体   繁体   中英

Performance issue with C++ SFML

I recently starting to work on a tile based game on C++ using SFML library. Because I dont have much exprience with either C++ and SFML I'm pretty sute that the performance issue has something to do with my code.

The basic idea is that I have and 2d array of tiles which I create dynamicly this way:

Tile tiles**;

tiles = new Tile*[sizeX];

for(int i = 0; i < sizeX; i++)
   tiles[i] = new Tile[sizeY];

And I draw everything this way:

for(int x = 0; x < sizeX; x++)
{
   for(int y = 0; y < sizeY; y++)
   {
       tiles[x][y].Draw(app);
   }
}

The variable app is sf::RenderWindow and it passes to the function as reference.

When I draw 10x10 map it runs on slow fps.

Sorry for my bad english!

Thanks in advance!

Your performance issue has probably nothing related to iterating the 2D array. It's probably because of Tile::Draw performance, so try to optimize it.

But anyway - as @chris said, using a 1D vector instead of a 2D array can be a bit faster. But that shouldn't make significant performance changes.

The correct way to render a tile map would be to create a large texture and then rendering your tiles onto this texture, and then rendering this large texture to the screen, instead of rendering every tile, like you'd normally do in XNA.

That's because the way SFML uses OpenGL isn't friendly to large number of Draw() calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM