• Advertisement
Sign in to follow this  

Creating timers.

This topic is 3572 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 all, This might be a bit of a silly question but im not exactly the brightest coder, but does anyone have a short bit of code that creates a timer. Prefferebly running time like: hh:mm:ss From that code, i should be able to create different types of timers. The easiest method possible would be nice, but any code or advice on where to look will be greatly appreciated. Im using C++ and OpenGL for my application. I already have code in place to output text to the screen. THANKS! :D

Share this post


Link to post
Share on other sites
Advertisement
There are several methods to create a timer.

One is record the current time as the start time at a moment, and then every frame/update take the difference between the current time and that start time. That gives you the time elapsed, which you can then format as hh:mm:ss.

Another method is to set a callback which gets called every second, for instance. In Windows you can use SetTimer, which sends WM_TIMER messages or calls a callback. SDL has timer functions as well.

The time elapsed is typically in seconds, or milliseconds, and formatting that as hh:mm:ss is quite easy, using normal modulo and divisions. For example:
// Assume 'int timer' is in seconds
int seconds = timer % 60;
int minutes = (timer / 60) % 60);
int hours = (timer / 60) / 60;
printf("%02d:%02d:%02d", hours, minutes, seconds);

If you want more concrete advice, give more concrete information about your situation.

Share this post


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

  • Advertisement