c++ - C++ 如何将事物保持在范围内?

标签 c++ sfml

更具体地说,我有一个如下所示的类:

class Ball {
    public:
        unsigned collider_size;
        scionofbytes::MovementComponent movement;
        scionofbytes::GraphicComponent graphic;

        Ball(u_int init_collider_size, std::string texture_path) {
            collider_size = init_collider_size;
            movement = scionofbytes::MovementComponent();
            graphic = scionofbytes::GraphicComponent(
                    (u_int) collider_size/2,
                    (u_int) collider_size/2,
                    texture_path
            );
        }
};

我接受texture_path并将其传递给图形组件,如下所示:

class GraphicComponent {
    unsigned height;
    unsigned width;

    public:
        sf::Texture texture;
        sf::Sprite sprite;

        GraphicComponent() {}

        GraphicComponent(unsigned init_height, unsigned init_width, std::string texture_path) {
            width = init_width;
            height = init_height;

            texture.loadFromFile(texture_path);
            sprite.setTexture(texture);
        }
};

当我通过传入纹理路径实例化球对象时,我将创建一个纹理作为图形组件的成员,然后将该纹理分配给图形组件的 Sprite 成员。

当使用这个 sprite 成员绘制到屏幕上时,我面对的是 SFML 已知的 white box problem .

现在,根据我的理解,球对象保持事件状态,图形组件成员也保持事件状态,图形组件的纹理成员也是如此。

所以我的问题是,为什么这不起作用?当使用 Sprite 在屏幕上绘制时,我仍然得到一个白框。为什么纹理会被破坏?

最佳答案

在您的 Ball 类构造函数中,您正在制作 GraphicComponent拷贝。 IIRC sf::Sprite 仅保存对 sf::Texture 的引用,因此您的拷贝最终可能会指向 sf::Sptite从复制的对象中删除的 sf::Texture

尝试在不复制 GraphicComponent 的情况下构建您的 Ball:

class Ball {
    public:
        unsigned collider_size;
        scionofbytes::MovementComponent movement;
        scionofbytes::GraphicComponent graphic;

        // Use initializer-list
        Ball(u_int init_collider_size, std::string texture_path)
        : collider_size(init_collider_size)
        , movement()
        , graphic((u_int) collider_size/2, (u_int) collider_size/2, texture_path)
        {
             // don't assign stuff here if you can avoid it
        }
};

除此之外,您可能还想为 GraphicComponent 类创建一个复制构造函数,以防止其他地方发生损坏:

class GraphicComponent
{
    unsigned height;
    unsigned width;

public:
    sf::Texture texture;
    sf::Sprite sprite;

    GraphicComponent()
    {
    }

    GraphicComponent(unsigned init_height, unsigned init_width,
        std::string texture_path)
    {
        width = init_width;
        height = init_height;

        texture.loadFromFile(texture_path);
        sprite.setTexture(texture);
    }

    // Give it a copy constructor
    GraphicComponent(GraphicComponent const& other)
    : height(other.height)
    , width(other.width)
    , texture(other.texture)
    , sprite(texture) // make it point to the new Texture not the other one
    {

    }
};

关于c++ - C++ 如何将事物保持在范围内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40253537/

相关文章:

c++ - SFML 正在做一些与 vector 数组非常相关的事情

c++ - 将引用传递给函数是否可以进行内存管理?

C++ sfml 在窗口打开后立即接收关闭事件

c++ - 模板函数作为模板参数

c++ - 小数背包中实现的贪心算法

c++ - boost::interprocess - allocate_aligned 在共享内存中?

c++ - 引用静态函数局部变量

c++ - 对于以下用途,vector 和 deque 之间最有效的是什么?

c++ - 在我的资源类中将通过 loadFromFile() 加载字体的函数更改为 loadFromMemory()

c++ - 虚函数C++中的默认参数