SFML community forums

Help => Window => Topic started by: MickeyKnox on November 21, 2019, 01:00:10 am

Title: Is a call to window.isOpen() required to see anything?
Post by: MickeyKnox on November 21, 2019, 01:00:10 am
I've written minimal program to display an image:

int main()
{
    sf::Texture texture;
    texture.loadFromFile("foo.jpg");
    sf::Sprite sprite(texture);

    sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "foo");
    window.draw(sprite);
    window.display();

    usleep(1000000);

    return EXIT_SUCCESS;
}
 

This doesn't display anything. Of course, when I replace usleep with:

    sf::Event event;

    while (window.isOpen())
    {
        window.waitEvent(event);
    }
 

I can see my picture. However, when I omit the call to window.waitEvent(), I don't see anything again. Calling only window.waitEvent(), not inside the window.isOpen() loop, doesn't display anything either.

My question is why? Is window.isOpen() or window.waitEvent() doing something hidden in the background that is neccessary to display anything?
Title: Re: Is a call to window.isOpen() required to see anything?
Post by: eXpl0it3r on November 21, 2019, 01:12:49 am
Can you please post one thread instead of three duplicates? ::)

You always have to handle events. Either with poll or wait event, but you have to do the call.
Otherwise your application will be marked as unresponsive by the OS and eventually killed.

What the side effects are for not calling it, are not really important, as it's more or less undefined behavior.

Also, I don't recommend to do just one draw call, you're really not losing anything, by writing a normal "game loop" and draw the image a few times.
Title: Re: Is a call to window.isOpen() required to see anything?
Post by: MickeyKnox on November 21, 2019, 01:46:51 am
I received an error when I was trying to post my question, something about setAuthor. I tried a few times. So they all came through, albeit the error? Sorry about that.

I've send Laurent a message about it quoting the error.


Back to my question: When I replace usleep with just:

    sf::Event event;
    window.waitEvent(event);
 

I don't see the picture either.
Title: Re: Is a call to window.isOpen() required to see anything?
Post by: Hapax on December 01, 2019, 11:44:14 pm
When I replace usleep with just:

    sf::Event event;
    window.waitEvent(event);
 

I don't see the picture either.

I tried this version that you mention and it works for me (very slightly adjusted):
(click to show/hide)

One thing to note, though, is that if any events are detected, the window will immediately close. It may be getting events that you didn't realise (for example, if your mouse is above the window when it opens, which is likely because you are using the desktop mode). It also may be closing quickly or even shut it down before it finishes opening, maybe.

Try my version above that creates a smaller window and keep your mouse out of the way to see if that helps.