Search code examples
c++spritesfml

a white box problem in C++ SFML due to Sprite


Hello i was trying to draw a simple square for my game but it is drawing a white square instead of my image. Here is my codes...

paddle.hpp

class Paddle
{
public:
    Paddle(){}
    Paddle(sf::RectangleShape myPaddle);

    void Draw(sf::RenderWindow& window);
    void setImage();

private:

    sf::Texture m_texBackground;
    sf::Sprite m_imgBackground;
    sf::RectangleShape m_shape;
};

paddle.cpp

Paddle::Paddle(sf::RectangleShape myPaddle) :m_shape(myPaddle)
{
    [b][font=times new roman]setImage();[/font][/b]
}
void Paddle::setImage()
{
    if (!m_texBackground.loadFromFile("img/bats/bat_black.png")) {
        std::cout << "bat_black.png didnt open" << std::endl;
    }
    else {
        m_imgBackground.setPosition(m_shape.x, m_shape.y);
        m_imgBackground.setTexture(m_texBackground);

        //Some scale stuffs
    }
}

void Paddle::Draw(sf::RenderWindow& window)
{

    window.draw(m_shape);
    window.draw(m_imgBackground);
}

game.hpp

    class Game
    {
    public:
        Game();
        void startGame(); //My main while loop here
                //some other functions...
    private:
        sf::RenderWindow m_window;
        Paddle m_paddle;

    };

game.cpp

Game::Game()
{
    sf::RectangleShape myPaddle(sf::Vector2f(100, 20));
    myPaddle.setPosition(sf::Vector2f(m_windowWidth/2.0f, m_windowHeight-4.0f * raketim.getSize().y));
    myPaddle.setFillColor(sf::Color::Green);
    m_paddle =Raket(myPaddle);
    //m_paddle.setImage();
}
void Game::startGame()
{
    while (m_window.isOpen())
    {
    
            //some other while loops event stuffs functions etc here
        m_paddle.Draw(m_window);
        //some other functions here
    }
}

So i am going to tell you what is my problem. When i try to call "setImage" function inside from Game.cpp inside of Game constructor all is well, it is working.. I mean drawing the Sprite yes... But when i try to call "setImage" inside from Paddle.cpp inside of Paddle constructor it is drawing a white square...
Actually for this paddle thats okay it is drawing at least.. But i added many images for my game walls. it is inside of big loops.. I cant call it from Game constructor always.. So how can i call it from my base class? I checked white box problems but i am already defining my Texture and my Sprite in my Paddle.hpp. I mean it is not disappear inside of small scopes..So whats the problem? I am new at SFML..Please can you help me?


Solution

  • I guess i fixed the problem at least. The problem is whatever i do i couldnt prevent some instances from losing because ending their related scopes. It took time for seeing because i didnt do it base class..But i am defining a paddle in my Game.cpp. So problem is same. They are calling this a white square problem..am letting SFML's document here you can check from there. I hope it helps you SFML the white square problem