Petar Putar Visnjic Petar Putar Visnjic - 1 year ago 135
C++ Question

C++ SFML texture and sprite in class (white box)

So i'm looking for an answer about 1 month and no one can't figure out what is going on. I have class GameObject which have sprite and texture in it, and when i call function ->getSprite() and draw it on my window i get only white box. I retyped my code 3 times again and i have the same problem again and again. I will be so happy if someone help me with this. Here are my files. files

Answer Source

I once had the SAME problem. I struggled with loading a texture (.png), but I made a function where you simply give it the source (as a string) and than it returns the valid sf::Sprite.

//loads a graphic from a source
static sf::Sprite loadSprite(std::string source){
    static sf::Texture t;
    if (!t.loadFromFile(source)){
        std::cerr << "Error: File couldn't be loaded" << std::endl;
    static sf::Sprite s;
    return s;

Try to make this function for GameObject.h available (and maybe other files,too). To use it, simply add a

void setSprite(std::string source){
    sprite = loadSprite(source); //call that function

function into one of your GameObject.h public member functions, wich you can also call from the constructor. Try it out! Good luck

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download