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

Author Topic: [SOLVED] Display window for a few seconds without event polling  (Read 5161 times)

0 Members and 1 Guest are viewing this topic.

lostmemories

  • Newbie
  • *
  • Posts: 3
    • View Profile
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!
« Last Edit: July 23, 2020, 03:09:18 am by lostmemories »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Display window for a few seconds without event polling
« Reply #1 on: July 22, 2020, 02:17:38 pm »
hi
have you tried
sf::sleep(sf::seconds(2));
instead of
usleep(2000000);
?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

lostmemories

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Display window for a few seconds without event polling
« Reply #2 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 :(

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Display window for a few seconds without event polling
« Reply #3 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

lostmemories

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Display window for a few seconds without event polling
« Reply #4 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!