Sign in to follow this  
brechtjah

Counting time C++ win32

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this