2017-08-03 110 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