仅具有地图和小地图绘制(SFML)的低帧率

时间:2017-08-03 14:26:58

标签: c++ sfml frame-rate

我正在进行一场小型游戏"像项目一样练习,我设法让我的帧速率达不到3 FPS。虽然唯一可以绘制的是屏幕填充图块和小地图。 现在我发现问题出在小地图上,没有60 FPS的上限。但不幸的是,我没有足够的经验来发现真正的问题是什么......

我的绘画功能:

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纹理。

因此,当我省略迷你地图时,只绘制地图本身时,它比我想要的更流畅......

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您正在为每个帧生成全新的视图。应该在启动时做一次。