• Advertisement
Sign in to follow this  

timer in c++

This topic is 4585 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

hey.. im new to c++ i was wondering if there was a timer function, or a way to code it, in c++. just like in VB, so i can stop the program for xx sec without having the user hit enter. maybe even some kind of global timer thing, witch could make it possible to tricker events while the game is running.. thx for a helpfull forum guys niux

Share this post


Link to post
Share on other sites
Advertisement
There is a function in the ctime header called time. It returns time_t which is a struct. (A quick Google search will show you what is within it)
Using that within a for loop would do what it sounds like you want.

Share this post


Link to post
Share on other sites
I'm not sure exactly what your asking, but with SDL you can use SDL_GetTicks(); to get the current time in milliseconds.

Share this post


Link to post
Share on other sites
I think he means a sleep function.

#include <ctime>

// delays for a given number of milliseconds
void delay(int m) {
time_t end = clock() + m;

while(clock() != end);
}

int main(int argc, char* argv[])
{
delay(1000); // delays for 1 second; busy waiting
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
skulldrudgery: not to knock you or anything, but thats not a very good Sleep solution. most people call Sleep or usleep not only to cause a pause, but to give some CPU time to other processes or threads. Your Delay function will eat that time up!

So call Sleep on windows and usleep on just about everything else.

-too lazy to log in.

Share this post


Link to post
Share on other sites
skulldrudgery, that looks like a dangerous compare. you're probably never going to get exact numbers from clock() as you calculated in end. maybe while(clock() < end); instead?

niux, since you seem to have used VB I'm going to guess that you're on a windows system and don't really care about cross plattform headaches then you might want to look at Sleep in the windows.h header. Although I personally don't like apps that freeze (sleep) :P

Share this post


Link to post
Share on other sites

::Sleep(1000);




LARGE_INTEGER freq;

if (::QueryPerformanceFrequency(&freq)!=0)
{
dFrequency = (double)freq.QuadPart;
}




LARGE_INTEGER counts;
::QueryPerformanceCounter(&counts);
return counts.QuadPart;

Share this post


Link to post
Share on other sites
http://www.codeproject.com/datetime/perftimer.asp

Has been very usefull for some of my 'real-time' apps..

Lea

Share this post


Link to post
Share on other sites
thanks for all the help guys:D

and yes im on winXP and dont care about crossplatform, yet - im still too newbie :)

-niux

Share this post


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

  • Advertisement