2017-08-03 108 views
0

我正在研究一个像项目一样的小型“游戏”作为练习,并且我设法让帧率降至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纹理。

所以,当我离开了微缩图,只画了地图本身,它更流畅,比我求之不得......

任何帮助表示赞赏!

+0

究竟是什么问题?这是封顶在60fps? – snb

+0

它仅以3 fps运行,这是因为小地图绘制循环。当我删除它,它运行在60,这很好。 – nepp95

+2

而不是使用视图来绘制小地图,它可能会更快,更灵活地将小地图渲染到[RenderTexture](https://www.sfml-dev.org/documentation/2.4.2/classsf_1_1RenderTexture.php ),然后在需要的地方绘制该纹理。 – 0x5453

回答

0

您正在为每个帧完整生成视图。在启动时这样做一次就足够了。

+0

然后再生相机的移动?这不会有相同的效果,但只有当移动相机? – nepp95

+0

尽管我并不真正了解您的答案如何使这项工作成功,但将其与0x5453的评论相结合已解决了我的问题。 我现在在启动时生成1x1像素形状的'sf :: RenderTexture'。整个地图,而不是边界被绘制到该纹理。在我的绘图函数中,只绘制了所需纹理的一部分。 – nepp95