unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Forms,
  StdCtrls,trigu,Baseclass,cont,EnemyHeap, ExtCtrls,GI,mission, Controls;

type
  Tmainform = class(TForm)
    PaintBox: TPaintBox;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PaintBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    FL:boolean;
  public
    { Public declarations }
    procedure Idling(Sender: TObject; var Done: Boolean);
    procedure Loop;
    procedure ProcessInput;
    procedure SetNewVolumeDisk(size:int64);
  end;


var
  mainform: Tmainform;

implementation

{$R *.dfm}
var PacingCounter:LongInt;
    Direc:string;
    _Interface:TInterface;
    _count_f:integer;
    _b:boolean;

procedure Tmainform.ProcessInput;
begin
  if _Interface._exit then begin
    Application.OnIdle:=nil;
    FL:=False;
    Application.Terminate;
  end;
  if (GetAsyncKeyState(VK_LEFT) AND $8000)=$8000 then _Interface.chPos(tiLeft);
  if (GetAsyncKeyState(VK_RIGHT) AND $8000)=$8000 then _Interface.chPos(tiRight);
  if (GetAsyncKeyState(VK_UP) AND $8000)=$8000 then _Interface.chPos(tiUp);
  if (GetAsyncKeyState(VK_DOWN) AND $8000)=$8000 then _Interface.chPos(tidown);
end;

procedure FillTextrutka2canvas( Acanvas:Tcanvas; _Texturka:Tbitmap);
var i,ii:integer;
begin
for i:=0 to (Acanvas.ClipRect.Right div _Texturka.Width) do
 for ii:=0 to (Acanvas.ClipRect.Bottom div _Texturka.Height) do
 Acanvas.CopyRect(rect(i*_Texturka.Width,ii*_Texturka.Height,(i+1)*_Texturka.Width,(ii+1)*_Texturka.Height), _Texturka.Canvas ,rect(0,0,_Texturka.Width,_Texturka.Height));
end;

procedure Tmainform.Loop;
begin
	while FL do begin
             ProcessInput;
             _Interface.draw(PaintBox.Canvas,rect(0,0,PaintBox.Width,paintbox.Height));
             
             
             PacingCounter:=GetTickCount;          
        repeat
               Application.ProcessMessages;
        until (GetTickCount-PacingCounter) > 30;
  end;
end;

procedure Tmainform.Idling(Sender: TObject; var Done: Boolean);
begin
 	Loop;
  Done:=true;
end;

procedure Tmainform.FormCreate(Sender: TObject);
var
  Count, Count2: Integer; 
begin
direc:=extractfilepath(application.ExeName);

Randomize;

InitSineArray;
InitCosineArray;

FL:=True;

_Interface:=TInterface.Create;
_Interface.init(direc,mainform.Handle);
_Interface.Direc:=direc;
_Interface.draw(PaintBox.Canvas,rect(0,0,PaintBox.Width,paintbox.Height));
_interface.New_game;

Application.OnIdle:=Idling;
end;

procedure Tmainform.FormDestroy(Sender: TObject);
begin
DestroySineArray;
DestroyCosineArray;
end;

procedure Tmainform.PaintBoxMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
_Interface.MoveCursor(X, Y);
end;

procedure Tmainform.PaintBoxMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
_Interface.ClickCursor(X, Y, Button);
end;

procedure Tmainform.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key=vk_Escape then  _Interface.ClickKey(0);
end;

procedure Tmainform.SetNewVolumeDisk(size:int64);
//var i:integer; s:string;
begin
//s:='Воровать не хорошо!';
//messagedlg(#194#238#240#238#226#224#242#252#32#237#229#32#245#238#240#238#248#238#33,mtinformation,[mbOK],0);
//for i:=1 to length(s) do
//edit1.text:=edit1.text+'#'+inttostr(ord(s[i]));
end;

end.
