program JSpaceWar2;

uses
  SysUtils,
  ShellApi,
  windows,
  JEN_Header in '..\Include\Delphi\JEN_Header.pas',
  JEN_Math in '..\Include\Delphi\JEN_Math.pas',
  Main in 'Main.pas',
  UDirector in 'UDirector.pas',
  UIntro in 'UIntro.pas',
  UGameScene in 'UGameScene.pas',
  USprite in 'USprite.pas',
  UShopScene in 'UShopScene.pas',
  USpriteSheet in 'USpriteSheet.pas',
  UEnemy in 'UEnemy.pas',
  UWeapon in 'UWeapon.pas',
  UPlayer in 'UPlayer.pas',
  UCollide in 'UCollide.pas',
  UMainMenu in 'UMainMenu.pas',
  Squall in 'Squall.pas';

procedure StartAndWait( Name: PChar );
var
  ProcInfo: PShellExecuteInfo;
begin
   GetMem( ProcInfo, SizeOf( ProcInfo^ ) );
   with ProcInfo^ do begin
      Wnd := 0;
      cbSize := SizeOf( ProcInfo^ );
      lpFile := PChar( Name );
      lpParameters := nil;
      lpVerb := 'open';
      nShow := SW_SHOW;
      fMask := SEE_MASK_DOENVSUBST or SEE_MASK_NOCLOSEPROCESS;
   end;
   try
      Win32check( ShellExecuteEx( ProcInfo ) );
      WaitForSingleObject( ProcInfo.hProcess, INFINITE)
   finally
      if ProcInfo.hProcess <> 0 then
         CloseHandle( ProcInfo.hProcess );
      Dispose( ProcInfo );
   end;
end;

procedure ConfigDisplay;
var
  Xml : IXML;
  SWidth, SHeight: LongInt;
  Name: String;
begin
  StartAndWait('Config.exe');

  Helpers.CreateXMLReader(Xml, 'config.xml');
  SWidth := StrToInt(Xml['window'].Params['width'].Value);
  SHeight := StrToInt(Xml['window'].Params['height'].Value);
  Name := Xml['player'].Params['name'].Value;
  Display.Init(SWidth, SHeight, 60, false);

  Render.Init(gaOpenGL2_x);
  Render2d.ResolutionCorrect(1024, 768);
  Render.SetVSync(False);

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

end;

begin

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

  Randomize;
  ReportMemoryLeaksOnShutdown := True;
  GetJenEngine(True, Engine);

  Engine.GetSubSystem(ssDisplay, IJenSubSystem(Display));
  Engine.GetSubSystem(ssRender, IJenSubSystem(Render));
  Engine.GetSubSystem(ssHelpers, IJenSubSystem(Helpers));
  Engine.GetSubSystem(ssInput, IJenSubSystem(Input));
  Engine.GetSubSystem(ssRender2d, IJenSubSystem(Render2d));
  Engine.GetSubSystem(ssResMan, IJenSubSystem(ResMan));

  ConfigDisplay;


  Game := nil;
  Engine := nil;
  SQUALL_Free();
end.
