Complete locking of frame rate
Complete locking of frame rate
Is there any way to COMPLETELY lock the FPS in an Irrlicht app to say, 40 FPS? I know locking the FPS is the easy way out, but I'd like to still do it that way.
No. Well, not a precise way, anyway. And not easy either.
What you'd need to do is calculate how long everything else has taken and then sleep for the rest of the time slice.
But you won't get precise timing that way, because of latency in swapping buffers (which likely depends on the specific harware in the user's machine) and because the operating system may give the CPU to another process.
You could use a separate thread for the world updates. That should be relatively easy to time right.
But I don't see why you'd want to limit the actual frame rate in the first place.
What you'd need to do is calculate how long everything else has taken and then sleep for the rest of the time slice.
But you won't get precise timing that way, because of latency in swapping buffers (which likely depends on the specific harware in the user's machine) and because the operating system may give the CPU to another process.
You could use a separate thread for the world updates. That should be relatively easy to time right.
But I don't see why you'd want to limit the actual frame rate in the first place.
I've herd of people doing the thing I'm trying to do in this forum. Anyway, I thought of using getTime() and such but how would I actually do a reasonably presise locking of the FPS? The reason I want to is because I'm using timers in animators such as createFlyStraightAnim() but I'm also using ints for things like how much longer until the gun can shoot in an FPS, E.G. shot = shot + 1... when mouse1clicked if shot > 10 shoot()
here's a (not recommended) way to limit the frame rate to 40 fps (windows only)
also, since I'm bored in work, here's some shooting code
Code: Select all
// your main loop
while(whatever)
{
lasttime = time;
time = device->getTimer()->getTime();
if (time-lasttime > 25)
{
printf("timing=broken! last frame was %d ms slow\n",time-lasttime-25);
}
else
{
// you could use a Sleep() here instead if you use windows.h
while(device->getTimer()->getTime() < lasttime+25);
}
// main loop here
}
Code: Select all
if (player_holding_fire && time > next_shoot_time)
{
if (bullets_in_gun > 0)
{
// BANG! we shoot the gun
shoot();
bullets_in_gun--;
next_shoot_time = time + time_between_shots;
}
else
{
// no ammo left, time to reload
next_shoot_time += time_to_reload;
bullets_in_gun = reload(); // returns number of bullets added
}
}