-
Best performance.
after making a game like "slime volleyball" and pong. I want to make a game under linux so I know how to use images in linux. Before I start I would like to know how to do best frame rate. I want my game to be the same speed on all computers. I found one method of doing this is too have a delay of a certain amount of milliseconds. That is not too good because on every computer it will be the same framerate. If I do not give delay's then on fast computers the game will go fast and slow on slow computers.
I played some games and they are programmed in a way that if the computer has slow framerate then it will move objects more pixels perframe and if the computer has a fast framerate then the objects will move at smaller pixels. What ends up at the end is that the games look like they both are playing at the same speed. How can I do something like this.
Thanx in advance!
-
A simple way is to test at the end of a function how much time is elapsed:
Code:
float start=GetTickCount();
//...
while ((GetTickCount()-start)<33);
That will cause a delay until 33 milliseconds (or whatever GetTickCount() measures time in) has passed.
-
Instead of frame-based movement, look up time-based movement. It adjusts how far objects move based on how much time has passed since the last frame was rendered.
-
I programmed my game engine using a high resolution timer. The high resolution timer doesn't count time, it counts ticks. The system is always running at a certain number of 'ticks' per second. You count the number of ticks between each frame to get a 'timefraction', which is ticksbetweenframes/tickspersec.
Then, once you have this timefraction, you have to setup speeds of objects in relation to seconds. Say you have a ball that moves at 10 feet per second. The amount it moves per frame is:
10 * timefraction
Code:
void FPS_t::Start()
{
LARGE_INTEGER a;
QueryPerformanceFrequency(&a);
tickspersec = a.QuadPart; //this cannot change while system is running
//can fark up on multiprocessor system due to bugs in bios
//but shouldn't usually matter
QueryPerformanceCounter(&a); //initialize number of system ticks at last frame to this
lastframesticks = a.QuadPart;
frames = 0;
unsigned int time = timeGetTime();
LastTimeBufferSwapped = time;
CurrentTime = time;
LastTickTime = a.QuadPart;
secondpassed = false;
}
void FPS_t::UpdateFPS()
{
++frames;
CurrentTime = timeGetTime();
LARGE_INTEGER a;
QueryPerformanceCounter(&a); //current number of ticks
unsigned int b = a.QuadPart; //ticks for this frame
TimeFrac = (b - lastframesticks) * (1/(float)tickspersec);
if(b - LastTickTime >= tickspersec) //if a second has passed then update FPS info
{
secondpassed = 1;
LastTickTime = b;
sprintf(display, "FPS: %d", (int)frames);
SetWindowText(hWnd, display);
frames = 0;
}
else
{
secondpassed = 0;
}
lastframesticks = b;
}
second passed is just true/false
TimeFrac is the time that has passed between frames in seconds. b is the current ticks, lastframeticks is the tickcount at the last frame, and the difference between the two, divided by the ticks per second (well, in this case multiplied by the reciprocal) gives the time fraction.
It looks complicated, but I didn't like the way my timeGetTime() time based system worked. On the other hand, there is no absolute guarantee that all computers will be able to supply you with a high resolution timer (although 98% of the time they will).
So, yeah, that's how I did that.