Public Group

# Counting time C++ win32

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

## Recommended Posts

Hi, I need to write a program with time based events, however, I want to count the playtime, as in: - It is now 8.06, I start the game, the game starts counting playtime seconds. - 8.51, program has counted 60*45 seconds so far The reason I need to count the playtime is because I want something to happen at ten minutes, at twenty, twenty five, etcetera. If anyone could shed some light for me on how to get started or maybe some links, that would be very helpful...

##### Share on other sites
GetTickCount returns the time since Windows started, in milliseconds (though beware, it has something in the order of 10s of millisecond resolution).

If you get this time when you start your program, and then subtract the current time from that start time (and divide by 1000), you'll have the number of elapsed seconds since your program started, i.e., something like:
static DWORD StartTime;void InitializeTime(){    StartTime = GetTickCount();}float GetTime(){    return (GetTickCount() - StartTime) / 1000.0f;}

##### Share on other sites
Works like a charm, thank you so much

##### Share on other sites
I'd wrap that into a class so you can use it for multiple events that start at different times. For instance, a player can start an event at a random time in a game, and the time it starts will be variable.

• 9
• 13
• 40
• 15
• 11