Search code examples
c++spritetexturesdrawsfml

Draw sprite in draw loop


Got a problem, when I am trying to draw a sprite in the draw loop it won't draw In the constructor for this class I am setting the sprite.

Constructor I have:

texture.loadFromFile("img1.png");
sprite.setTexture(texture);

sprite and texture is in the header file

I did some experiments, and if I use texture.loadFromFile(); in the draw loop it works. But then I have to "reload" the picture 60 times per second.

void PlayerReceiver::draw(sf::RenderWindow & rw){
        //texture.loadFromFile("mario.png");

        //std::cout<<texture.getSize().y<<std::endl;

            rw.draw(sprite);

    }

Thank you for answers.


Solution

  • Got a problem, when I am trying to draw a sprite in the draw loop it won't draw In the constructor for this class I am setting the sprite.

    From what I understand from this question, you want to load a texture and sprite inside a constructor and it's not working when you try to draw it from your draw loop.

    I have a program which loads textures in the constructor and it runs fine, so what I would imagine is wrong (and I'm not sure since I don't see but six lines of your program in the above post) is that the texture and sprite objects are not defined on a class level.

    Here's my header file for a texture object I'm using:

    class GraphPaper
    {
        public:
            GraphPaper();
            ~GraphPaper();
            bool isObjectLoaded();
            sf::Sprite getSprite();
    
        private:
            void load(std::string filename);
            bool isLoaded;
    
            sf::Texture texture;
            sf::Sprite sprite;
    
            const std::string file = "images/Graph-Paper.png";
    };
    

    The texture and sprite objects are listed on a class-wide level as private members and are accessed through getter methods.

    And here's the constructor for that class (altered a bit to better match what you were wanting done):

    #include "GraphPaper.h"
    
    GraphPaper::GraphPaper() //: isLoaded(false)
    {
        if (texture.loadFromFile(file) == false)
            isLoaded = false;
        else
        {
            texture.setRepeated(true);
            sprite.setTexture(texture);
            isLoaded = true;
        }
        //load(file);
        assert(isObjectLoaded());
    }
    

    After this I simply draw my "GraphPaper" object.

    mainWindow.draw(paper.getSprite());
    

    I already have access to it because I defined a "GraphPaper" object called "paper" in my MainWindow header file.

    #pragma once
    #include "GraphPaper.h"
    #include "stdafx.h"
    
    class MainWindow
    {
        public:
             void close();
             void start();
             void moveCamera(sf::Event);
    
        private:
            bool leftMousePressed, rightMousePressed, isExiting;
            int r, g, b, mouseX, mouseY;
    
            GraphPaper paper;
    
            const sf::Color white = sf::Color(255, 255, 255);
    
            sf::RenderWindow mainWindow;
            sf::View view;
    };
    

    Assuming this is what you are wanting to do, then your problem is likely in the header file. Any objects defined on a class level, once loaded, won't get destroyed until the class itself is destroyed. Maybe you need to make sure you aren't destroying the reference to the class that the constructor is in. For instance, if you create an instance of your class inside a loop, the moment that loop ends the class object will be destroyed.