Hello everyone.

I'm a bit new to c++. I'm trying to write a game loop (see the code below) and I'm getting some weird behavior with the GetSystemTime().

I ran through it with the debugger and I get things like this:

startTime = 452

endTime = 210

And nothing seems to be over 1000 - so I figure that getSystemTime() literally only gets the time in milliseconds from the last second or something... Anyway, my question is - how the hell can I get the time?

void Game::Run()
{
	SYSTEMTIME systemTime;

	//framerate measuring
	char timerCount=0;
	long fpstimer=0;

	//work out the time each frame needs to take to keep fps
	long frameTime = 1000/this->fps;

	while(true)
	{
		//get the frame start time
		::GetSystemTime(&systemTime);
		long startTime = systemTime.wMilliseconds;

		//perform logic
		this->LogicStep();

		//perform rendering
		this->Render();

		//get the frame end time
		::GetSystemTime(&systemTime);
		long endTime = systemTime.wMilliseconds;

		//wait for the right time!
		long timeTaken = endTime - startTime;
		
		long waitTime = frameTime - timeTaken;
		if (waitTime > 0)
		{
			::Sleep(waitTime);
		}

		//finally keep track of the fps by timing 100 frames
		if (timerCount > 100)
		{
			::GetSystemTime(&systemTime);
			long newfpstimer = systemTime.wMilliseconds;
			double actualFPS = (double)(newfpstimer - fpstimer)/(double)(timerCount*1000);
			printf("FPS: %f\n\n" , actualFPS);

			fpstimer = newfpstimer;
			timerCount = 0;
		}
		timerCount++;
	}
}

that worked fine, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.