unit Unit1;

interface
             
uses
  Windows,
  Messages,
  SysUtils,
  Variants,
  Classes,
  Graphics,
  Controls,
  Forms,
  Dialogs,
  Math,
  DJX,
  DJXTimer,
  DJXMaterials,
  DJXLights,
  DJXMeshes,
  DJXTextures,
  DJXFonts,
  DJXTexFonts,
  DJXClasses,
  DJXUtils,
  DJXPrimitives,
  DJXRender,
  DJXShaders,
  DJXLandscape,
  DJXEffects,
  DJXFreeCamera,
  Direct3D9,
  D3DX9,
  CSD;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
      MousePos: TPoint; var Handled: Boolean);
    procedure FormShow(Sender: TObject);
  private
    ErrorInitializing: boolean;
  public
    DanJetX1: TDanJetX;
    DJXTimer1: TDJXTimer;
    DJXFontList1: TDJXFontList;
    DJXTexFontList1: TDJXTexFontList;
    DJXMeshList1: TDJXMeshList;
    DJXLightList1: TDJXLightList;
    DJXMaterialList1: TDJXMaterialList;
    DJXShaderList1: TDJXShaderList;
    DJXEffectList1: TDJXEffectList;
    DJXTextureList1: TDJXTextureList;
    procedure OnTimer(Sender: TObject);
    procedure OnProcess(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

uses
  GameEngine;

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Game.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  sl: TStringList;
  i: integer;
begin
  try
    ErrorInitializing := true;
    DanJetX1 := TDanJetX.Create(Form1);
    DanJetX1.Width := 1024;
    DanJetX1.Height := 768;
    DanJetX1.InitParams.BBufferFormat := bbX8R8G8B8;
    DanJetX1.InitParams.DepthStencil := dsD24S8;
    DanJetX1.FullScreen := false;
    DanJetX1.Vsync := true;
    DJXTextureList1 := TDJXTextureList.create(Form1);
    DJXTextureList1.AutoGenerateMipMaps := true;
    DJXMeshList1 := TDJXMeshList.Create(Form1);
    DJXFontList1 := TDJXFontList.Create(Form1);
    DJXTexFontList1 := TDJXTexFontList.Create(Form1);
    DJXLightList1 := TDJXLightList.Create(Form1);
    DJXTimer1 := TDJXTimer.Create(Form1);
    DJXEffectList1 := TDJXEffectList.Create(Form1);
    DJXTimer1.MaxFPS := 0;
    DJXTimer1.OnTimer := Form1.OnTimer;
    DJXTimer1.OnProcess := Form1.OnProcess;

    Game := TGame.Create;
    Game.DanJetX1 := DanJetX1;
    Game.DJXTimer1 := DJXTimer1;
    Game.DJXFontList1 := DJXFontList1;
    Game.DJXTexFontList1 := DJXTexFontList1;
    Game.DJXMeshList1 := DJXMeshList1;
    Game.DJXLightList1 := DJXLightList1;
    Game.DJXMaterialList1 := DJXMaterialList1;
    Game.DJXShaderList1 := DJXShaderList1;
    Game.DJXEffectList1 := DJXEffectList1;
    Game.DJXTextureList1 := DJXTextureList1;

    Game.ShaderEnable := false;
    Game.MusicEnable := false;
    sl := TStringList.Create;
    sl.LoadFromFile('config.txt');
    for i := 0 to sl.Count - 1 do
    begin
      if GetParam(sl[i], ' ', 0) = 'shader_enable' then
      Game.ShaderEnable := boolean(StrToInt(GetParam(sl[i], ' ', 1)));
      if GetParam(sl[i], ' ', 0) = 'music_enable' then
      Game.MusicEnable := boolean(StrToInt(GetParam(sl[i], ' ', 1)));
      if GetParam(sl[i], ' ', 0) = 'fullscreen' then
      DanJetX1.FullScreen := boolean(StrToInt(GetParam(sl[i], ' ', 1)));
    end;
    sl.Free;
  
    ClientWidth := 1024;
    ClientHeight := 768;

    DanJetX1.DepthStencilWidth := DanJetX1.Width;
    DanJetX1.DepthStencilHeight := DanJetX1.Height;

    DanJetX1.Start(form1.Handle);
  finally
    if DanJetX1.Initialized then
    begin
      ErrorInitializing := false;
      DJXTimer1.Enabled := true;
    end;
  end;
end;

procedure TForm1.OnProcess(Sender: TObject);
begin
  try
    Game.Process;
  except
    DJXTimer1.Enabled := false;
    ThrowError('Error: Run time error');
    Close;
  end;
end;

procedure TForm1.OnTimer(Sender: TObject);
begin
  try
    if not DanJetX1.CanRender then exit;
    Game.Render;
  except
    DJXTimer1.Enabled := false;
    ThrowError('Error: Run time error');
    Close;
  end;
end;

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  Game.KDown(Key);
end;

procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  Game.KUp(Key);
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Game.MDown(X, Y, Button);
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Game.MUp(X, Y, Button);
end;

procedure TForm1.FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  Game.WDown;
end;

procedure TForm1.FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
  MousePos: TPoint; var Handled: Boolean);
begin
  Game.WUp;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  if ErrorInitializing then
  begin
    ThrowError('Error: Initialization failed.');
    Close;
  end;
end;

end.
