简体   繁体   English

C ++ SFML的性能问题

[英]Performance issue with C++ SFML

I recently starting to work on a tile based game on C++ using SFML library. 我最近开始使用SFML库在C ++上开发基于tile的游戏。 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. 因为我对C ++和SFML都没有多少经验,所以我非常认为性能问题与我的代码有关。

The basic idea is that I have and 2d array of tiles which I create dynamicly this way: 基本的想法是我有和我用这种方式动态创建的2d瓷砖阵列:

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. 变量app是sf :: RenderWindow,它作为引用传递给函数。

When I draw 10x10 map it runs on slow fps. 当我绘制10x10地图时,它以慢速fps运行。

Sorry for my bad english! 对不起,我的英语不好!

Thanks in advance! 提前致谢!

Your performance issue has probably nothing related to iterating the 2D array. 您的性能问题可能与迭代2D数组无关。 It's probably because of Tile::Draw performance, so try to optimize it. 这可能是因为Tile::Draw性能,所以尝试优化它。

But anyway - as @chris said, using a 1D vector instead of a 2D array can be a bit faster. 但无论如何 - 正如@chris所说,使用1D向量而不是2D数组会更快一些。 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. 渲染切片贴图的正确方法是创建一个大纹理,然后将切片渲染到此纹理上,然后将这个大纹理渲染到屏幕上,而不是渲染每个切片,就像通常在XNA中一样。

That's because the way SFML uses OpenGL isn't friendly to large number of Draw() calls. 这是因为SFML使用OpenGL的方式对大量的Draw()调用并不友好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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