unit uMainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TMain_Form = class(TForm)
    procedure FormShow(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
  public
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  Main_Form: TMain_Form;

implementation

{$R *.dfm}

uses  QuadEngine,
      uSMath,
      uUtils,
      uConst,
      uVar,
      uStateManager,
      uMain,
      uBass;

procedure TMain_Form.AfterConstruction;
var VSVersion : Byte;
    PSVersion : Byte;
begin
  inherited;

  Randomize;

  Quad := CreateQR;

  AppScrW := 800;
  AppScrH := 600;
  AppPixelWidth     := 1 / AppScrW;
  AppPixelHeight    := 1 / AppScrH;
  AppPixelSize[ 1 ] := AppPixelWidth;
  AppPixelSize[ 2 ] := AppPixelHeight;

  setBlurCof( 1.5 );
  setOldTVCof( 1.2 );

  AppScrResi := Vec2i( AppScrW, AppScrH );
  AppScrResf := AppScrResi;
  AppFullScr := False;

  ClientWidth   := AppScrW;
  ClientHeight  := AppScrH;

  Quad.Initialize( Handle, AppScrW, AppScrH, AppFullScr );

  VSVersion := Quad.GetVSVersionMajor;
  PSVersion := Quad.GetPSVersionMajor;
  if ( VSVersion < 2 ) or ( PSVersion < 2 ) then begin
    UseShaders := False;
    ShadersSup := False;
  end else begin
    UseShaders := True;
    ShadersSup := True;
  end;

  Timer := Quad.CreateTimer;
  Timer.SetInterval( 16 );
  Timer.SetCallBack( @AppTimer );

  Timer_2 := Quad.CreateTimer;
  Timer_2.SetInterval( 5 );
  Timer_2.SetCallBack( @AppTimer_2 );

  Init_Sounds( Handle );

  StateManager := cStateManager.Create;

  AppInit;

  Timer.SetState( True );
  Timer_2.SetState( True );
end;

procedure TMain_Form.BeforeDestruction;
begin
  inherited;

  AppFree;
end;

procedure TMain_Form.FormHide(Sender: TObject);
begin
  StateManager.MakeActivate( False );
end;

procedure TMain_Form.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  StateManager.KeyDown( key );
end;

procedure TMain_Form.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if ( Key = K_E ) and ShadersSup then UseShaders := not UseShaders;  

  StateManager.KeyUp( key );
end;

procedure TMain_Form.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  case Button of
    mbLeft  : StateManager.MouseDown( MB_LEFT );
    mbRight : StateManager.MouseDown( MB_RIGHT );
    mbMiddle: StateManager.MouseDown( MB_MIDDLE );
  end;
end;

procedure TMain_Form.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  StateManager.MouseMove( x, y );
end;

procedure TMain_Form.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  case Button of
    mbLeft  : StateManager.MouseUp( MB_LEFT );
    mbRight : StateManager.MouseUp( MB_RIGHT );
    mbMiddle: StateManager.MouseUp( MB_MIDDLE );
  end;
end;

procedure TMain_Form.FormShow(Sender: TObject);
begin
  StateManager.MakeActivate( True );
end;

end.
