Search code examples
c++classvectorsfmlpush-back

Vector of class not storing separate Textures


I'm creating a car simulation using SFML.

As a matter of organization and logic, I created a single class "car", which also inherits sf::RectangleShape, and within this class there are other SFML objects, among them a Texture and a method to setup it.

I want to have multiple cars, so I created a vector of class "car".

In this example, I left only 2 cars with the images: "car-red.png" and "car-black.png". enter image description here

Here is an extract from the logic I'm using (I did a test program to make it easier to understand):

#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;
}

There are two problems I can not solve:


1) Even doing push_back of the two cars, only the last image prevails. Apparently, push_back refers to a single RAM address for the image.

Then the result looks like this: enter image description here

That is, the second image (car-black.png) is probably overlapping the address of the first image.

The curious thing is that this only happens with the Texture class. In the example, the program debug a string within the class and in this case there is no overlap:

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

However the Texture objects within the class vector are pointing to the same memory address:

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

How to solve this?

2) The second problem is that, for each vecRet.push_back(objRet), the following errors appear in the console:

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.

What is this?


Solution

  • Your first problem happens because you only instantiate a single car, but you load both images onto it. The second problem might be caused by the fact that push_back() copies the elements into the vector, resulting in the creation of four cars in total, rather than two.

    Try this code:

    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());