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.
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
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);
returnfalse;
}
// Move the New Count To The Old Count For Next Frame
lngFirstCount = lngSecondCount;
if ((CRuntimeController::bQuit) || (pEngine->GetMode() != GAME))
returnfalse;
}
}
// return success
returntrue;
}
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.