{$MODE OBJFPC}
{$MODESWITCH AUTODEREF}
uses
  {$IF Defined(WINDOWS)}
    windows,
  {$ELSEIF Defined(ANDROID)}
    ctypes, UnixType, Linux,
  {$ENDIF}
  dtypes,
  dlog,
  dlog_stderr,
  dstrnum,
  dvector2i,
  dvector2f,
  dvector4f,
  drect2i,
  dstream,
  dstreamfile,
  dtexture2d,
  dtga;

{$IF Defined(WINDOWS)}
function GetTimer: Integer;
var
  T, F: LARGE_INTEGER;
begin
  QueryPerformanceFrequency(@F);
  QueryPerformanceCounter(@T);
  Result:=Trunc(1000*T.QuadPart/F.QuadPart);
end;
{$ENDIF}
{$IF Defined(ANDROID)}
function GetTimer: Integer;
var
  TS: TTimeSpec;
begin
  clock_gettime(CLOCK_MONOTONIC_RAW, @TS);
  Result := TS.tv_sec * 1000 + TS.tv_nsec div 1000000;
end;
{$ENDIF}

procedure Test(const Name: AnsiString; Retry, BufSize: LongInt);
var
  I: LongInt;
  Texture: TTexture2d;
  Start: LongInt;
  Stream: TStreamFile;
begin
  Write(Name, ' BufSize=', BufSize, ' '#9);
  for I := 0 to Retry - 1 do begin
    Start := GetTimer;
    Stream.Init(Name);
    Stream.BufferSize := BufSize;
    if not LoadTexture2d(@Stream, Texture) then begin
      Writeln;
      Writeln(stderr, 'Failed to load ', Name);
      Halt(1);
    end;
    Stream.Done;
    Write(GetTimer - Start, #9);
    Texture.Free;
  end;
  Writeln;
end;

var
  Name: AnsiString;
begin
  Name := 'countdown.tga';
  if ParamStr(1) <> '' then
    Name := ParamStr(1);
  Test(Name, 6, 0);
  Test(Name, 6, 16);
  Test(Name, 6, 8192);
  Test(Name, 6, 0);
  Test(Name, 6, 16);
  Test(Name, 6, 8192);
end.
