unit uAnimdata;

interface

uses Classes, SysUtils, GR32, Dialogs, Graphics;

type    TAnimdataRecord = packed record
        Frames : array[0..63] of ShortInt;
        Unknown : Byte;
        Count : Byte;
        FrameInterval : Byte;
        FrameStart : Byte;
        end;

type TAnimdata = class
private
        mulStream : TMemoryStream;
public
        Loaded : Boolean;
        constructor Create;
        destructor Free;
        procedure Load( FilePath : String );
        procedure Save( FilePath : String );
        function GetAnimdata( Id : Integer ) : TAnimdataRecord;
        procedure SetAnimdata( Id : Integer; Anim : TAnimdataRecord );
        function Exists( Id : Integer ) : Boolean;
        function GetStartTime( Id : Integer ) : Integer;
        function GetDelay( Id : Integer ) : Integer;
        function GetCount( Id : Integer ) : Integer;
        function GetFrame( Id : Integer; Count : Integer ) : Integer;
end;

var     Animdata : TAnimdata;

implementation

uses uUtilities, StrLib;

constructor TAnimdata.Create;
begin
        mulStream := TMemoryStream.Create;
        loaded := false;
end;

destructor TAnimdata.Free;
begin
        mulStream.Free;
end;

procedure TAnimdata.Load( FilePath : String );
begin
        mulStream.LoadFromFile( FilePath );
        loaded := true;
end;

procedure TAnimdata.Save( FilePath : String );
begin
        mulStream.SaveToFile( FilePath );
end;

function TAnimdata.GetAnimdata( Id : Integer ) : TAnimdataRecord;
var     Pos : Integer;
begin
        Pos := ( Id div 8 )*(8*sizeof(TAnimdataRecord)+4) + ( Id mod 8 )*sizeof(TAnimdataRecord) +4;
        mulStream.Seek( Pos, soFromBeginning );
        mulStream.Read( Result, sizeof( TAnimdataRecord ) );
end;

function TAnimdata.Exists( Id : Integer ) : Boolean;
var     DataRecord : TAnimdataRecord;
begin
        DataRecord := GetAnimdata( Id );
        if (DataRecord.Count > 0) and (DataRecord.FrameInterval > 0) then
                Result := true
        else
                Result := false;
end;

procedure TAnimdata.SetAnimdata( Id : Integer; Anim : TAnimdataRecord );
var     Pos : Integer;
begin
        Pos := ( Id div 8 )*(8*sizeof(TAnimdataRecord)+4) + ( Id mod 8 )*sizeof(TAnimdataRecord) +4;
        mulStream.Seek( Pos, soFromBeginning );
        mulStream.Write( Anim, sizeof( TAnimdataRecord ) );
end;

function TAnimdata.GetStartTime( Id : Integer ) : Integer;
var     Anim : TAnimdataRecord;
        Pos : Integer;
begin
        Pos := ( Id div 8 )*(8*sizeof(TAnimdataRecord)+4) + ( Id mod 8 )*sizeof(TAnimdataRecord) +4;
        mulStream.Seek( Pos, soFromBeginning );
        mulStream.Read( Anim, sizeof( TAnimdataRecord ) );

        Result := Anim.FrameStart;
end;

function TAnimdata.GetDelay( Id : Integer ) : Integer;
var     Anim : TAnimdataRecord;
        Pos : Integer;
begin
        Pos := ( Id div 8 )*(8*sizeof(TAnimdataRecord)+4) + ( Id mod 8 )*sizeof(TAnimdataRecord) +4;
        mulStream.Seek( Pos, soFromBeginning );
        mulStream.Read( Anim, sizeof( TAnimdataRecord ) );

        Result := Anim.FrameInterval;
end;

function TAnimdata.GetCount( Id : Integer ) : Integer;
var     Anim : TAnimdataRecord;
        Pos : Integer;
begin
        Pos := ( Id div 8 )*(8*sizeof(TAnimdataRecord)+4) + ( Id mod 8 )*sizeof(TAnimdataRecord) +4;
        mulStream.Seek( Pos, soFromBeginning );
        mulStream.Read( Anim, sizeof( TAnimdataRecord ) );

        Result := Anim.Count;
end;

function TAnimdata.GetFrame( Id : Integer; Count : Integer ) : Integer;
var     Anim : TAnimdataRecord;
        Pos : Integer;
begin
        Pos := ( Id div 8 )*(8*sizeof(TAnimdataRecord)+4) + ( Id mod 8 )*sizeof(TAnimdataRecord) +4;
        mulStream.Seek( Pos, soFromBeginning );
        mulStream.Read( Anim, sizeof( TAnimdataRecord ) );

        Result := Id+Anim.Frames[Count];
end;

end.
