SFML community forums

Help => Graphics => Topic started by: gmestanley on February 04, 2017, 07:04:54 pm

Title: Problem with the draw & display methods
Post by: gmestanley on February 04, 2017, 07:04:54 pm
I'm making a game on C++ using SFML 2.4.1 for GCC SJLJ (CodeBlocks with MinGW). When it starts, I want my game to draw the SFML logo (like a splash screen) and, after 3 seconds, draw the title screen's background. Sounds simple, but after displaying the background, after a time it displays the SFML logo again, thus making a loop rather than making the image static. Could you guys help me? This is my code:
#include <Windows.h>
#include <SFML/Graphics.hpp>

int main()
{
    // Setting up resources and the window
    sf::RenderWindow screen(sf::VideoMode(816, 624), "Elkiria");
    sf::Texture tx_sfml;
    tx_sfml.loadFromFile("sfml.png");
    sf::Sprite sp_sfml;
    sp_sfml.setTexture(tx_sfml);
    sf::Texture tx_titlebg;
    tx_titlebg.loadFromFile("skybg.png");
    sf::Sprite sp_titlebg;
    sp_titlebg.setTexture(tx_titlebg);

    // While the window is opened, do stuff.
    while (screen.isOpen())
    {
        // Create game's event and event's types
        sf::Event main_ev;
        while (screen.pollEvent(main_ev))
        {
            if (main_ev.type == sf::Event::Closed) {
                screen.close();
            }
        }

        // Operate with the window
        screen.clear();
        screen.draw(sp_sfml);
        screen.display();
        Sleep(3000);
        screen.draw(sp_titlebg);
        screen.display();
    }

    return 0;
}
 
Title: Re: Problem with the draw & display methods
Post by: DylanMorgan on February 04, 2017, 10:55:36 pm
Don't know if this is any help. Or if it even matters. But you haven't declared anything for your sprite? Position, size, etc.
Title: Re: Problem with the draw & display methods
Post by: fallahn on February 05, 2017, 12:57:52 am
you only need call clear() and display() once with all your drawing in between (from back to front).

screen.clear();
screen.draw(sp_titlebg);
screen.draw(sp_sfml);
screen.display();
 
Title: Re: Problem with the draw & display methods
Post by: gmestanley on February 05, 2017, 01:47:20 am
you only need call clear() and display() once with all your drawing in between (from back to front).

screen.clear();
screen.draw(sp_titlebg);
screen.draw(sp_sfml);
screen.display();
 
Actually, what I want to do is display only the sfml logo and after 3 seconds, display the background, not one behind another.
Don't know if this is any help. Or if it even matters. But you haven't declared anything for your sprite? Position, size, etc.
Well you can declare a sprite without properties, but it'll be on the top of the screen.
Title: Re: Problem with the draw & display methods
Post by: jamesL on February 05, 2017, 04:52:42 am
just use an
if
and a boolean

Code: [Select]

 bool firstTime = false;
 
 while (screen.isOpen())
 {
      blah blah blah

      // Operate with the window
      screen.clear();
      if ( firstTime )
     {
         screen.draw(sp_sfml);
         firstTime = false;
         screen.display();
         sleep(3000);
     }
     screen.draw(sp_titlebg);
     screen.display();
 }

Title: Re: Problem with the draw & display methods
Post by: Hapax on February 05, 2017, 12:38:02 pm
I would say that it would be better to use a clock rather than blocking the window for three seconds. Something like this, maybe:
   
    sf::Clock clock;
    while (screen.isOpen())
    {
        // Create game's event and event's types
        sf::Event main_ev;
        while (screen.pollEvent(main_ev))
        {
            if (main_ev.type == sf::Event::Closed) {
                screen.close();
            }
        }

        // Operate with the window
        screen.clear();
        if (clock.getElapsedTime() < sf::seconds(3))
            screen.draw(sp_sfml);
        else
            screen.draw(sp_titlebg);
        screen.display();
    }

Remember that the whole loop should be constantly and regularly looping with every attempt at processing events as soon as they arrive/happen.