0

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++;
	}
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by hanvyj
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.