1

I'm trying to make a vector of a custom class (Called "WorldObject").

The WorldObject class is defined here:

class WorldObject : public sf::Drawable {
private:
    sf::Texture _texture;

    int spriteWidth;
    int spriteHeight;
    int xPos;
    int yPos;

public:
    WorldObject(std::string filename, int width, int height, int _xPos, int _yPos);
    sf::Sprite _sprite;
    ~WorldObject();
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;

    sf::FloatRect getCollisionBox();

};

The initialization class is defined here:

WorldObject::WorldObject(std::string filename, int width, int height, int _xPos, int _yPos) {

    if(_texture.loadFromFile(filename)) {
        _sprite.setTexture(_texture);
    }

    spriteWidth = width;
    spriteHeight = height;

    xPos = _xPos;
    yPos = _yPos;
    _sprite.setPosition(xPos, yPos);

}

And the vector code is defined here (It is a vector<WorldObject>):

worldObjects.push_back({name, width, height, x, y});

I've also used worldObjects.push_back(WorldObject(name, width, height, x, y));

where name is the filename (In this case, test_object.png), width is the width of the object (192 px), height is the height of the object (192 px), x is the x position (300) and y is the y position (400) in the SFML window (800px, 600px).

I'm trying to load test_object.png. For some kind of reason, it ALWAYS ends up creating a white square instead, in the correct position and with the right dimensions, though.

I can't figure out why this happens. I've tried passing "name" by reference, by value, trying to use a vector<Texture> and substituting filename's std::string type by a sf::Texture one without any avail.

Thanks @super for making me remember that the Texture doesn't load and that the file name is correct and that the file exists

8
  • Is the texture loading succesfully? Commented Oct 7, 2021 at 16:06
  • Well, no. The texture doesn't load Commented Oct 7, 2021 at 16:11
  • Have you checked that the filename is correct, and that the file actually exists in the right location? How do you run the program? Commented Oct 7, 2021 at 16:34
  • Yes, the filename is correct. I forgot to mention it. I've checked like 5 times. Thanks Commented Oct 7, 2021 at 16:36
  • And what about if the file in the correct place/folder? Normally the file should be in the same folder as the executable. But that depends on how you run it. Commented Oct 7, 2021 at 16:43

1 Answer 1

2

I've finally solved it! Thanks, @rafix07!

this

worldObjects.push_back({name, width, height, x, y});

line should be substituted by:

WorldObject *tmp = new WorldObject(name, width, height, x, y);
worldObjects.push_back(*tmp);

Remember to delete the object when it's not needed to reduce risk of memory leaks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.