c++ - 类的 vector 不存储单独的纹理

标签 c++ class vector sfml push-back

我正在使用 SFML 创建汽车模拟。

出于组织和逻辑的考虑,我创建了一个类“car”,它也继承了 sf::RectangleShape,并且在该类中还有其他 SFML 对象,其中有一个纹理和一个设置它的方法。

我想要拥有多辆汽车,所以我创建了一个“汽车”类的 vector 。

在这个例子中,我只留下了 2 辆汽车的图像: “汽车-红色.png”和“汽车-黑色.png”。 enter image description here

这是我正在使用的逻辑的摘录(我做了一个测试程序以使其更容易理解):

#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
#define debug(x) std::cout << #x << "=" << x << std::endl;

using namespace std;

class car : public sf::RectangleShape {
public:
    string s;
    sf::Texture tex;
    sf::Sprite img;

    void imgInit(string imgFile) {
        tex.loadFromFile(imgFile);
        img.setTexture(tex);
        s = imgFile;
    }
};
int main()
{
    vector<car> vecRet;
    car objRet;

    objRet.imgInit("car-red.png");
    objRet.setSize(sf::Vector2f(150, 70));
    objRet.setFillColor(sf::Color::Yellow);
    vecRet.push_back(objRet);

    objRet.imgInit("car-black.png");
    objRet.setPosition(sf::Vector2f(300, 300));
    objRet.img.setPosition(objRet.getPosition());
    vecRet.push_back(objRet);

    debug(vecRet[0].s);
    debug(vecRet[1].s);
    debug(vecRet[0].img.getTexture());
    debug(vecRet[1].img.getTexture());

    sf::RenderWindow window(sf::VideoMode(500,500), "Window", sf::Style::Close);
    window.setFramerateLimit(120);

    while (window.isOpen())
    {
        for (sf::Event event; window.pollEvent(event);) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(vecRet[0]);
        window.draw(vecRet[1]);
        window.draw(vecRet[0].img);
        window.draw(vecRet[1].img);
        window.display();
    }

    return EXIT_SUCCESS;
}

有两个问题我无法解决:


1) 即使对两辆车进行push_back,也只有最后一张图像占上风。 显然,push_back 指的是图像的单个 RAM 地址。

然后结果如下所示: enter image description here

也就是说,第二个图像 (car-black.png) 可能与第一个图像的地址重叠。

奇怪的是,这只发生在Texture 类中。在示例中,程序调试类中的字符串,在本例中没有重叠:

vecRet[0].s=car-red.png
vecRet[1].s=car-black.png

但是类 vector 中的纹理对象指向相同的内存地址:

vecRet[0].img.getTexture()=000000C57A5FF250
vecRet[1].img.getTexture()=000000C57A5FF250

如何解决这个问题?

2)第二个问题是,对于每个vecRet.push_back(objRet),控制台中都会出现以下错误:

An internal OpenGL call failed in Texture.cpp(98).
Expression:
   glFlush()
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.

这是什么?

最佳答案

您的第一个问题发生是因为您只实例化了一个汽车,但您将两个图像加载到它上面。第二个问题可能是由于 push_back() 将元素复制到 vector 中,导致在总共,而不是两个。

试试这个代码:

vector<car> vecRet(2);

vecRet[0].imgInit("car-red.png");
vecRet[0].setSize(sf::Vector2f(150, 70));
vecRet[0].setFillColor(sf::Color::Yellow);

vecRet[1].imgInit("car-black.png");
vecRet[1].setPosition(sf::Vector2f(300, 300));
vecRet[1].img.setPosition(vecRet[1].getPosition());

关于c++ - 类的 vector 不存储单独的纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50652690/

相关文章:

c++ - 如何从格式为 "a:b"的字符串数组中填充 C++ 对 vector ?

c++ - 为什么用Visual Studio 2017编译openCV3.2会出错?

c++ - DirectX 11 访问冲突

c++ - C++构造对象时提供PV函数内容

python - 将数据帧作为类方法返回

c# - 两个向量 2D 之间的角度

c++ - 在 Visual Studio 中调整目录结构搞乱了#includes?

c++ - 对象创建必须使用哪种设计模式?

c++ - 如何通过重载 << 运算符打印出 vector 中的对象?

c++ - 使用 vector 作为类成员