This is my conversion attempt, but I'm sure it won't compile under Linux/Mac OSX as I don't know the correct units to include for those platforms:


Code:
unit xeEngine_timer;

interface

{$I xeEngine_include.inc}

procedure Timer_Init;                        cdecl; // initialize timer (like constructor)
procedure Timer_Start;                       cdecl; // start timer
procedure Timer_Stop;                        cdecl; // stop the timer
function  Timer_GetElapsedTime: Double;      cdecl; // get elapsed time in second
function  Timer_GetElapsedTime_Sec: Double;  cdecl; // get elapsed time in second (same as getElapsedTime)
function  Timer_GetElapsedTime_mSec: Double; cdecl; // get elapsed time in milli-second
function  Timer_GetElapsedTime_uSec: Double; cdecl; // get elapsed time in micro-second

implementation

uses
{$ifdef windows}
  windows
{$endif}
  ;

var
  startTimeInMicroSec : Double;  // starting time in micro-second
  endTimeInMicroSec   : Double;  // ending time in micro-second
  stopped             : Boolean; // stop flag
{$ifdef windows}
  frequency           : Int64;   // ticks per second
  startCount          : Int64;   //
  endCount            : Int64;   //
{$else}
  startCount          : timeval; //
  endCount            : timeval  //
{$endif}

procedure Timer_Init;
begin
{$ifdef windows}
  QueryPerformanceFrequency(frequency);
  startCount := 0;
  endCount   := 0;
{$else}
  startCount.tv_sec  := 0;
  startCount.tv_usec := 0;
  endCount.tv_sec    := 0;
  endCount.tv_usec   := 0;
{$endif}

  stopped             := False;
  startTimeInMicroSec := 0;
  endTimeInMicroSec   := 0;
end;

procedure Timer_Start;
begin
  stopped := False; // reset stop flag
{$ifdef windows}
  QueryPerformanceCounter(startCount);
{$else}
  gettimeofday(startCount, nil);
{$endif}
end;

procedure Timer_Stop;
begin
  stopped := True; // set timer stopped flag

{$ifdef windows}
  QueryPerformanceCounter(endCount);
{$else}
  gettimeofday(endCount, NULL);
{$endif}
end;

function  Timer_GetElapsedTime: Double;
begin
  Result := Timer_GetElapsedTime_Sec;
end;

function  Timer_GetElapsedTime_Sec: Double;
begin
  Result := Timer_GetElapsedTime_uSec * 0.000001;
end;

function  Timer_GetElapsedTime_mSec: Double;
begin
  Result := Timer_GetElapsedTime_uSec * 0.001;
end;

function  Timer_GetElapsedTime_uSec: Double;
begin
{$ifdef windows}
  if not stopped then
    QueryPerformanceCounter(endCount);

  startTimeInMicroSec := startCount * (1000000.0 / frequency);
  endTimeInMicroSec   := endCount   * (1000000.0 / frequency);
{$else}
  if not stopped then
    gettimeofday(endCount, nil);

  startTimeInMicroSec := (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
  endTimeInMicroSec   := (endCount.tv_sec   * 1000000.0) + endCount.tv_usec;
{$endif}

  Result := endTimeInMicroSec - startTimeInMicroSec;
end;

end.
What do you guys/gals think?

cheers,
Paul