unit _Resources;

interface

uses
  QuadEngine,
  Classes,
  uFMOD;

const
  UNITS_SIZE = 128;

  LEVEL_1 = 'Map_1.png';  
  LEVEL_2 = 'Map_2.png';
  LEVEL_3 = 'Map_3.png';
  LEVEL_4 = 'Map_4.png';

  R_BASE      = 0;
  R_ROCKET    = 1;
  R_HELIVINT  = 2;
  R_BLOW      = 3;
  R_FACTORY_1 = 4;
  R_FACTORY_2 = 5;
  R_FACTORY_3 = 6;
  R_UPGRADE   = 7;
  R_TURRET    = 8;
  R_ANTIAIR   = 9;
  R_JETBUE    = 10;
  R_HELIBUE   = 11;
  R_JET       = 13;
  R_HELI      = 12;
  R_TRANSPORT = 14;
  R_RESOURCE  = 15;

type
TTexture = Array of Byte;
PTexture = ^TTexture;

TResources = class
  private
    const PROGRESS_SIZE = 128;
    const TILE_SIZE = 64;
    const PATH = 'Data/';  
  private
    MusicBuffer: Pointer;
    FQuad: IQuadRender;
    procedure PlaySound(const FileName: string);
  public
    Sounds: Boolean;
    Music: Byte;
    Intro,
    QuadTexture: IQuadTexture;
    LogoTexture: IQuadTexture;
    TileTexture: IQuadTexture;     
    BulletTexture: IQuadTexture;
    PortalTexture: IQuadTexture;     
    CrystalTexture: IQuadTexture;
    Font: IQuadFont;
    constructor Load(Quad: IQuadRender);
    destructor Destroy;
    procedure StopMusic;
    procedure PlayIntro;
    procedure PlayMusic(num: Byte);
  end;

var
  Resources:TResources;  
  T: TTexture;
  S: Pointer;

implementation

//TResources
constructor TResources.Load(Quad: IQuadRender);
var
  i,j: Integer;
begin
  Sounds:= true;
  FQuad:= Quad;
  QuadTexture:=  FQuad.CreateAndLoadTexture(0, PATH + 'quad.png');
  LogoTexture:=  FQuad.CreateAndLoadTexture(0, PATH + 'logo.png');
  TileTexture:=  FQuad.CreateAndLoadTexture(0, PATH + 'circle.png');
  BulletTexture:=  FQuad.CreateAndLoadTexture(0, PATH + 'fire.png', 64, 256);
  PortalTexture:=  FQuad.CreateAndLoadTexture(0, PATH + 'portal.png', 256, 256);
  CrystalTexture:=  FQuad.CreateAndLoadTexture(0, PATH + 'hex.png', 256, 256);

  Font:= FQuad.CreateAndLoadFont(PATH + 'Font/Font.png', PATH + 'Font/Font.qef');
  Font.SetIsSmartColoring(True);
end;

procedure TResources.PlaySound(const FileName: string);
var
  Stream : TFileStream;
begin
  StopMusic;
  Stream := TFileStream.Create(FileName,0);
  if Stream <> nil then
  begin
    MusicBuffer := GetMemory(Stream.Size);
    Stream.Read(MusicBuffer^, Stream.Size);
    if uFMOD_PlaySong(MusicBuffer, Stream.Size, XM_MEMORY) = nil then
    begin
      FreeMemory(MusicBuffer);
      MusicBuffer := nil;
    end;
    Stream.Free;
  end;
end;

procedure TResources.StopMusic;
begin
  if MusicBuffer <> nil then
  begin
    uFMOD_PlaySong(nil, 0, 0);
    FreeMemory(MusicBuffer);
    MusicBuffer := nil;
  end;
end;


procedure TResources.PlayIntro();
begin            
  if not Sounds then
    Exit;
  if Music = 4 then
    Exit;
  Music:= 4;
  PlaySound(PATH + 'Music/XFrog.xm');
end;

procedure TResources.PlayMusic(num: Byte);
begin
  if not Sounds then
    Exit;
  if num = Music then
    Exit;
  Music:= num;
  case num of
    0:PlaySound(PATH + 'Music/XFrog.xm');
  end;             
end;


destructor TResources.Destroy;
begin
  QuadTexture:= nil;
  TileTexture:= nil;
  LogoTexture:= nil;
  BulletTexture:= nil;
  CrystalTexture:= nil;
  Font:= nil;
  inherited;
end;

end.
