program Game;

uses
  SysUtils,
  DGLEngine_header in 'Exports\DGLEngine_header.pas',
  Variables in 'Modules\Variables.pas',
  GameScenes in 'Modules\GameScenes.pas',
  GameEng in 'Modules\GameEng.pas',
  MenuButtons in 'Exports\MenuButtons.pas',
  GameUtils in 'Modules\GameUtils.pas',
  GameMap in 'Modules\GameMap.pas',
  GameObjects in 'Modules\GameObjects.pas';

procedure Init;
begin

 DirectSoundInit;

 LoadTextureFromPackage('..\Data\Textures.dpc','MainFont.bmp',tex_MainFont);
 LoadTextureFromPackage('..\Data\Textures.dpc','bg_1.jpg',tex_bg1,TexDetail);
 LoadTextureFromPackage('..\Data\Textures.dpc','bg_2.jpg',tex_bg2,TexDetail);
 LoadTextureFromPackage('..\Data\Textures.dpc','bg_3.jpg',tex_bg3,TexDetail);

 CreateConsole(tex_MainFont,0.5,20);
 AddStringToConsole('Starting "'+GameVersion+'"...');
 RegisterCommandProcedure('quit',@QuitEngine);
 RegisterCommandValue('drawfps',@DrawFps,1,0);

StartScene(gsInit);
end;

procedure Process;
begin
  ProcessConsole;

  if FNextScene<>gsNone then
  begin
   StartScene(FNextScene);
   FNextScene := gsNone;
  end;

  case FScene of
   gsMenu:ProcessSceneMenu;
   gsGame:ProcessSceneGame;
   gsInit:ProcessSceneInit;
  end;

end;

procedure Draw;
begin

 case FScene of
  gsMenu:DrawSceneMenu;
  gsGame:DrawSceneGame;
  gsInit:DrawSceneInit;
 end;

 Begin2D;
 if DrawFps=1 then DrawText2D(tex_MainFont,2,2,$FFFFFF,'FPS: '+inttostr(GetFPS),255,0.4,18);
 DrawConsole;
 End2D;
end;

begin

 RegProcedure(0,@Draw);
 RegProcedure(1,@Process);
 RegProcedure(2,@Init);

 SetEngineInitParametrs(800,600,32,0,false,false,true,true);

 case strtoint(ReadValueFromIniFile('Settings.ini','Graphics','TexDetail')) of
 1:TexDetail:=2;
 2:TexDetail:=3;
 end;

 case strtoint(ReadValueFromIniFile('Settings.ini','Graphics','GameDetail')) of
 1:GameDetail:=1;
 2:GameDetail:=2;
 end;

 KeyUp:=strtoint(ReadValueFromIniFile('Settings.ini','Input','Forward'));
 KeyDown:=strtoint(ReadValueFromIniFile('Settings.ini','Input','Backward'));
 KeyLeft:=strtoint(ReadValueFromIniFile('Settings.ini','Input','Left'));
 KeyRight:=strtoint(ReadValueFromIniFile('Settings.ini','Input','Right'));


 if strtoint(ReadValueFromIniFile('Settings.ini','Sound','PlayMusic'))=1 then PlayMusicInGame:=true else PlayMusicInGame:=false;

 //40 FPS
 SetGameProcessInterval(25);

 SetCutingPlanes(0.4,100.0);

 ApplicationName(GameVersion);

 ExtractFromPackage('..\Data\Sounds.dpc','Music.mid','Music.mid');
 if PlayMusicInGame then PlayMusic('Music.mid');

 StartEngine;
 DeleteFile('Music.mid');
end.
