Timing in C++

Hello--I'm looking for a little help with using graphics (such as openGL) in C++. I'm using Visual C++ express edition.

I've been using the nehe.gamedev.net tutorials. The problem is--they all run so FAST. I would imagine that any respectable program would need to slow down the operation so that the user could see it. On my computer, a tiny tap of a button makes a 3D object zip by at a bajillion frames per second.

To make something like a game, I'd probably want something in the 30 frames per second range, huh?

I've done some googling. The two ways to slow down program execution I've encountered seem to be these:

GetTickCount and similar clocking functions. These get the number of milliseconds since the program started running. That's cool. But it seems like using it would dramatically waste system resources.

You'd probably write a function that boils down to

while(GetTickCount() < Milliseconds) { };

and use that to delay your program's execution. But hot damn! what a waste of resources! It seems somewhat ridiculous to have my processor check that condition a zillion times.

Now, there's something called GlutTimerFunc(), which seems to simply have the program's execution wait a number of milliseconds, and then executes a function call. However, some people have expressed doubts as to its accuracy. For instance, I have heard that there are some times when it takes way too long to perform the call.

So what I'd like is a happy medium of sorts: I would like someone to please point me in the direction of a function that will delay my program's execution for a period of milliseconds without making my processor go into overdrive. What do game developers use to make their displays work predictably? Should I try this GlutTimerFunc(), or is the best approach the GetTickCount method?

Thank you very much for your time and consideration. Please just point me in the right direction--my googling efforts are starting to come up dry.
For games that will be viewed on a monitor, the minimum is 60 FPS.

http://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759

I don't think the speed of your game should be tied to the frame rate. Notice for example, how a game will run at 100+ FPS yet run at normal speed. I would imagine the frame rate is detected and the speed of things adjusted accordingly.
For games that will be viewed on a monitor, the minimum is 60 FPS.

This isn't true. While there is a minimum framerate before the game becomes laggy and unplayabale (usually around 20-30fps) the framerate will flucuate greatly.

What you want to use is called a "game-loop". This loop runs continiously calling your render function with a parameter of "time passed since last call". This number is then used to determine the distance anythinig is moved etc.

Note; the code below is very old, and for Win32 systems only. But it'll work. You can't copy and paste it because of the constructs specific to my own 3D system, but you should be able to get a working copy for yourself

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
bool CRuntimeController::RunGame() { 
	if (!QueryPerformanceFrequency((LARGE_INTEGER *) &lngPreFreq)) 
		return;  

  // Local Variables
  bool 				bGameRunning 			= true;
  double 			dbTime_Elapsed 		= 0.0;
 	// Get Initial Count Now, This Is Used To Get Time Elapsed
	QueryPerformanceCounter((LARGE_INTEGER *) &lngFirstCount);
 	// Loop
	while (bGameRunning) {
		//is there a msg to process?
		if (PeekMessage(&msgSysMsg, NULL, 0, 0, PM_REMOVE)) {
			// Translate, and Dispatch Message for Processing
			TranslateMessage(&msgSysMsg);
			DispatchMessage(&msgSysMsg);
			
			if (pEngine->GetMode() != GAME)
				bGameRunning = false;
		} else {

			// Get The Second Tick Count For Time Elapsed
			QueryPerformanceCounter((LARGE_INTEGER *) &lngSecondCount);
			// Workout The Time Elapsed
			dbTime_Elapsed = (double) (lngSecondCount - lngFirstCount) / lngPreFreq;
  		// Render    
			if (!pRenderManager->RenderGame(dbTime_Elapsed)) {
				Debug(FATAL, ERROR_RUNCONTROLLER_GAME_FAILED);
				return false;
			}
      // Move the New Count To The Old Count For Next Frame
			lngFirstCount = lngSecondCount;
			
			if ((CRuntimeController::bQuit) || (pEngine->GetMode() != GAME))
				return false;
		}
 	} 

	// return success
	return true;
}
Last edited on
This isn't true. While there is a minimum framerate before the game becomes laggy and unplayabale (usually around 20-30fps) the framerate will flucuate greatly.
60 FPS is where the game is updating at least as fast as the screen. Below this the game is indeed playable, but the movement starts looking choppy and irregular. At 40 and below, fast paced games may become unplayable.
60 FPS is where the game is updating at least as fast as the screen.

Only if your VSync is set to 60. What about people who use a VSync of 75, 85 or even >100?

Have a read through: http://www.100fps.com/how_many_frames_can_humans_see.htm
That'll give you some proper perspective.
Topic archived. No new replies allowed.