Thread: Frame Rate Limiter

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    In my house
    Posts
    29

    Frame Rate Limiter

    Hey guys, my first time posting, sorry if there is a post like this I looked and coudn't find one

    What I need help with is this :

    I am creating a 2D OpenGL strategy game under Windows, and I need help with code that will limit the frame rate of the program, as it is refreshing it well I guess it would be everytime around the game loop (however long that is). I am not sure what code would be helpful to you guys in helping me, but my drawing all happens in the one function which gets called by the game loop.

    Thanks in advance for any help anyone can give me

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You need an accurate timer and the Sleep function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    In my house
    Posts
    29
    Oh ok thanks

    I do have a performance counter active in the program

    thanks again

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Here all of us are trying to eek out more frames and we continually get these posts about limiting frame rate!

    Why does everyone insist on limiting the frame rate?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    That's because we (I'm included in the list) are still on the initial phase of game development, making 2d games that runs at the max speed that cpu can give, and different speed in each pc. I don't think that's a wrong practice; if I can determine that 27fps are a good rate for a game, I set a timer-counter for my first game to limit and unify it's speed, but and I still use it on my (I'm dreaming here) mega virtual 3d game using it as an alert of low framerate.

    Niara

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The game speed should stay the same through delta calculation (time elapsed per frame), not through frame rate limiters.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Ok, but if you let rendering backbuffer-frames in higer speed than swapping backbuffer into screen will cause cutted movements if you want to slow down the game speed.

    Niara

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You mean like waiting for vsync? Shouldn't there be a flag for that somewhere?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    As long as the gameplay is not frame-based, you don't really need to limit the frame rate. But if, for example, you move something a given distance each frame, then either stop doing that or limit the frame rate.

    The trick to limiting the frame rate is to not just delay a fixed period of time after displaying each frame. What you need to do is record the time you start painting the frame, and then after the frame, delay as long as you want, but incorporate the time spent painting the frame.

    Here's the basic idea (SDL code, but you get the idea):
    Code:
    Uint32 before, delay;
    
    while(!game_ended()) {
        before = SDL_GetTicks();
    
        paint_frame();
    
        delay = before + 33 - SDL_GetTicks();
        if(delay > 0) SDL_Delay(delay);
    }
    SDL_GetTicks() is much like clock(): it returns the number of milliseconds the program has been executing for. But you can use any timing function, such as time().

    33 is of course the frame rate. Make it whatever you like.

    SDL_Delay() delays for the given number of milliseconds. If you can't find a function like that you could use something like this, though it would kill your CPU:
    Code:
    while(SDL_GetTicks() < before + 33);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    @Niara:

    Before you insist on wasting precious CPU and GPU horsepower, perhaps a bit of research would be in order. There is no reason to clamp framerates for rendering. There is a reason to clamp physics framerate but I won't go into that here.

    Plain and simple: You should not be wasting cycles.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Hey Bubba, I'll keep in mind your recomanations. And "...I won't go into that here.", you have reason, that isn't my thread.
    I make my first post on that thread because I was remembering that when I started using Allegro (yes, I have already seen that the post is about OGL) there was a timer function and the game speed was controlled via that timer.

    And forget my posts if I have induced to confusion.
    Niara

  12. #12
    Darkness Prevails Dark_Phoenix's Avatar
    Join Date
    Oct 2006
    Location
    Houston, Texas
    Posts
    174
    I found this site that talks about the differences in using FPS vs Frame Time. It is based in DirectX but the ideas could be used for any graphics library.
    The FAQ also has an artical about how to perform timing.
    Using Code::Blocks and Windows XP

    In every hero, there COULD be a villain!

  13. #13
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    He's making a strategy game, so if he has AI in there, frame limiting isn't so bad a thought since you don't want the AI to be working so fast that you'll never be able to keep up with it. Though I would suggest that you can clamp the AI loop instead of the entire game loop.

  14. #14
    C maniac
    Join Date
    Aug 2004
    Location
    Cyberwarping to Middle Earth
    Posts
    154
    SDL_gfx has a nice frame-speed limiter. It's quite useful, and I use it all the time. That is, if you're using the SDL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Animation not working....
    By aquinn in forum C Programming
    Replies: 7
    Last Post: 02-19-2005, 05:37 AM
  2. the effects of textures on my frame rate
    By DavidP in forum Game Programming
    Replies: 37
    Last Post: 10-03-2003, 11:24 AM
  3. Frame Rate
    By Tommaso in forum Game Programming
    Replies: 6
    Last Post: 04-04-2003, 06:40 PM
  4. Lock Frame Rate??
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 06-06-2002, 11:03 PM
  5. Whats a good frame rate?
    By compjinx in forum Game Programming
    Replies: 16
    Last Post: 04-07-2002, 05:31 PM