Jump to content
  • Advertisement
Sign in to follow this  
bgoldbeck

Best timer (clock) functions to use.

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

Hello,
I am currently using SDL's timer functions and they work great for me so far. However, the timer eventually will go out of range after (42 days) I believe it was. That does seem like a long time I know, but I was wondering if there are better alternatives like 'ctime' or something. Although on paper it looks to me ctime will share the same 42 days flaw, unless someone knows a workaround.

Share this post


Link to post
Share on other sites
Advertisement

Hello,
I am currently using SDL's timer functions and they work great for me so far. However, the timer eventually will go out of range after (42 days) I believe it was. That does seem like a long time I know, but I was wondering if there are better alternatives like 'ctime' or something. Although on paper it looks to me ctime will share the same 42 days flaw, unless someone knows a workaround.


Do you expect your game to be running on someones machine for more than 42 days? Does SDL's timer work the way you want / need it to?

Share this post


Link to post
Share on other sites
Use a platform-dependent clock. In POSIX systems, I would use this:
#include <time.h>

double get_time_in_seconds() {
struct timespec t;

clock_gettime(CLOCK_REALTIME, &t);

return t.tv_sec + 1e-9 * t.tv_nsec;
}

Share this post


Link to post
Share on other sites
Perhaps i'm being a little eccentric, but I like to not be limited. Which is why I chose SDL and OpenGL in the first place. Otherwise, I would have picked a single platform to work with. For me, as a programmer, I am inquisitive about learning another way that isn't limited to 42 days.

Share this post


Link to post
Share on other sites

Use a platform-dependent clock. In POSIX systems, I would use this:
#include <time.h>

double get_time_in_seconds() {
struct timespec t;

clock_gettime(CLOCK_REALTIME, &t);

return t.tv_sec + 1e-9 * t.tv_nsec;
}



Ooh, I like that. How is that platform dependent though? Would time.h not work the same on multiple OS?

Share this post


Link to post
Share on other sites

Perhaps i'm being a little eccentric, but I like to not be limited. Which is why I chose SDL and OpenGL in the first place. Otherwise, I would have picked a single platform to work with. For me, as a programmer, I am inquisitive about learning another way that isn't limited to 42 days.


It's actually 49 days. :)

If you use SDL to abstract away the differences between platforms, you have to live with the decisions that the SDL writers made, including the fact that the timer function returns a 32-bit number of milliseconds.

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.

Share this post


Link to post
Share on other sites

Ooh, I like that. How is that platform dependent though? Would time.h not work the same on multiple OS?


clock_gettime is not part of C or C++. It is part of the POSIX specification, which is followed by pretty much every OS except for Windows. You need some other way of doing it for Windows.

Share this post


Link to post
Share on other sites

It's actually 49 days. :)

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.


Very good idea alvaro : )
So then it would only be windows machines that crash after "49 Days".

Share this post


Link to post
Share on other sites

[quote name='alvaro' timestamp='1316723757' post='4864842']
It's actually 49 days. :)

I normally keep one module for things that are platform specific. For that I provide a single header file and several implementations. When I've done this, I've often ended up with an implementation for Windows, one for POSIX systems and a fallback implementation for any other system, although I don't know what those systems would be. This might be the only function you need to write in that module.


Very good idea alvaro : )
So then it would only be windows machines that crash after "49 Days".
[/quote]

Thanks. You can write an implementation for Windows around QueryPerformanceCounter. It shouldn't be too hard.

Share this post


Link to post
Share on other sites
This is actually good news for me, because I was thinking of eventually implementing a client/server and the server would be UNIX anyway. Still 49 days is probably a really long time for a server to run and a client would probably be extremely absurd to run for that long.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!