C++ and Time

Started by
5 comments, last by Buckeye 13 years, 9 months ago
Hi, im looking for a way to make a number increase in ms. that has the format of 1.000, so basicly that would be 1 second and 0.500 would be 500ms.

Id like this number to start increasing from 0 once the application starts.

How can I do this? Ive tried methods like:

int _tmain(int argc, _TCHAR* argv[]){	FILETIME now;	float tempTime = 0;	float curTime = 0;	while(1)	{		GetSystemTimeAsFileTime(&now);		if(now.dwLowDateTime >= tempTime+0.00009f)		{			tempTime = now.dwLowDateTime;			curTime += 0.0001f;		}		std::cout << curTime << std::endl;	}}

But for some reson that is to slow. or I dunno. Is there anything out there that allready does what I need?
Advertisement
DWORD startTime = timeGetTime(); // initialized when app startsfloat ftime;while(..){   ftime = float(timeGetTime()-startTime)/1000.0f;}

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Mhmm, im getting:

unresolved external symbol __imp__timeGetTime@0

#include "stdafx.h"#include <iostream>#include <Windows.h>int main(){	DWORD startTime = timeGetTime(); // initialized when app starts	float ftime = 0;	while(1)	{		ftime = float(timeGetTime()-startTime)/1000.0f;		std::cout << ftime << std::endl;	}	return 0;}



Edit: nvm, I didnt link WINMM.LIB.

Thanks alot!
Not sure on the accuracy of timeGetTime() but I've used QueryPerformanceCounter() and QueryPerformanceFrequency() for this kind of thing.
Quote:Original post by AndyEsser
Not sure on the accuracy of timeGetTime() but I've used QueryPerformanceCounter() and QueryPerformanceFrequency() for this kind of thing.


The accurace on timeGetTime is 1 ms and won't go below it, if you need more accurace QueryPerfomanceTimer is the only way. Make sure to bind it to one thread though otherwise you might get inaccuracies, http://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Isn't there a limitation due to the multitasking nature of the operating system that the accuracy of any timer will only go to about 50ms?
Quote:...the accuracy of any timer will only go to about 50ms?

The discussion isn't really about timers, but requests for the current time.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement