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

Author Topic: While pollEvent loop blocks program  (Read 1151 times)

0 Members and 1 Guest are viewing this topic.

GentleSlayer

  • Newbie
  • *
  • Posts: 3
    • View Profile
While pollEvent loop blocks program
« on: November 28, 2017, 11:27:28 pm »
Hello!

I started using SFML a week or so ago and I have a little problem that has been bugging me for a while now(but not major enough to stop me from working on my project).
I added a "Logo Screen" where I simply fade in and out an animated picture and switch to the next screen after a few seconds or if a button/mouse click happens.
void logoScreen::show(sf::RenderWindow& app) {
    sf::Texture texture;
    texture.loadFromFile("Images/Logo.png");

    sf::IntRect rectSourceSprite(0, 0, 784, 472);
    sf::Sprite sprite(texture,rectSourceSprite);
    sprite.setOrigin(sprite.getGlobalBounds().width / 2, sprite.getGlobalBounds().height / 2);
    sprite.setPosition(app.getSize().x / 2, app.getSize().y / 2);

    sf::Clock timer;
    sf::Clock animate;
    sf::Event event;
    while (timer.getElapsedTime().asSeconds() < 5.0f) {
        // Doing anything will skip the logo
        while (app.pollEvent(event)) {
            if (event.type == sf::Event::EventType::MouseButtonPressed || event.type == sf::Event::EventType::KeyPressed) {
                return;
            }
        }
        if (animate.getElapsedTime().asSeconds() > 0.1f) {
            if (rectSourceSprite.left == 784 * 4) {
                rectSourceSprite.left = 0;
                if (rectSourceSprite.top < 472 * 5) {
                    rectSourceSprite.top += 472;
                }
            }
            // This is when the logo pauses before fading off
            else if (rectSourceSprite.left == 784 * 2 && rectSourceSprite.top == 472 * 4) {
                // Resume animation when timer reaches 4.8s
                if (timer.getElapsedTime().asSeconds() > 4.8f) {
                    rectSourceSprite.left += 784;
                    rectSourceSprite.top += 472;
                }
               
            }
            else {
                rectSourceSprite.left += 784;
            }
            sprite.setTextureRect(rectSourceSprite);
            animate.restart();
        }

        app.clear(sf::Color(255,255,255,255));
        app.draw(sprite);
        app.display();
    }
    return;
}

The problem is that if I dont move the mouse (or anything) when the timer reaches 5 seconds, the program just waits there for something to happen. Strangely enough, when I'm debbugging the application this problem doesn't occur. I know the problem is the pollEvent call because when I remove it the application works fine (but then I can't skip the logo). I don't understand why it's blocking the loop.
Thank you for you help!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10284
    • View Profile
    • development blog
    • Email
Re: While pollEvent loop blocks program
« Reply #1 on: November 28, 2017, 11:37:44 pm »
Don't put code inside the event loop if you don't want it to only get triggered when there's an event.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

GentleSlayer

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: While pollEvent loop blocks program
« Reply #2 on: November 28, 2017, 11:56:35 pm »
I'm not sure I understand,

I do want the code inside my event loop to only happen when there's an event (which is to skip in this case only if something is pressed).
But I also want the program to flow normally even if there are no events (not sure if I'm clear or not).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10284
    • View Profile
    • development blog
    • Email
Re: While pollEvent loop blocks program
« Reply #3 on: November 29, 2017, 12:22:10 am »
Looks like I read your code wrong. Anyways, if you want to loop to end after 5 seconds you need to use a clock that doesn't get restarted. The animate clock gets restarted inside the loop after every 0.1s and thus never reaches a value higher than five seconds.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/

GentleSlayer

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: While pollEvent loop blocks program
« Reply #4 on: November 29, 2017, 12:25:28 am »
Yes, but my actual loop uses the clock named "Timer", which never gets restarted.