unit g_Main;

interface
uses
  g_Data,g_GUI,g_Types,g_World,g_Land,g_Camera,g_Character_Load,g_Monster_Load,g_Utils,
  GPAHeader,SysUtils,
  {$INCLUDE uses.inc};

procedure MainInit();
procedure GameInit();

procedure MainDraw();
procedure MenuDraw();
procedure GameDraw();

procedure MainUpdate();
procedure MenuUpdate();
procedure GameUpdate();

procedure MainDestroy();

implementation


procedure MainInit();
var Dir: string;
begin
  randomize;

  Dir := iz_dir_local_temp_gfx;
  SysUtils.ForceDirectories(Dir);

  Options_Input_Load(@Options, iz_dir_local_sys+'options.txt');
  GPA := GPALoadArchive(PChar(iz_dir_local_gfx+'graphics.gpa'));
  GUI_Create();
  GameInit();
  Game.State := gsMenu;
end;

procedure GameInit();
begin
  Camera_Create(@Camera);
  Camera.Typ := ctFree;
  //Camera.Typ := ctCharView;
  ScrCenterX := Options.ScreenWidth div 2;
  ScrCenterY := Options.ScreenHeight div 2;
  World_Load(@World);
end;

procedure MainUpdate();
begin
  case Game.State of
   gsLoad : begin end;
   gsMenu : MenuUpdate;
   gsGame : GameUpdate;
  end;
end;

procedure MenuUpdate();
begin
  // Time
  CurrentTime := GetTickCount();
  DeltaTime := (CurrentTime-StartTime)/1000;
  StartTime := CurrentTime;
  
  GUI_Menu_Update(DeltaTime);
end;

procedure GameUpdate();
begin
  // Time
  CurrentTime := GetTickCount();
  DeltaTime := (CurrentTime-StartTime)/1000;
  StartTime := CurrentTime;

  // Interface
  GUI_Game_Update(DeltaTime);

  World_Update(@World);

  Camera_Update(@Camera,DeltaTime);
end;

procedure MainDraw();
begin
  case Game.State of
   gsLoad : begin end;
   gsMenu : MenuDraw;
   gsGame : GameDraw;
  end;
end;

procedure MenuDraw();
begin
  gfx.begin_2D_render();
  GUI_Menu_Draw();

  gfx.End_render();
end;

procedure GameDraw();
begin
  World_Draw(@World);

  GUI_Game_Draw();

  gfx.End_render();
end;

procedure MainDestroy();
begin
  World_Destroy(@World);
  GPACloseArchive(false);
  DelDir(iz_dir_local_temp);
end;

end.


