【问题标题】:SFML texture not loading when encapsulating class member function is called from outside从外部调用封装类成员函数时未加载 SFML 纹理
【发布时间】:2015-09-13 17:49:20
【问题描述】:

tl/dr:我已将函数调用从类内部移至类外部,但该函数停止工作。

我遇到了一年中最令人困惑的问题,所以我一直在使用 c++。我找不到任何东西来解释这里发生了什么,但老实说,我什至很难提出一个 SEO 问题。

这里的基础操作比较简单,

  1. 创建一个 sf::Texture 和 sf::Sprite 对象
  2. 将纹理加载到 sf::Texture 对象
  3. 将 sf::Sprite 的纹理设置为纹理对象
  4. 显示 sf::Sprite

在一个函数中所有 4 个步骤都很好,但因为我的目标是构建一个游戏引擎,我开始将它封装到更大的类中。

我创建了一个 GameEngine 类并让它处理第 4 步。经过一些更正后一切顺利。

然后我创建了一个 GameObject 类来处理前三个步骤,作为框架的“用户”,我所要做的就是创建对象并告诉它进行渲染,这也有效。

然后,当我将第 2 步的函数调用从对象的构造函数移到对象外部时,我遇到了障碍。

旧情况: 类游戏对象 { 对象类型 d_type; 游戏引擎 *d_engine;

    sf::Texture d_texture;
    sf::Sprite d_sprite;
    bool isactive;
    public:
        GameObject(GameEngine *engine, ObjectType type);

        void addtexture(std::string textpath);

        void render();

和

GameObject::GameObject(GameEngine *engine, ObjectType type)
:
    d_type(type),
    d_engine(engine),
    d_texture(),
    d_sprite(),
isactive{false}
{
    addtexture("textures//MenuBackGround.png"); //<- problematic line
    d_sprite.setTexture(d_texture);
}

void GameObject::addtexture(std::string textpath)
{
    if(!d_texture.loadFromFile(textpath))
    {
        cout << "couldn't load texture in\n";
    } else
    {
        cout << "did load texture\n";
    }
}

这行得通,我看到我创建的纹理出现在窗口中。如果我现在创建一个类 Loadingscreen:

class Loading_Screen : public Base_State
{
    std::vector<GameObject> d_backgrounds;

    public:
        Loading_Screen(GameEngine *engine);
        virtual ~Loading_Screen();

实施:

Loading_Screen::Loading_Screen(GameEngine *engine)
{
    GameObject temp(engine, ObjectType::BACKGROUND);
    d_backgrounds.push_back(temp);
    temp.addtexture("textures//MenuBackGround.png");
}

我只看到黑屏。但在这两种情况下,我都会收到纹理已加载的消息。

【问题讨论】:

    标签: c++ c++11 sfml


    【解决方案1】:

    假设您实际上是在渲染 d_backgrounds,我认为错误就在这里:

    Loading_Screen::Loading_Screen(GameEngine *engine)
    {
        GameObject temp(engine, ObjectType::BACKGROUND);
        d_backgrounds.push_back(temp);
        temp.addtexture("textures//MenuBackGround.png");
    }
    

    您正在创建一个GameObject 对象。然后您将 它的副本 插入到容器中,而您稍后尝试addtexture 的对象与您插入的对象不同。

    试试这个:

    Loading_Screen::Loading_Screen(GameEngine *engine)
    {
        GameObject temp(engine, ObjectType::BACKGROUND);
        temp.addtexture("textures//MenuBackGround.png");
        d_backgrounds.push_back(temp);
    }
    

    查看 SFML api,Texture 和 Sprite 都有正确的复制构造函数,所以这样应该没问题。

    【讨论】:

    • 不,那没有用,同时我自己也在玩它并试图使 d_backgrounds 成为指针向量,遗憾的是这并没有解决问题。就像我说的那样,从工作到不工作的唯一改变就是改变调用 addtexture 函数的位置,在两种情况下渲染都是以相同的方式完成的。
    • 那是错误的。您在没有初始化纹理的情况下传递了对象的副本。尝试使用指针时,您是否添加了这样的对象?游戏对象* tmp = 新游戏对象(...);临时->添加纹理(...); d_backgrounds.push_back(temp);我目前没有考虑到 SFML api,但可能有些东西弄乱了精灵和纹理既是对象又是复制构造函数。
    • 我通过将 sf::Texture 和 sf::Sprite 更改为由 GameObject 保存为指针来修复它,从而消除了在处理 GameObject 时可能发生的任何复制。感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多