【问题标题】:SFML drawing on absolute position with reference to windowSFML 参照窗口在绝对位置上绘图
【发布时间】:2018-02-26 19:09:39
【问题描述】:

我目前的 sfml 项目有一些看法。视图改变大小和中心。我可以用数学方法计算出我想要的位置,但这似乎工作量太大。有没有我可以使用的内置函数?

【问题讨论】:

  • 所以你的位置不是绝对的——它是相对于窗口的。你想做什么?也许有更好的解决方案 - 例如:用户界面。
  • 我的相机/视图随着玩家移动。现在我想在窗口上覆盖记分板。无论视图指向何处,它都需要始终位于窗口的特定部分。
  • 所以有更好的方法。

标签: c++ sfml


【解决方案1】:

是的,您可以按如下方式使用sf::RenderWindow::getDefaultView()(假设您有一个名为windowsf::RenderWindow):

// First draw all objects that you have set a view for
window.setView(yourView);

window.draw(viewObject);

window.setView(window.getDefaultView()); // Reset the view to the window's default one

// ... Set the position and all (You can do this before as well)

window.draw(yourScoreBoard);

window.display(); // You should see your views with a score board overlay that
                  // stays in the same place

如果你想绘制有自己视图的东西,那么首先将窗口的视图设置为那个视图,然后再绘制你的对象。

【讨论】:

    【解决方案2】:

    正如你所说,你想要一个叠加层。 UI 覆盖是使用单独的渲染纹理完成的。您应该将游戏拆分为两个渲染目标:

    • 游戏内事物(玩家、怪物、法术等)的渲染目标
    • UI 的渲染目标(分数、健康状况等)

    两个渲染目标也是纹理 (sf::RenderTexture)。您将所有内容呈现给他们,因为它们是简单的窗口。一旦将它们放在单独的纹理中,就可以使用它们在窗口内使用精灵来渲染所有内容:

    sf::RenderTexture inGameRT;
    sf::RenderTexture uiRT;
    
    while(window.isOpen())
    {
        // event loop
    
        // now drawing in-game (only example):
        inGameRT.clear();
        // std::vector<sf::Sprite*> players; // or sth other...
        for(auto const & player : players)
             inGameRT.draw(*player);
    
        inGameRT.display(); // don't forget it
    
    
    
        // now drawing UI (only example):
        uiRT.clear();
        // std::vector<sf::Sprite*> uiWidgets; // or sth other...
        for(auto const & widget : uiWidgets)
             uiRT.draw(*widget);
    
        uiRT.display(); // don't forget it
    
        // Once you drawn everything, you can display two textures inside window
    
        window.clear();
    
        sf::Sprite inGameSprite{inGameRT.getTexture()};
        sf::Sprite uiSprite{uiRT.getTexture()};
        window.draw(inGameSprite);
        window.draw(uiSprite);
        window.display();
    }
    

    sf::Views 也可以应用于sf::RenderTargets。

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2012-05-17
      • 2015-04-05
      • 1970-01-01
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 2013-10-14
      相关资源
      最近更新 更多