unit UMain;

interface

uses
  JEN_Header,
  JEN_Math,
  Squall,
  UGame;

const Screen_Width = 1024;
      Screen_Height = 768;

var
  Engine   : IJenEngine;
  Display  : IDisplay;
  Render   : IRender;
  Render2d : IRender2d;
  Utils    : IUtils;
  Game     : TGame;
  Input    : IInput;
  ResMan   : IResourceManager;

  procedure Start;
  function LineVsCircle(const lp1, lp2, cp: TVec2f; Rad: Single): Boolean;
  function CircleVsCircle(const cp1: TVec2f; Rad1: Single;const cp2: TVec2f; Rad2: Single): Boolean;


implementation

function LineVsCircle(const lp1, lp2, cp: TVec2f; Rad: Single): Boolean;
var
  p1, p2  : TVec2f;
  d  : TVec2f;
  a, b, c : Single;
begin
  p1 := lp1 - cp;
  p2 := lp2 - cp;

  d := p2- p1;

  a := d.LengthQ;
  b := 2.0 * (p1.x * d.x + p1.y * d.y);
  c := p1.LengthQ - Sqr(Rad);

  if -b < 0 Then
    Result := c < 0
  else
    if -b < (2.0 * a) Then
      Result := 4.0 * a * c - b * b  < 0
    else
      Result := a + b + c < 0;
end;

function CircleVsCircle(const cp1: TVec2f; Rad1: Single;const cp2: TVec2f; Rad2: Single): Boolean;
begin
    Result := (cp1 - cp2).Length <= Rad1 + Rad2;
end;

procedure Start;
begin
  if(SQUALL_Init(0) < 0) then
  begin
    SQUALL_Free();
    Exit;
  end;

  GetJenEngine(Engine);
  Engine.GetSubSystem(ssDisplay,  IJenSubSystem(Display));
  Engine.GetSubSystem(ssUtils,    IJenSubSystem(Utils));
  Engine.GetSubSystem(ssRender,   IJenSubSystem(Render));
  Engine.GetSubSystem(ssInput,    IJenSubSystem(Input));
  Engine.GetSubSystem(ssRender2d, IJenSubSystem(Render2d));
  Engine.GetSubSystem(ssResMan,   IJenSubSystem(ResMan));
  Display.Init(Screen_width, Screen_Height,9,False);
  Render.Init();
  Render.SetVSync(False);

  Game := TGame.Create();
  Engine.Start(Game);

  Engine := nil;

  SQUALL_Free();
end;

end.
