Latest version of sdlticks...

[pascal]
interface

uses
sdl;

type
TSDLTicks = class
private
FStartTime : UInt32;
FTicksPerSecond : UInt32;
FElapsedLastTime : UInt32;
FFPSLastTime : UInt32;
FLockFPSLastTime : UInt32;
public
constructor Create;
destructor Destroy; override; // destructor

{************************************************* ****************************
Init
If the hi-res timer is present, the tick rate is stored and the function
returns true. Otherwise, the function returns false, and the timer should
not be used.
************************************************** ***************************}
function Init : boolean;

{************************************************* **************************
GetGetElapsedSeconds
Returns the Elapsed time, since the function was last called.
************************************************** *************************}
function GetElapsedSeconds : Single;

{************************************************* **************************
GetFPS
Returns the average frames per second.
If this is not called every frame, the client should track the number
of frames itself, and reset the value after this is called.
************************************************** *************************}
function GetFPS : single;

{************************************************* **************************
LockFPS
Used to lock the frame rate to a set amount. This will block until enough
time has passed to ensure that the fps won't go over the requested amount.
Note that this can only keep the fps from going above the specified level;
it can still drop below it. It is assumed that if used, this function will
be called every frame. The value returned is the instantaneous fps, which
will be less than or equal to the targetFPS.
************************************************** *************************}
procedure LockFPS( targetFPS : Byte );
end;

implementation

{ TSDLTicks }
constructor TSDLTicks.Create;
begin
inherited;
FTicksPerSecond := 1000;
end;

destructor TSDLTicks.Destroy;
begin
inherited;
end;

function TSDLTicks.GetElapsedSeconds : Single;
var
currentTime : Cardinal;
begin
currentTime := SDL_GetTicks;

result := ( currentTime - FElapsedLastTime ) / FTicksPerSecond;

// reset the timer
FElapsedLastTime := currentTime;
end;

function TSDLTicks.GetFPS : Single;
var
currentTime, FrameTime : UInt32;
fps : single;
begin
currentTime := SDL_GetTicks;

FrameTime := ( currentTime - FFPSLastTime );

if FrameTime = 0 then
FrameTime := 1;

fps := FTicksPerSecond / FrameTime;

// reset the timer
FFPSLastTime := currentTime;
result := fps;
end;

function TSDLTicks.Init : boolean;
begin
FStartTime := SDL_GetTicks;
FElapsedLastTime := FStartTime;
FFPSLastTime := FStartTime;
FLockFPSLastTime := FStartTime;
result := true;
end;

procedure TSDLTicks.LockFPS( targetFPS : Byte );
var
currentTime : UInt32;
targetTime : single;
begin
if ( targetFPS = 0 ) then
targetFPS := 1;

targetTime := FTicksPerSecond / targetFPS;

// delay to maintain a constant frame rate
repeat
currentTime := SDL_GetTicks;
until ( ( currentTime - FLockFPSLastTime ) > targetTime );

// reset the timer
FLockFPSLastTime := currentTime;
end;
[/pascal]