Can't find my original post, but never mind as it's been updated slightly - added a few new functions, made sure a help file can now be created, and most importantly, the variables are now in their own TYPE.
This code is for making sure all movement is at a fixed speed, independent of processor speed or platform.
// --------------------------------- //
// Project: AppTimer
// Start: Thursday, September 11, 2008
// IDE Version: 5.360
//! This help file contains information for using the AppTiming system, which can be used to give a consistent movement speed
//! independant of the processor speed
//! The original code was for BlitzMax, and was written by Leadwerks
//! Converted to GLBasic by Nicholas Kingsley
//! You call the routines like :
//! initAppTime()
//! speed=updateAppTime()
TYPE tAppTime
AppTime_UPS
AppTime_Iterator
AppTime_CurrentTime
AppTime_PauseStart
AppTime_Speed
AppTime_DesiredLoopTime
AppTime_LastUpdateTime
AppTime_LastUPSTime
AppTime_DesiredFrequency%
ENDTYPE
GLOBAL _appTime AS tAppTime
//! This function initialises the AppTimer system, and must be called first.
//\param frameRate% - This is the frame rate that you want all movement to be based on. It defaults to 60 FPS, which is the lowest rate that should be used.
//\return Nothing is returned
FUNCTION initAppTime:frameRate%=60
_appTime.AppTime_UPS=0
_appTime.AppTime_Iterator=0
_appTime.AppTime_CurrentTime=0
_appTime.AppTime_PauseStart=0
_appTime.AppTime_Speed=1.0
_appTime.AppTime_DesiredLoopTime=1000.0/60.0
_appTime.AppTime_LastUpdateTime=0
_appTime.AppTime_LastUPSTime=0
_appTime.AppTime_DesiredFrequency%=frameRate%
ENDFUNCTION
//! This function updates the timing system. It needs to be called once per loop.
//\param No parameters are passed
//\return A movement value is returned. This should be muliplied by the amount you want to move. If the routine is paused, then 0.0 is returned
FUNCTION updateAppTime:
LOCAL time
LOCAL elapsed
IF _appTime.AppTime_PauseStart<>0
RETURN 0.0
ENDIF
_appTime.AppTime_DesiredLoopTime=1000.0/_appTime.AppTime_DesiredFrequency%
time=GETTIMERALL()
IF _appTime.AppTime_LastUpdateTime=0
_appTime.AppTime_Speed=1.0
_appTime.AppTime_LastUpdateTime=time
_appTime.AppTime_CurrentTime=time
_appTime.AppTime_LastUPSTime=time
ELSE
elapsed=time-_appTime.AppTime_LastUpdateTime
IF elapsed=0.0
elapsed=1.0
SLEEP 1
INC time,1.0
ENDIF
_appTime.AppTime_Speed=elapsed/_appTime.AppTime_DesiredLoopTime
_appTime.AppTime_CurrentTime=time
_appTime.AppTime_LastUpdateTime=time
ENDIF
INC _appTime.AppTime_Iterator,1
IF _appTime.AppTime_CurrentTime-_appTime.AppTime_LastUPSTime>=1000
_appTime.AppTime_UPS=_appTime.AppTime_Iterator/((_appTime.AppTime_CurrentTime-_appTime.AppTime_LastUPSTime)/1000)
_appTime.AppTime_LastUPSTime=_appTime.AppTime_CurrentTime
_appTime.AppTime_Iterator=0
ENDIF
RETURN _appTime.AppTime_Speed
ENDFUNCTION
//! This function is used when your program is paused or not. If it is, then the step speed is still updated
//\param pause% - If this is TRUE, then your program is paused. If FALSE, then it isn't
//\return Nothing is returned
FUNCTION pauseUnPauseAppTime:pause%
LOCAL elapsed
IF pause%=TRUE
IF _appTime.AppTime_PauseStart=0.0
_appTime.AppTime_PauseStart=GETTIMERALL()
_appTime.AppTime_UPS=0
_appTime.AppTime_Speed=0
ENDIF
ELSE
IF _appTime.AppTime_PauseStart<>0.0
IF _appTime.AppTime_LastUpdateTime<>0.0
elapsed=GETTIMERALL()-_appTime.AppTime_PauseStart
INC _appTime.AppTime_LastUpdateTime,elapsed
ENDIF
_appTime.AppTime_PauseStart=0.0
updateAppTime()
ENDIF
ENDIF
ENDFUNCTION
//! This function returns the time taken to complete a loop
//\param No parameters are passed
//\return Time taken (in milliseconds) to complete a loop
FUNCTION AppTime:
RETURN _appTime.AppTime_CurrentTime
ENDFUNCTION
//! This function returns the frame rate
//\param No parameters are passed
//\return Frame rate
FUNCTION AppSpeed:
RETURN _appTime.AppTime_Speed
ENDFUNCTION
//! This function returns the updates per second, and is an approximation
//\param No parameters are passed
//\return Updates per second
FUNCTION UPS:
RETURN _appTime.AppTime_UPS
ENDFUNCTION