how to set a max fps?

Started by
21 comments, last by Jmelgaard 18 years, 6 months ago
i saw a code in the forum which shows how to get the fps. how can i do that in my game no one can go further then 40fps? so if someone got a new good geforce card it wont go too much fast? thanks in advance
Advertisement
You shouldn't need to restrick people's framerates.

What is the problem? It sounds like the palyer's character and NPCs movement are based off of the fps. If so then you should make it so that the distance they move is based off of the time between frames instead of a fixed distance.

Hope that helps
the problom is that one computer run it too much fast
and another is running good
Quote:Original post by mc30900
the problom is that one computer run it too much fast
and another is running good


Hold on a few minutes, I'll post a method for you.
DWORD t;main loop{    t = GetTickCount();   //end of main loop   //25 = ~40 fps   //33 = ~30 fps   //16 = ~60 fps   while (GetTickCount() - t < 25)}
It is a good idea to fix the framerate so that the physics calculations can be done using a constant time-step.

Vampyre_Dark's code shows you how to set up a mid-resolution timer where every 25ms (1000/40) the application is advanced by that much time, then everything's redrawn.

The Win32 API functions QueryPerformanceCounter() and QueryPerformanceFrequency() can be used instead for a high-resolution timer. This time, set up a timer based on double floating point precision, and advance/redraw every 0.025th of a second.

If you are on *NIX, clock() and CLOCKS_PER_SEC can be used for a fairly decent double floating point precision timer.
Quote:Original post by Vampyre_Dark
DWORD t;main loop{    t = GetTickCount();   //end of main loop   //25 = ~40 fps   //33 = ~30 fps   //16 = ~60 fps   while (GetTickCount() - t < 25)}


Instead of using a hardcoded number like 25 (@OP - which btw, is how many milliseconds your program will take per frame), you can also calculate the number for an arbitrary target framerate like this:

const double frames_per_second = 33.5; // could be any framerate you want
const DWORD wait_time = (DWORD)(1000.0 / frames_per_second);

and then use wait_time in the code above in place of the 25, like this:

while(GetTickTime() - t < wait_time);
noo it killed my game now everything ismisplaced and everything is a mass
Quote:Original post by mc30900
noo it killed my game now everything ismisplaced and everything is a mass
Huh? Show us your main loop.


How about like this:

float fixed_time_step;

void setfps(float fps)
{
fixed_time_step = 1.0f / fps;
}



float lastupdate,currenttime;

void serverpoll()
{
lastupdate = currentime;

//update all objects by fixed_time_step
}

void renderscene()
{
//render each object
}

void updategame()
{

while(game_is_on)
{
currenttime = gettime(); //in milliseconds

int missedframes = (currenttime - lastupdate) / fixed_time_step;

for(i = 0;i<

This topic is closed to new replies.

Advertisement