Complete locking of frame rate

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Anteater

Complete locking of frame rate

Post by Anteater »

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.
T101
Posts: 44
Joined: Thu Jul 29, 2004 4:41 pm

Post by T101 »

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.
Anteater

Post by Anteater »

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()
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

why not use the timer class in the wiki?
it s really easy
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

here's a (not recommended) way to limit the frame rate to 40 fps (windows only)

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
}
also, since I'm bored in work, here's some shooting code

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
  }
}
Anteater

Post by Anteater »

THX 4 the code. Can I use it in both commercial/noncommercial?
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Of course, if I had objections to you using it, I wouldn't have posted it ;)
Anteater

Post by Anteater »

I'm a little lost here. :oops: Where exactly do I put the frames per second code?
Also, what do I replace whatever [while(whatever)] with?
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

meh. open wide, here comes the aeroplane NEEEYOOOWWWN

read through it til you understand it, it should be pretty obvious
Anteater

Post by Anteater »

OK, now I feel really stupid. :oops: I still can't figure it out. :(
:?:
Post Reply