//DJXEffects v1.0
unit DJXEffects;

interface

uses
  Classes,
  Windows,
  SysUtils,
  DJXClasses,
  DJXUtils,
  Direct3D9,
  D3DX9;

type
  TDJXEffectList = class;
  TDJXEffect = class;

  TDJXEffect = class
  private
    m_DanJetX: Pointer;
    m_Name: string;
    m_Errors: string;
    m_Effect: ID3DXEffect;
  public
    constructor Create; virtual;
    destructor Destroy; override;
    property DanJetX: pointer read m_DanJetX write m_DanJetX;
    property Name: string read m_Name write m_Name;
    property Effect: ID3DXEffect read m_Effect write m_Effect;
    property Errors: string read m_Errors write m_Errors;
    procedure LoadFromFile(f: string); virtual;
    procedure Release; virtual;
  end;

  TDJXEffectList = class(TDJXComponent)
  protected
    m_List: Tlist;
    function getItem(Index: integer): TDJXEffect;
    procedure setItem(Index: integer; Value: TDJXEffect);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property DanJetX: Pointer read m_DanJetX write m_DanJetX;
    property Effects[Index: integer]: TDJXEffect read getItem write setItem;
    function Find(name: string): TDJXEffect;
    function Add(Effect: TDJXEffect): TDJXEffect;
    procedure FreeEffects;
    function LoadEffectFromFile(f: string; NameInList: string): TDJXEffect;
    procedure Init(DanJetX: Pointer); override;
    procedure OnLostDevice; override;
    procedure OnResetDevice; override;
  published
  end;



implementation

uses
  DJX;

//TDJXEffect BEGIN
constructor TDJXEffect.Create;
begin
  inherited Create;
end;
destructor TDJXEffect.Destroy;
begin
  safe_release(m_Effect);
  inherited Destroy;
end;

procedure TDJXEffect.LoadFromFile(f: string);
var
  ErrorBuffer: ID3DXBuffer;
begin
  Release;
  if FAILED(
    D3DXCreateEffectFromFile(
      TDanJetX(m_DanJetX).Device,
      PChar(f),
      nil,
      nil,
      0,
      nil,
      m_Effect,
      @ErrorBuffer
    )
  ) then
  begin
    setlength(m_Errors, ErrorBuffer.GetBufferSize);
    move(PChar(ErrorBuffer.GetBufferPointer)^, m_Errors[1], ErrorBuffer.GetBufferSize);
    safe_release(ErrorBuffer);
    exit;
  end;
end;
procedure TDJXEffect.Release;
begin
  safe_release(m_Effect);
end;
//TDJXEffect END

//TDJXEffectList BEGIN
constructor TDJXEffectList.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  m_List := Tlist.Create;
end;
destructor TDJXEffectList.Destroy;
begin
  FreeEffects;
  m_List.Free;
  inherited Destroy;
end;

function TDJXEffectList.getItem(Index: integer): TDJXEffect;
begin
  result := TDJXEffect(m_List.items[Index]);
end;
procedure TDJXEffectList.setItem(Index: integer; Value: TDJXEffect);
begin
  m_List.items[Index] := Value;
end;

function TDJXEffectList.Find(name: string): TDJXEffect;
var
  i: integer;
begin
  for i := 0 to m_List.Count - 1 do
  if TDJXEffect(m_List.Items[i]).Name = name then
  begin
    result := TDJXEffect(m_List.Items[i]);
    exit;
  end;
  result := nil;
end;
function TDJXEffectList.Add(Effect: TDJXEffect): TDJXEffect;
begin
  m_List.Add(Effect);
  result := Effect;
end;
procedure TDJXEffectList.FreeEffects;
begin
  if m_List <> nil then
  begin
    while m_List.Count > 0 do
    begin
      TDJXEffect(m_List.Items[0]).Free;
      m_List.Delete(0);
    end;
  end;
end;
function TDJXEffectList.LoadEffectFromFile(f: string; NameInList: string): TDJXEffect;
var
  Effect: TDJXEffect;
begin
  Effect := TDJXEffect.Create;
  Effect.m_DanJetX := m_DanJetX;
  Effect.LoadFromFile(f);
  if length(Effect.Errors) > 0 then
  begin
    Effect.Free;
    exit;
  end;
  Effect.Name := NameInList;
  result := Add(Effect);
end;
procedure TDJXEffectList.Init(DanJetX: Pointer);
begin
  inherited Init(DanJetX);
end;
procedure TDJXEffectList.OnLostDevice;
var
  i: integer;
begin
  for i := 0 to m_List.Count - 1 do
  TDJXEffect(m_List.Items[i]).Effect.OnLostDevice;
end;
procedure TDJXEffectList.OnResetDevice;
var
  i: integer;
begin
  for i := 0 to m_List.Count - 1 do
  TDJXEffect(m_List.Items[i]).Effect.OnResetDevice;
end;
//TDJXEffectList END


end.
