Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法使用渲染器创建精灵_C++_Sfml - Fatal编程技术网

C++ 无法使用渲染器创建精灵

C++ 无法使用渲染器创建精灵,c++,sfml,C++,Sfml,我正在制作一个游戏,我创建了一个类来存储地图。我有一个功能,创建一个单一的精灵,绘制所有可见的地图。我使用了一个渲染器,然后创建并返回一个用它创建的精灵。然而,雪碧是完全白色的 下面是地图生成和精灵绘制的代码 void Map::procedural_generate() { cout << "Starting the map generation" << endl; int generation_max = m_size.x * m_size.y,

我正在制作一个游戏,我创建了一个类来存储地图。我有一个功能,创建一个单一的精灵,绘制所有可见的地图。我使用了一个渲染器,然后创建并返回一个用它创建的精灵。然而,雪碧是完全白色的

下面是地图生成和精灵绘制的代码

void Map::procedural_generate()
{
    cout << "Starting the map generation" << endl;
    int generation_max = m_size.x * m_size.y, 
        current_generation = 0;
    vector<Decor> decor_vector;
    m_decor_vector.clear();

    const vector<Decor> const_vector
    {
        Decor(GRASS)
    };

    for (int i = 0; i < m_size.x; i++)
    {
        decor_vector.clear();

        for (int j = 0; j < m_size.y; j++)
        {
            decor_vector.push_back(const_vector[GRASS]);
            decor_vector[j].set_position(Vector2f(i * 32, j * 32));
            current_generation++;
            cout << "Generation : " << current_generation << '/' << generation_max << '\r';
        }

        m_decor_vector.push_back(decor_vector);
        decor_vector.clear();
    }
    cout << "Map generation has ended" << endl;
}

Sprite Map::sprite()
{
    RenderTexture map;
    if (!map.create(WINDOW_WIDTH, WINDOW_HEIGTH))
        cout << "Map : Unable to create the RenderTexture" << endl;

    map.clear(Color::Green);

    for (int i = 0; i < m_size.x; i++)
        for (int j = 0; j < m_size.y; j++)
            map.draw(m_decor_vector[i][j].sprite());

    map.display();

    Sprite sprite(map.getTexture());
    return sprite; 
}
void映射::程序生成()
{

使用SFML时可能会出现常见错误

Sprite Map::sprite()
{
    RenderTexture map;
    // ...
    Sprite sprite(map.getTexture());
    return sprite; 
}
map
是本地的。当函数结束时,
map
被销毁。Sprite将
map
的纹理作为浅拷贝,它是指向纹理的唯一指针,根据:

白方问题 你成功地加载了一个纹理,正确地构建了一个精灵,并且…你现在在屏幕上看到的一切 是一个白色的正方形。发生了什么事

这是一个常见的错误。当你设置一个雪碧的纹理时,所有的 内部是否存储指向纹理实例的指针。因此, 如果纹理被破坏或移动到内存中的其他位置,则精灵 以无效的纹理指针结束

所以,复制返回的sprite存储悬空指针。这只是未定义的行为



解决方案:您必须在某种深度可复制类中使用纹理包装精灵。 不能依赖默认生成的复制操作


这样的类可能看起来像:

class TexturedSprite {
public:
    sf::Sprite sprite;
    sf::Texture texture;

    TexturedSprite() {
        init(); // load texture
    }

    void init() {
        // load texture
        texture.loadFromFile("texture1.png");
        sprite.setTexture(texture);
        sprite.setPosition(0,0);
    }

    TexturedSprite(const TexturedSprite& theOther) {
        texture = theOther.texture; // deep copy
        sprite.setTexture(texture);
    }

    TexturedSprite& operator=(const TexturedSprite& theOther) {
        if (this == &theOther)
            return *this;
        texture = theOther.texture; // deep copy
        sprite.setTexture(texture);
        return *this;
    }
};
然后代码:

TexturedSprite fooMain;
{
    TexturedSprite foo;     // local 
    fooMain = foo;    
} // foo is destroyed, but = did deep copy of texture

是安全的。

使用SFML时常见的错误

Sprite Map::sprite()
{
    RenderTexture map;
    // ...
    Sprite sprite(map.getTexture());
    return sprite; 
}
map
是本地的。当函数结束时,
map
被销毁。Sprite将
map
的纹理作为浅拷贝,它是指向纹理的唯一指针,根据:

白方问题 你成功地加载了一个纹理,正确地构建了一个精灵,并且…你现在在屏幕上看到的一切 是一个白色的正方形。发生了什么事

这是一个常见的错误。当你设置一个雪碧的纹理时,所有的 内部是否存储指向纹理实例的指针。因此, 如果纹理被破坏或移动到内存中的其他位置,则精灵 以无效的纹理指针结束

所以,复制返回的sprite存储悬空指针。这只是未定义的行为



解决方案:您必须在某种深度可复制类中使用纹理包装精灵。 不能依赖默认生成的复制操作


这样的类可能看起来像:

class TexturedSprite {
public:
    sf::Sprite sprite;
    sf::Texture texture;

    TexturedSprite() {
        init(); // load texture
    }

    void init() {
        // load texture
        texture.loadFromFile("texture1.png");
        sprite.setTexture(texture);
        sprite.setPosition(0,0);
    }

    TexturedSprite(const TexturedSprite& theOther) {
        texture = theOther.texture; // deep copy
        sprite.setTexture(texture);
    }

    TexturedSprite& operator=(const TexturedSprite& theOther) {
        if (this == &theOther)
            return *this;
        texture = theOther.texture; // deep copy
        sprite.setTexture(texture);
        return *this;
    }
};
然后代码:

TexturedSprite fooMain;
{
    TexturedSprite foo;     // local 
    fooMain = foo;    
} // foo is destroyed, but = did deep copy of texture

是安全的。

它工作得很好,我只需要添加一些函数和变量来存储位置和纹理矩形。它工作得很好,我只需要添加一些函数和变量来存储位置和纹理矩形