Skip to main content
GameDev.net gamedev.net
🔒 Locked

Counting time

Started by Floru Sep 21, 2000 at 5:53 AM 10 replies 350+ views
Original Post
Floru
Floru
Hi! I''m making a two-player board game. What I would like to do is to count time for both of the players. Like in chess for example. The format would be following hh:mm:ss. So does anybody have suggestions how this should be done (which functions to use)? I''m working under windows. I did take a look at the windows functions but they are quite a confusing... :/ Thanks Floru
deakin
deakin
You could always create your own time manager that just adds one second to the time every 1000 milliseconds (you can get the milliseconds with GetTickCount()) and then adds one to minutes every 60 seconds, etc. This wouldnt be too hard to use if you put it all in a class.

- Daniel
VG Games
- DanielMy homepage
Floru
Floru
Ok... So in the beginning of each player''s turn I have to get the current time and when there is +1000ms I add one second for the player?

Floru
Null and Void
Null and Void
Get the time at the beginning of their turn (with GetTickCount) and at the end of their turn get another GetTickCount. Then subtract the second one from the first and you will have how many milliseconds have passed during their turn.


Null and Void

Floru
Floru

But I''d like to update the time real-time not at end of turn.
Tornado
Tornado
quote:
Original post by Floru

But I''d like to update the time real-time not at end of turn.


That''s not a problem.
Some basic code:

    
int clock = 0; // the time elapsed

DWORD time = GetTickCount();
// Loop starts here

if (GetTickCount() - time >= 1000) // one sec passed

clock++; // add one second to clock

time = GetTickCount();


Now the hours will be: (int)(time / 3600)
The minutes will be: (int)(time / 60)
The seconds will be: time % 60



- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
fifty1
fifty1
I was the anonymous poster.
Forgot to login in the excitement of coding.

I have some (fairly ugly) code that I''m using for a high resolution timer in a game I''m writing. It''s a class with a lot of features you may not need, but I can send you a copy if you''d like.
Houdini
Houdini
What Tornado posted will work, but there is a small problem with it (which you may or may not care about). Inside that loop you are going to have to poll for input, draw graphics, etc, and those take time. Say your tickcount is at 999 (0.9 seconds) and after the next loop iteration it could be 1099 ticks (1.09 seconds). If you just lop off the remainder then you lose time. 0.09 seconds might not sound like much, but after a minute your clock would be off by over 5 seconds.

Anyways, it''s real easy to fix. When a second has passed subtract 1000ms (1 sec) from the time holder and add what is left to the next call to GetTickCount:

    

int clock = 0; // the time elapsed

DWORD time = GetTickCount();

// Loop starts here


if (GetTickCount() - time >= 1000) // one sec passed

clock++; // add one second to clock


// Don''t lose time

time = (time - 1000) + GetTickCount();



Also, one more note. GetTickCount has a resolution of up to 55ms. In other words, the value returned by GetTickCount only changes every 55ms, so it is possible to "lose" 54ms every loop iteration, once again causing your clock to fall behind. If you want your timer to be accurate, then use QueryPerformanceCounter(), otherwise GetTickCount() works fine.


- Houdini
Houdini
Houdini
Houdini
Bah, I hate being beat to the punch. Die fifty1
Houdini
Tornado
Tornado
Well, I said the code was basic!
Houdini's right, If you want to get accurate as possible, use QueryPerformenceCounter().
Another thing, if I'm not mistaken, my code will overflow after ~ 9 hours and 6 minutes.
Just something to think about

Edited by - Tornado on September 21, 2000 2:16:50 PM
Goblineye EntertainmentThe road to success is always under construction
Gladiator
Gladiator
Your best bet is to update your players'' timers by using WM_TIMER. Just start a timer, and increase the time of the players in the WM_TIMER event. It''s not the most accurate thing in the world but it works.

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.