# Creating timers.

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

## 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 on other sites
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 secondsint seconds = timer % 60;int minutes = (timer / 60) % 60);int hours   = (timer / 60) / 60;printf("%02d:%02d:%02d", hours, minutes, seconds);

• 10
• 10
• 11
• 12
• 11