我正在研究一个像项目一样的小型“游戏”作为练习,并且我设法让帧率降至3甚至3帧。虽然唯一正在绘制的是屏幕填充砖和小地图。 现在我发现问题在于小地图,没有60 FPS的盖帽。但不幸的是,我没有足够的经验,找出真正的问题是做什么用......只有地图和小地图绘制(SFML)的低帧率
我绘制函数:
void StateIngame::draw()
{
m_gui.removeAllWidgets();
m_window.setView(m_view);
// Frame counter
float ct = m_clock.restart().asSeconds();
float fps = 1.f/ct;
m_time = ct;
char c[10];
sprintf(c, "%f", fps);
std::string fpsStr(c);
sf::String str(fpsStr);
auto fpsText = tgui::Label::create();
fpsText->setText(str);
fpsText->setTextSize(16);
fpsText->setPosition(15, 15);
m_gui.add(fpsText);
//////////////
// Draw map //
//////////////
int camOffsetX, camOffsetY;
int tileSize = m_map.getTileSize();
Tile tile;
sf::IntRect bounds = m_camera.getTileBounds(tileSize);
camOffsetX = m_camera.getTileOffset(tileSize).x;
camOffsetY = m_camera.getTileOffset(tileSize).y;
// Loop and draw each tile
// x and y = counters, tileX and tileY is the coordinates of the tile being drawn
for (int y = 0, tileY = bounds.top; y < bounds.height; y++, tileY++)
{
for (int x = 0, tileX = bounds.left; x < bounds.width; x++, tileX++)
{
try {
// Normal view
m_window.setView(m_view);
tile = m_map.getTile(tileX, tileY);
tile.render((x * tileSize) - camOffsetX, (y * tileSize) - camOffsetY, &m_window);
} catch (const std::out_of_range& oor)
{}
}
}
bounds = sf::IntRect(bounds.left - (bounds.width * 2), bounds.top - (bounds.height * 2), bounds.width * 4, bounds.height * 4);
for (int y = 0, tileY = bounds.top; y < bounds.height; y++, tileY++)
{
for (int x = 0, tileX = bounds.left; x < bounds.width; x++, tileX++)
{
try {
// Mini map
m_window.setView(m_minimap);
tile = m_map.getTile(tileX, tileY);
sf::RectangleShape miniTile(sf::Vector2f(4, 4));
miniTile.setFillColor(tile.m_color);
miniTile.setPosition((x * (tileSize/4)), (y * (tileSize/4)));
m_window.draw(miniTile);
} catch (const std::out_of_range& oor)
{}
}
}
// Gui
m_window.setView(m_view);
m_gui.draw();
}
的Tile
类有它的地图中设置sf::Color
类型的变量发电。然后使用该颜色绘制小地图,而不是用于地图本身的16x16纹理。
所以,当我离开了微缩图,只画了地图本身,它更流畅,比我求之不得......
任何帮助表示赞赏!
究竟是什么问题?这是封顶在60fps? – snb
它仅以3 fps运行,这是因为小地图绘制循环。当我删除它,它运行在60,这很好。 – nepp95
而不是使用视图来绘制小地图,它可能会更快,更灵活地将小地图渲染到[RenderTexture](https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1RenderTexture.php ),然后在需要的地方绘制该纹理。 – 0x5453