program Ballistics;

uses
  Windows,
  Messages,
  _Scene,
  _Forms,
  _Geometry,
  _SysUtils,
  _World,
  QuadEngine,
  Direct3d9 in 'DirectX\Direct3d9.pas',
  DXTypes in 'DirectX\DXTypes.pas',
  _Effects,
  _Resources,
  _Map in '_Map.pas',
  Base in 'Base.pas',
  _Units in '_Units.pas',
  _Camera in '_Camera.pas';

{ $R *.res}

var
  QuadDevice: IQuadDevice;
  Timer: IQuadTimer;
  DeltaT: Single;

{$REGION 'Kill_program'}
procedure KillApplication;
begin                 
  Timer.SetState(false);
  Resources.Destroy;   
  Camera.Destroy;  
  World.Destroy;  
  Manager.Destroy;
  Resources:= nil;
  Timer:= nil;
  Quad:= nil;
  QuadDevice:= nil;
end;
{$ENDREGION}

{$REGION 'Change_state'}
procedure ChangeState;
begin
  Quad.ChangeResolution(Window.Width, Window.Height);
end;
{$ENDREGION}

{$REGION 'Timer'}
procedure OnTimer(out delta: Double); stdcall;
begin
  DeltaT:= delta * 0.02 + DeltaT * 0.98;
//  Window.Title:= IntToStr(Trunc(Timer.GetFPS));
  Camera.Update(DeltaT);
  Manager.Update(DeltaT);
  Manager.Control;

  Quad.BeginRender;
  Manager.Render;
  Quad.EndRender;
end;
{$ENDREGION}

{$REGION 'Create_prog'}
procedure CreateApplication;
begin
  Randomize;

  QuadDevice:= CreateQuadDevice;
  QuadDevice.CreateRender(Quad);
  Quad.Initialize(Window.Handle, Window.Width, Window.Height, False, True);
  //Quad.SetBlendMode(qbmNone);

  Resources:= TResources.Load(QuadDevice);
  Camera:= TCamera.Create(Quad);
  World:= TWorld.Create(Quad);
//  Menu:= TMainMenu.Create(Quad, World);
//  Manager:= TSceneManager.Create(World);
  QuadDevice.CreateTimer(Timer);     

  Timer.SetCallBack(@OnTimer);
  Timer.SetInterval(8);
  Timer.SetState(True);
  //Logo
  Manager:= TSceneManager.Create(
    TLogo.Create(
      TLogo.Create(World, Quad,
      Resources.LogoTexture, Resources.TileTexture), Quad,
    Resources.QuadTexture, Resources.TileTexture));
end;
{$ENDREGION}

begin
  Window:=TWindow.Create(
      'Ballistics',
      @CreateApplication, @KillApplication, @ChangeState{,
      (GetSystemMetrics(SM_CXSCREEN) - 1024) div 2,
      (GetSystemMetrics(SM_CYSCREEN) - 768) div 2,
      1024, 768});
  Window.Initialize;
end.
