Jump to content
  • Advertisement
Sign in to follow this  
boboS

Newbie clock thing

This topic is 4677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem on C++. I want to "refresh" a function after 30 sec. I found this time function: --------------------------------- do { end=clock(); } while ((end-start)/CLK_TCK<30); ---------------------------------- that function should run 30 sec; Bu my function is: ------------------------------------- status() { cout<<money; cout<<xp; cout<<citizens; } -------------------------------------- So what I want is that status(); to refresh after 30 sec; (will display money,xp,citizens for 30sec and after that time should draw again same thing) I've tried to implemet the time function over my function bu I dont really know how. Sorry for this dumb question but I'm a newbie. Thanks

Share this post


Link to post
Share on other sites
Advertisement
That while loop will "do nothing" for 30 seconds, yes - while consuming all the CPU power to keep asking what time it is :) What you want to do is leave your refresh() function as is, and in a main "game loop", call it when an appropriate amount of time has elapsed.


int main() {
int start = clock();
while (gameShouldRun) {
doSomethingUseful();
sleep(1); // play nice with other processes
if ((clock() - start) / CLK_TCK < 30) {
// 30 seconds have elapsed, so we call the special update function
status();
// and "reset" the timer.
start = clock();
}
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
That while loop will "do nothing" for 30 seconds, yes - while consuming all the CPU power to keep asking what time it is :) What you want to do is leave your refresh() function as is, and in a main "game loop", call it when an appropriate amount of time has elapsed.
In this case you really do need the delay loop however, since clock often measures the process' elapsed cpu time.
So it's probably a better idea to use time() instead of clock(), and delay with a function like Unix's sleep() or Window's Sleep().

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!