简体   繁体   English

SFML 纹理未正确加载

[英]SFML texture not loading propely

I got class a with access to all standard SFML stuff:我得到了 class a 可以访问所有标准 SFML 的东西:

class A{
public:
A(int type, Vector2f pos){
if(type == 1) tex.loadFromFile(path1);
else if(type == 2) tex.loadFromFile(path2); 
//etc. etc.
shape.setTexture(&tex);

//then I set up the radius, and the position (from the constructor)
//I debugged this part and it seemed to work so I wont bother typing it out
};


Texture tex;
CircleShape shape;
};

In another class BI got an std::vector of A:在另一个 class BI 中得到了 A 的 std::vector:

 class B{
 public:
 void update(){
 //under a bunch of conditions
 list.push_back(A(1,Vector2f(100,100)); //examples
 list.push_back(A(1,Vector2f(200,200))
 }
 std::vector<A> list;
 };

Anyhow the textures don't load properly and I'm left with white spheres.无论如何,纹理无法正确加载,我只剩下白色球体。 I tried putting it in a separate function and calling that with back() and it loaded only the first one, but not the second.我试着把它放在一个单独的 function 并用back()调用它,它只加载了第一个,而不是第二个。 It's very strange behaviour and I don't know what's causing it.这是非常奇怪的行为,我不知道是什么原因造成的。

By calling shape.setTexture(&tex) , a pointer to your Texture object is stored in shape.通过调用shape.setTexture(&tex) ,指向纹理 object 的指针存储在 shape 中。

The problem is that you are using std::vector to manage your A objects.问题是您正在使用std::vector来管理您的 A 对象。 A std::vector manages an array on the heap. std::vector管理堆上的数组。 But this array is not dynamic, it cannot change its size.但是这个数组不是动态的,它不能改变它的大小。 So, in order to increase its size, a vector allocates a completely new array with the required size, copies the elements from the old array into the new one and deletes the old array.因此,为了增加它的大小,向量分配一个具有所需大小的全新数组,将旧数组中的元素复制到新数组中并删除旧数组。
Now, the pointer inside your Shape object is invalidated since it has not been updated and it now points to a memory location that could have anything in it.现在,形状 object 内的指针已失效,因为它尚未更新,它现在指向 memory 位置,其中可能包含任何内容。

The website https://en.cppreference.com/w/cpp/container/vector shows under "Iterator invalidation" what iterators (which are basically pointers) are invalidated by what methods when using a std::vector .网站https://en.cppreference.com/w/cpp/container/vector在“迭代器失效”下显示了哪些迭代器(基本上是指针)在使用std::vector时被哪些方法失效。

That is the reason why your textures are not working properly.这就是您的纹理无法正常工作的原因。

In order to fix this issue, I would suggest using some type of pointer to the tex object inside your A class.为了解决这个问题,我建议在你的 A class 中使用某种指向tex object 的指针。
You could use a raw pointer that points to a Texture object that you created with new inside your constructor.您可以使用原始指针指向您在构造函数中使用new创建的纹理 object。 But be sure to delete the object in the destructor.但一定要delete析构函数中的object。
As an alternative, you could use a std::unique_ptr or a std::shared_ptr to automatically manage the destruction of the Texture object.作为替代方案,您可以使用std::unique_ptrstd::shared_ptr来自动管理纹理 object 的销毁。

By using a pointer to a seperate object, pointer invalidation to the Texture object can be avoided since the object itself is not moved.通过使用指向单独的 object 的指针,可以避免指向纹理 object 的指针失效,因为 object 本身不会移动。

Alternatively, you could use a std::list instead of a std::vector because adding objects to a list does not invalidate pointers to objects in that list.或者,您可以使用std::list而不是std::vector ,因为将对象添加到列表不会使指向该列表中对象的指针无效。

From https://en.cppreference.com/w/cpp/container/list :https://en.cppreference.com/w/cpp/container/list

Adding, removing and moving the elements within the list or across several lists does not invalidate the iterators or references.在列表中或跨多个列表添加、删除和移动元素不会使迭代器或引用无效。 An iterator is invalidated only when the corresponding element is deleted.只有当相应的元素被删除时,迭代器才会失效。

The downside to lists is that they do not provide random access.列表的缺点是它们不提供随机访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM