Search code examples
c++classinitializationtexturessfml

C++ SFML texture wont load from class


I am working on a simple little game in c++ (SFML).
It involves the need to create objects of the Ghost class and store them in an array, each ghost must also be assigned a texture based one their type, something I have as input to the class init function. Overall my class looks like this:

class Ghost{
    public:
        Sprite sprite;
        Texture texture;
        int type;



        Ghost(int ty=1){
            type = ty;

            if (type == 1){
                texture.loadFromFile("assets/ghost.png");
            } else if (type == 2){
                texture.loadFromFile("assets/monster.png");
            }

            sprite.setTexture(texture);
        }
};

The thing is, when attempting to create a ghost the texture is not loaded. I know the initializer runs since other code in the method do run, but for some reason not the texture loader. In case it is of any use, here is how I load a ghost:

Ghost ghosts[20];

ghosts[0] = Ghost(1);
int numGhosts = 1;

Why doesn't this approach work?


Solution

  • Textures dont mix well with arrays/vectors. The copy operator needs to specified as the default one is not enough to maintain the same functionality in arrays/vectors. Pointers/dynamic textures or a different method needs to be implemented for this to work. I suggest doing the latter and instead of sending a number as an argument to the contructor, sending a premade texture variable.

    #include <SFML/Graphics.hpp>
    #include <SFML/Window.hpp>
    #include <iostream>
    using namespace std;
    using namespace sf;
    
    class Ghost{
        public:
            Sprite sprite;
    
            Ghost(sf::Texture tex){
                sprite.setTexture(tex);
    
            }
            Ghost(){}
    };
    
    int main()
    {
        Texture gh,mon;
        gh.loadFromFile("boy.png");
        mon.loadFromFile("girl.png");
        
        Ghost arr[] = {gh, gh, mon, mon, gh};//this
        arr[1].sprite.setTexture(gh);//or this
        
        // create the window
        sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
        window.setFramerateLimit(60);
        // run the program as long as the window is open
        while (window.isOpen())
        {
            // check all the window's events that were triggered since the last iteration of the loop
            sf::Event event;
            while (window.pollEvent(event))
            {
    
            }
    
    
            // clear the window with black color
            window.clear(sf::Color::Black);
    
            // draw everything here...
            window.draw(arr[1].sprite);
            // end the current frame
            window.display();
        }
    
        return 0;
    }
    

    If you NEED the textures to be in the objects, make them as dynamic variables, this should dissolve the problem then.

    if you NEED to use arr[1] = Ghost(1), look into operator overloading in c++ and overload the '=' operator