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

Author Topic: Is a call to window.isOpen() required to see anything?  (Read 4072 times)

0 Members and 1 Guest are viewing this topic.

MickeyKnox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Is a call to window.isOpen() required to see anything?
« 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?
« Last Edit: November 21, 2019, 01:12:10 am by MickeyKnox »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11038
    • View Profile
    • development blog
    • Email
Re: Is a call to window.isOpen() required to see anything?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MickeyKnox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: Is a call to window.isOpen() required to see anything?
« Reply #2 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.
« Last Edit: November 21, 2019, 02:33:13 am by MickeyKnox »

Hapax

  • Hero Member
  • *****
  • Posts: 3383
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Is a call to window.isOpen() required to see anything?
« Reply #3 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*