Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Need help! Sprite White Box Problem  (Read 1853 times)

0 Members and 1 Guest are viewing this topic.

Eru

  • Newbie
  • *
  • Posts: 3
    • View Profile
Need help! Sprite White Box Problem
« on: April 09, 2021, 10:22:12 pm »
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]   //This was i talked about
}
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 will 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. İ mean it is not disappear inside of small scopes..So whats the problem? Please can you help me? I am asking a question first time..Sorry if i havemistakes :'(

Eru

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Need help! Sprite White Box Problem
« Reply #1 on: April 10, 2021, 12:38:09 pm »
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
 https://www.sfml-dev.org/tutorials/2.3/graphics-sprite.php#the-white-square-problem