TGC Codebase Backup



Frame Rate Independant Code Done Simple. by Mage

6th Jun 2007 15:39
Summary

Framerate independent code. An ultra simple method for making games run the same speed no matter what the frame rate is. Even if the framerate jumps around.



Description

Framerate independant code.

FrameTimer() is put in your main loop (all loops with sync in it). This updates the timing.

If you are moving objects at a certain pace say Move Object 1, 2.5 multiply the distance by FrameX#. It will compensate for the framerate not being 60. Infact multiply FrameX# with anything that is affected by a change in frame rate. IE: Move Object 1, 2.5 * FrameX#

If you need the game to pause at all, perhaps for a menu. Run FrameTimer_Reset() right before returning to the gameplay. This will reset the timing.


FrameX# is a ratio based on the current fps. If frame rate is 60 then FrameX# = 1. If Framerate is 120 then FrameX# = 0.5. If frame rate is 30 then FrameX# = 2.
It will make sure everything is moving at a consistent speed.

Framerate is calculated per frame, not using the built in method.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Dim Master_FrameTimer#(0)
Dim Master_FrameLast(0)
Global FrameX#

Do
   FrameTimer()
   `PUT MAIN LOOP CODE HERE ////
   Sync
Loop

Function FrameTimer()
   Diff# = ABS(Timer() - FrameLast(0))
   Ideal# = 1000.0 / 60.0
   FrameX# = Diff# / Ideal#
   If Diff# > 1000 then FrameX# = 0
   FrameLast(0) = Timer()
EndFunction

Function FrameTimer_Reset()
   FrameLast(0) = Timer()
EndFunction