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

Author Topic: Problem with waiting  (Read 1709 times)

0 Members and 1 Guest are viewing this topic.

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Problem with waiting
« on: July 23, 2014, 05:13:10 pm »
Hello.

Currently, I am working on a program which prints a character and then waits for 100 milliseconds, after which it prints a second character.

However, I also have a... well, let's call it a "box". When the mouse clicks on this box, it changes color.

I have figured most of this out. However, I have a problem due to the wait time. This is, in mostly psuedo-code, what's giving me trouble.

sf::RenderWindow window;

while (!shouldStop)
{
        window.clear(sf::Color::White);
        window.draw(boxSprite_NotSelected);

        // print characters, etc.

        if (boxRectangle.Contains(GetMousePosition())
        {
                if (IsLeftMouseButtonPressedInRectangle())
                { shouldStop = true; window.draw(boxSprite_Selected); }
        }

        window.display();
        sf::sleep(sf::milliseconds(100));
}
 

During the sleep time of 0.1 seconds, if I click on the box, it, obviously, doesn't register. What I want to know is how to fix this. Should I not use sf::sleep? What do I do?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Problem with waiting
« Reply #1 on: July 23, 2014, 05:47:06 pm »
of course, if the loop is sleeping, it will not detect any inputs.
look for sf::Time and sf::Clock to do this. every loop you check if the clock time is 100miliseconds or more, and if it is, you reset the clock and change your character :)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Problem with waiting
« Reply #2 on: July 23, 2014, 05:51:21 pm »
I want it to change characters after pretty much EXACTLY 0.1 seconds, is this possible this way?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Problem with waiting
« Reply #3 on: July 23, 2014, 05:57:36 pm »
yes, it is really precise. i can't test it now, but it would be something like this:

sf::RenderWindow window;
sf::Clock clock;
while (!shouldStop)
{
        window.clear(sf::Color::White);
        window.draw(boxSprite_NotSelected);

        if (clock.getElapsedTime() >= sf::milliseconds(100){
            // print characters, etc.
            clock.restart();
        }

        if (boxRectangle.Contains(GetMousePosition())
        {
                if (IsLeftMouseButtonPressedInRectangle())
                { shouldStop = true; window.draw(boxSprite_Selected); }
        }

        window.display();
        //sf::sleep(sf::milliseconds(100));
}
 
Visit my game site (and hopefully help funding it? )
Website | IndieDB

ineed.help

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: Problem with waiting
« Reply #4 on: July 23, 2014, 05:59:44 pm »
Yes, I tried it out, and it seems to work perfectly! Thanks!

(On a side note, I can't believe I never realized how to do this myself...  :-[)