Controlling Speed
- Posted by John DeHope <jwap at TAMPABAY.RR.COM> Nov 03, 1998
- 667 views
I'd like to throw my two cents in on the question of controlling a program's speed. I'm using this method for a game, so that no matter how fast the computer is the game executes at the same speed. The code is shown below... global atom delay_time global atom last_time last_time = 0 delay_time = .05 global procedure delay ( ) while last_time + delay_time > time ( ) do -- nothing end while last_time = time ( ) end procedure before running this you have to put a value into delay_time. This is the maximum number of seconds that the delay() procedure will wait. If the amount of time has already passed it doesn't wait at all, it just keeps going. In the example above I set last_time to zero (I think that is the only logical choice) and delay_time to .05, or 1/20th of a second. If you put a call to delay() in your main program loop you can insure that the program only goes through the loop at most 20 times per second. This code has some shortcomings, I know... 1) What happens when the internal Euphoria time() function has to reset to 0. This would seem to kill my delay() procedure immediately. RDS: How big a number can time() return? 2) This only works if the delay() procedure is imbedded into a main loop that is executing constantly. It does not slow down a program unless it is called relatively constantly. 3) Calling delay() just 1 time won't necessarily cause a delay. If delay() is called milliseconds before last_time + delay_time will be > time() then the procedure will not cause a noticeable pause. For good measure, here is my generic pause routine... global procedure wait ( atom seconds ) atom t t = time() while ( t + seconds ) > time() do -- do nothing end while end procedure John.