unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, QuadEngine, mSettings, Resources, Field, Inputs;

type
  TfMain = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormClick(Sender: TObject);
  private
    FQuadRender: IQuadRender;
    FQuadDevice: IQuadDevice;
    FQuadTimer: IQuadTimer;

    FTime: Single;
    FField: TField;
    FLastMsg: Integer;
    procedure WndProc(var Message: TMessage); override;
  public
    procedure Process(const delta: Double);
    procedure Draw(dt: Double);
  end;

var
  fMain: TfMain;

implementation

{$R *.dfm}

procedure OnTimer(out delta: Double); stdcall;
begin
  fMain.Process(delta);
  fMain.Draw(delta);
  Input.Clear;
end;

procedure TfMain.FormClick(Sender: TObject);
begin
  FTime := 10;
end;

procedure TfMain.FormCreate(Sender: TObject);
begin
  Randomize;
  FTime := 0;
  Cursor := crNone;
  ClientWidth := Settings.WindowWidth;
  ClientHeight := Settings.WindowHeight;

  FQuadDevice := CreateQuadDevice;
  FQuadDevice.CreateRender(FQuadRender);

  FQuadRender.Initialize(Handle, ClientWidth, ClientHeight, False);
  FQuadDevice.CreateTimer(FQuadTimer);

  FQuadTimer.SetInterval(16);
  FQuadTimer.SetCallBack(@OnTimer);

  Textures.LoadTextures(FQuadDevice, FQuadRender);
  Fonts.LoadFonts(FQuadDevice);
  Shaders.LoadShaders(FQuadDevice);


  FField := TField.Create(FQuadRender);

  FQuadTimer.SetState(True);
end;

procedure TfMain.FormDestroy(Sender: TObject);
begin
  FQuadTimer.SetState(False);
  Sleep(500);
  FQuadTimer := nil;
  FQuadRender := nil;
  FQuadDevice := nil;
  FField.Free;
end;

procedure TfMain.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Input.SetMouseDown(Button);
  //FField.MouseDown(Button, True);
end;

procedure TfMain.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
 // FField.SetMouse(X, Y);
end;

procedure TfMain.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Input.SetMouseUp(Button);
  //FField.MouseDown(Button, False);
end;

procedure TfMain.Process(const delta: Double);
begin
  if FTime < 5 then
    FTime := FTime + delta;
  FField.Process(delta);
end;

procedure TfMain.Draw(dt: Double);
begin
  FQuadRender.BeginRender;
  FQuadRender.Rectangle(0, 0, ClientWidth, ClientHeight, $FF000000);

  FQuadRender.SetBlendMode(qbmNone);
  Textures.Table.Draw(0, 0);

  if FTime >= 5 then
    FField.Draw;

  Caption := 'FPS: ' + FormatFloat('##.00', FQuadTimer.GetFPS)
    + ' CPU: ' + FormatFloat('0.00', FQuadTimer.GetCPUload);

  FQuadRender.EndRender;
end;

procedure TfMain.WndProc(var Message: TMessage);
begin
  if not(Message.Msg = FLastMsg) then
  begin
    Input.SetMsg(Message);
    FLastMsg := Message.Msg;
  end;
  inherited WndProc(Message);
end;

end.
