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

Author Topic: Custom sleeping function freezes the game  (Read 2311 times)

0 Members and 1 Guest are viewing this topic.

UglyIgloo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Custom sleeping function freezes the game
« on: September 14, 2014, 11:52:51 pm »
void GSleep
{
        sf::Clock clock;
        sf::Time time = sf::seconds(0);

        while(x > time)
        {
                time = clock.getElapsedTime();
        }
}

This function was created to make the game wait for a certain amount of time before carrying out the next order. Unfortunately, this causes the entire game to freeze until it is done waiting. I only want it to prevent the next line of code from happening until it is done. Thanks.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Custom sleeping function freezes the game
« Reply #1 on: September 15, 2014, 12:01:16 am »
What else would you expect to happen? When you utilize 100% of 1 cpu core with your only thread how do you expect anything else to happen? And no, this is not a recommendation to start with multithreading. Instead redesign your code and don't expect blocking execution with a loop to not stop the rest of your program.

On a side note: The proper way to pause execution and let the system utilize the cpu for something else is by sleeping your thread.
« Last Edit: September 15, 2014, 12:08:38 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

UglyIgloo

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Custom sleeping function freezes the game
« Reply #2 on: September 15, 2014, 12:32:46 am »
How would I utilize less of the CPU?


Quote
The proper way to pause execution and let the system utilize the cpu for something else is by sleeping your thread.


Not entirely sure what you mean by that. I can only guess you're suggesting I use windows.h's "Sleep" function, which yields the same results.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Custom sleeping function freezes the game
« Reply #3 on: September 15, 2014, 07:48:20 am »
It yields the same result from the point og view of your program, but it does not consume 100% CPU.
To achieve what you want you'll need to redesign your code, to not depend on sleeping, as already said.
There are several ways to do what you want. One way would keep track of the time that has passed and then only call the delayed function when enough time has elapsed, then reset the time counter.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Custom sleeping function freezes the game
« Reply #4 on: September 15, 2014, 08:35:55 am »
The difference is that the sleep function tells the operating system that you don't need to do anything for a given period of time, so that the OS does not dedicate processing time to your program until the sleep is complete.

It's also called the idle state of a process.