unconstant framerate

This is a discussion on unconstant framerate within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I'm making yet another game, this time one that constantly refreshes the screen.. the only problem is, it ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    unconstant framerate

    Hi all,

    I'm making yet another game, this time one that constantly refreshes the screen.. the only problem is, it runs faster at times and slower at other times (as in, during the *game*). I've tried a couple of methods, but both kind of suck:

    a) Check the time, store it in a variable, then at the end of the loop check the time and sleep for x minus the time elapsed during the body of the loop.

    -Works ok, but for some reason sometimes freezes the game when I minimize or move the window. I'm suspecting it's because when a variable gets high enough, it loops back into negatives.. then it ends up sleeping for x - (-348723) millisecs (?)

    b) Set a timer, and when the time is up, call a gameMain() function, which resets the timer, etc.

    -Doesn't freeze, but is very slow and inconsistant, thus defeating the purpose. I'm thinking it's because WM_TIMER messages have really low priority.


    Does anybody have any better way (as in low performance cost, doesn't screw up) of evening out the framerate?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    Couldnt you use the first method and stop the loop when your game loses the focus?
    VC++ 6

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    nono, I don't think that's the problem... the problem is sometimes more resources are being used and stuff, so the game slows down, resulting in your ship sometimes going really fast and sometimes at a crawl.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Using GDI is not the best method for games.........Your windows thread is at the mercy of the rest of the system and how busy it is.....sometimes you can be down to a crawl speed...sometimes you are doing ok...

    Remember this is why timing your app from WM_TIMER is not reliable........it might try send the message at the correct interval, but if your thread is constantly losing its timeslice due to lots of activity you will not get the message at the proper interval...and once you get the message it is cued with all the other messages to the window...and this might have a slight distortion effect too..

    Best bet - invest in some time with a graphics library......for example, Direct X allows you to use hardware blitters to send your image to screen faster than the GDI...also memory can be held on the graphics card freeing up your system memory fo other parts of the game......best of all you can set your window to run exclusively.....this stops other windows using up the CPU time and allows your window a lot of extra CPU time that it wouldnt have when runnign in normal mode....

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use something like:
    Code:
    DWORD x = some_value;
    DWORD start, time_elapsed;
    start = GetTickCount();
    .
    //loop
    .
    time_elasped = GetTickCount() - start;
    if (time_elapsed < x)
       Sleep(x-time_elapsed);

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks both of you!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [SDL+OpenGL] Constant framerate code?
    By DARKGuy in forum Game Programming
    Replies: 14
    Last Post: 05-05-2007, 02:08 PM
  2. framerate vs refreshrate
    By hannibar in forum Game Programming
    Replies: 2
    Last Post: 03-15-2005, 03:40 PM
  3. weird framerate
    By psychopath in forum Game Programming
    Replies: 18
    Last Post: 01-03-2005, 06:26 PM
  4. Framerate
    By cgod in forum Game Programming
    Replies: 5
    Last Post: 11-30-2004, 09:33 AM
  5. Is this a good framerate method?
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 07-08-2004, 02:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21