SFML community forums

Help => Window => Topic started by: lostmemories on July 21, 2020, 09:29:56 pm

Title: [SOLVED] Display window for a few seconds without event polling
Post by: lostmemories on July 21, 2020, 09:29:56 pm
Hi, I am a beginner at SFML and am making a Pokemon copycat. I need help with displaying the exact same window on the screen for a few seconds. For example, when Pokemon use attacks, the text bar will display which attack they used for ~2 seconds. My current "fight menu" (where you select which attack to use) code follows this layout:

while(window.isOpen()){
  while(window.waitEvent(event)){
    switch(event.type){
      case sf::Event::KeyPressed:{
        if(event.key.code == sf::Keyboard::A){ //player selects the attack they are currently hovering over
          window.clear();
          window.draw(everything);
          window.display();
          usleep(2000000); //sleep for 2 seconds so that this window stays on screen
          //opponent uses an attack
          usleep(2000000); //sleep for 2 seconds again for the opponent's attack
          //loop back to player's turn

The problem is that if I do it this way, my program still picks up Events during the 2 seconds of sleep. So if you spam 'A' during the 2 seconds of sleep, the program will immediately repeat the same attack after the opponent's attack ends. How can I stop events from being collected while displaying these extended windows? Or is there a better way to display an extended window? Any help/ideas would be greatly appreciated, thanks!
Title: Re: Display window for a few seconds without event polling
Post by: Stauricus on July 22, 2020, 02:17:38 pm
hi
have you tried
sf::sleep(sf::seconds(2));
instead of
usleep(2000000);
?
Title: Re: Display window for a few seconds without event polling
Post by: lostmemories on July 22, 2020, 05:50:01 pm
hi
have you tried
sf::sleep(sf::seconds(2));
instead of
usleep(2000000);
?

Unfortunately, I tried it just now and it gave the same result :(
Title: Re: Display window for a few seconds without event polling
Post by: Hapax on July 22, 2020, 08:46:01 pm
Instead of stopping the program dead for some time (sleep), you could loop until the time has passed and poll all the events while looping:
const sf::Time freezeLength{ sf::seconds(2.f) };
sf::Clock freezeClock;
while (freezeClock.getElapsedTime() < freezeLength)
{
    sf::Event event;
    window.pollEvent(event);
}

Whether or not you want to process those events and react to them is your own choice but I would recommend at least processing the main ones (e.g. close window).

Note that all events during this "pause" are completely discarded, which seems to be what you wanted.
Title: Re: Display window for a few seconds without event polling
Post by: lostmemories on July 23, 2020, 01:51:31 am
Instead of stopping the program dead for some time (sleep), you could loop until the time has passed and poll all the events while looping:
const sf::Time freezeLength{ sf::seconds(2.f) };
sf::Clock freezeClock;
while (freezeClock.getElapsedTime() < freezeLength)
{
    sf::Event event;
    window.pollEvent(event);
}

Whether or not you want to process those events and react to them is your own choice but I would recommend at least processing the main ones (e.g. close window).

Note that all events during this "pause" are completely discarded, which seems to be what you wanted.

This worked! Thank you so much!