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

Author Topic: background image to change to another image after a few seconds.  (Read 2076 times)

0 Members and 1 Guest are viewing this topic.

yuri00

  • Newbie
  • *
  • Posts: 1
    • View Profile
background image to change to another image after a few seconds.
« 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;
}
« Last Edit: September 22, 2017, 01:20:12 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: background image to change to another image after a few seconds.
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything