SFML community forums

Help => Graphics => Topic started by: yuri00 on September 22, 2017, 12:57:50 am

Title: background image to change to another image after a few seconds.
Post by: yuri00 on September 22, 2017, 12:57:50 am
I want to change the background of the Spirit. However, if I use loadFromFile, I can see only the last image.
....OTL.........
I want the background image to change to another image after a few seconds.

int main()
{
        int i = 0;
        std::string link;
        char fileName = '0';
        std::string str(".jpg");
        std::string str0("tutorial/");
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
        sf::Texture texture;
        if (!texture.loadFromFile("images/bg.png"))
                return EXIT_FAILURE;

        sf::Sprite sprite(texture);
        // Create a graphical text to display

        // Start the game loop
        while (window.isOpen())
        {
                // Process events
                sf::Event e;
                while (window.pollEvent(e)){
                        // Close window : exit
                        if (e.type == sf::Event::Closed)
                                window.close();
                        if(i<6){
                                        fileName++;
                                        std::cout << fileName << std::endl;
                                        link = str0 + (char)fileName + str;
                                        _sleep(1000);
                                        texture.loadFromFile(link);
                                       
                                        i++;   
                        }
                }//while (window.pollEvent(e))
               
                window.clear();
                // Draw the sprite
                window.draw(sprite);
                window.display();
        }//while (window.isOpen())
        return EXIT_SUCCESS;
}
Title: Re: background image to change to another image after a few seconds.
Post by: eXpl0it3r on September 22, 2017, 02:23:11 am
Have both images in one texture (e.g. use a image editor) and then you can simply adjust the texture rect.