Slowing down programs

This is a discussion on Slowing down programs within the Windows Programming forums, part of the Platform Specific Boards category; I have ran into another problem... If I handle my messages in pure loops, it goes all too fast. Anyone ...

  1. #1
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    Slowing down programs

    I have ran into another problem...

    If I handle my messages in pure loops, it goes all too fast.
    Anyone know a good way to make the game go in a normal speed, which can be about machine independent, and without slowing other parts of the program?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    38
    I dont know what your asking, but possibly a timer? Im not exactly sure how to use them, i think you catch the WM_TIMER message, have someone else here answer that :-p

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You could use a sleep() function, that allows you to pause your program for a certain amount of time (usually in milliseconds), and often uses the spare time to work on other CPU tasks. There are a number of different sleep() functions out there, so just look for one that statisies your requirements from machine indpendency. Just place that in your loop and pause for however long you think it should pause for. Be sure to make this amount adjustable, as processor speed will vary from machine to machine.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    > Anyone know a good way to make the game go in a normal speed
    Which graphics library are you using?
    Some of them allow you to synchronise your program to the monitor refresh rate, so that your game progresses at a known rate.

    Or say for example that your character has a velocity of 100 pixels per second.
    If a game loop takes 100ms, then you move the character 10 pixels.
    If a game loop takes 500ms, then you move the character 50 pixels.
    The end result is, the quicker your program loops, the smaller the move your character makes on each iteration of the loop.

  5. #5
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    I use OpenGL for my game-part, together with pure Win32API.

    What are the goodies and what are the drawbacks using
    WM_TIMER and Sleep() ?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    The correct way of doing this is NOT a timer or a sleep() function. To make your timing machine independent, you need to pass a delta value to your render function. This delta value is the amount of time that has passed since the last time your render function was called.

    This way the timing will be the same on all machines, but faster machines will get better fps than slower machines.

    If you want to limit the fps, then you just need to wait until the delta value reaches a preset number before you call the render function.

  7. #7
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    good idea
    Got any links or such how to bring it into life?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting programs
    By Molokai in forum C Programming
    Replies: 1
    Last Post: 04-16-2009, 11:10 AM
  2. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 07:32 AM
  3. How come Dev C++ cant run some programs??
    By Sephiroth in forum C Programming
    Replies: 41
    Last Post: 09-17-2005, 06:35 AM
  4. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 04:12 AM

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