English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have been programming for a little while (2 years or so) and I only know a couple languages. I have been wondering for a while how you would program something so that it always runs the same speed on any computer all the time (for instance, all games nowadays don't run faster on faster computers). I have tried googling but have been unable to figure it out. I know about "delay" type statements that basically just eat up a certain amount of processor operations, and ones that delay for a certain length of time, but without knowing how long it took for operations between delays this would not help. I also know of time classes that allow you to measure time, but only to the nearest second (I assume from an internal clock). Maybe I would need a class that allowed to nearest milisecond? Seems like it would be really tricky to implement...

If you know, please let me know :) and if it's too complicated or something to explain here then the broad stroke or a reference is fine

2007-02-14 14:23:05 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

3 answers

Heres an example in pseudocode...

1. store (current time - i) in (d)
2. If (current time) - (d) >= i then
2a. execute (f)
2b. store (current time) in (d)
3. goto 2

Assuming that (current time) and i are both in seconds, (f) will be executed every (i) seconds regardless of the speed of the computer executing it.

Note that if (f) takes longer to execute than (i) seconds, it will execute every loop.

This is different to using delays as you can use the time not executing (f) to execute other things..

The resolution (seconds, milliseconds etc.) of the interval you can use will depend on the platform you are developing for. If you are using a PC, have a look at www.msdn.com.

This is not the only way of doing it but it is one of the more common and easy.

2007-02-14 15:40:02 · answer #1 · answered by Maniaca Esoterica 3 · 1 0

There are two ways: scaling all of your time-sensitive calculations by the last frame's time, or time slicing. Basically, when you being a frame of execution, keep track of time, when that frame ends, get the new time. The difference between new and old time is the frame time. All time sensitive calculations are scaled to this. Many physics engines recommend time slicing, so they always get the same results. In time slicing, if you want to execute in slices of 20 milliseconds, you execute in a loop for each amount of time that passed, so if 43 milliseconds elapsed that frame, it will execute twice with a remainder of 3.

2007-02-15 02:47:58 · answer #2 · answered by Pfo 7 · 1 0

there are programs that can slow thae faster processors ( to run old games, fo instance). http://www.geocities.com/kulhain/

2007-02-14 14:35:59 · answer #3 · answered by BigJohnny 4 · 0 2

fedest.com, questions and answers