{*******************************************************}
{                                                       }
{       Small Game Engine                               }
{                                                       }
{       Copyright (C) 2011 IdeyaX studio                }
{                                                       }
{*******************************************************}

unit GM_Menu;

interface
     uses
     OpenGl,
     Windows,
     GM_Vars,
     Classes,
     Math,
     SysUtils,
     GM_Scene;

procedure GameProcces;
procedure GameRender;
Procedure GameInit;
Procedure GameClose;

///
procedure ProccesGame();
procedure ProccesMenu();
procedure ProccesOptions();
procedure ProccesExit();
procedure ProccesLogo();

procedure DrawGame();
procedure DrawMenu();
procedure DrawOptions();
procedure DrawExit();
procedure DrawLogo();

implementation
        uses
         IDX_Engine;

procedure ProccesLogo();
begin

 if (GetTickCount-MyLogoTimer)> 10 then
 begin
   MyLogoTimer:=GetTickCount;
   if MyLogoState=0 then
   begin
     inc(MyLogoAlpha);
     if MyLogoAlpha>255 then
     begin
       MyLogoState:=1;
     end;
   end;

   if MyLogoState=1 then
   begin
     dec(MyLogoAlpha);
     if MyLogoAlpha<10 then
     begin
       MyLogoState:=2;
       MyLogoAlpha:=0;
       GameStatus:=sGMenu;
     end;
   end;

 end;




end;

procedure DrawLogo();
begin
  IDXENG_SetBlendMode(FX_BLEND_NORMAL);
  IDXENG_Draw2d_Texture(gameWND.DWidth,gameWND.DHeight,256,256,MyLogoGFX,0,True,$FFFFFF,MyLogoAlpha);
end;

procedure ProccesExit();
begin
 if gameWND.Keys[VK_Space]or gameWND.Keys[VK_ESCAPE] then
   begin
     gameWND.Keys[VK_Space]:=False;
     gameWND.Keys[VK_ESCAPE]:=False;
     GameStatus:=sGMenu;
   end;

 if gameWND.Mouse.bLeft then
 begin
    gameWND.Mouse.bLeft:=False;

  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth-155,gameWND.DHeight+40-20,40,28) then
  GameStatus:=sGMenu;

  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth+45 ,gameWND.DHeight+40-20,30,28) then
  begin
    //GameClose;
    gameWND.isFinished:=True;
  end;


 end;

end;

procedure DrawExit();
var
  c:Integer;
begin
DrawMenu;
  IDXENG_SetBlendMode(FX_BLEND_NORMAL);
  IDXENG_Draw2d_Rectangle(0,0,gameWND.Width,gameWND.Height,0,100,0,True);

  IDXENG_Draw2d_Text(MySysfont[2],'Вы действительно хотите выйти из игры?',gameWND.DWidth-200 ,gameWND.DHeight-20,$CDCDCD,255);

  c:=$CDCDCD;

  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth-155,gameWND.DHeight+40-20,40,28) then c:=$FFFFFF;
  IDXENG_Draw2d_Text(MySysfont[2],'Нет',gameWND.DWidth-150 ,gameWND.DHeight+40,c,255);

  c:=$CDCDCD;

  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth+45 ,gameWND.DHeight+40-20,30,28) then c:=$FFFFFF;
  IDXENG_Draw2d_Text(MySysfont[2],'Да',gameWND.DWidth+50 ,gameWND.DHeight+40,c,255);

end;




procedure ProccesOptions();
begin
 if gameWND.Keys[VK_Space]or gameWND.Keys[VK_ESCAPE] then
   begin
     gameWND.Keys[VK_Space]:=False;
     gameWND.Keys[VK_ESCAPE]:=False;
     GameStatus:=sGMenu;
   end;

 if gameWND.Mouse.bLeft then
 begin
    gameWND.Mouse.bLeft:=False;

   if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth-25,gameWND.DHeight-20-20,120,28) then
   begin
    gameSound.PlayMusic:=not gameSound.PlayMusic;

    IDXSound_PauseMusic(level0song,not gameSound.PlayMusic);
    
//    if playingmusic=False then StopMusic;
//    if playingmusic=True then PlayMusic('music.mp3');
   end;

  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth-25,gameWND.DHeight+30-20,120,28) then
  begin

    gameSound.PlaySound:=not gameSound.PlaySound;
  end;



  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth-125,gameWND.DHeight+100-20,70,28) then GameStatus:=sGMenu;
  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth+55,gameWND.DHeight+100-20,105,28) then
  begin
   IDXENG_Options_Save;
   GameStatus:=sGMenu;
  end;
 end;
  
end;

procedure DrawOptions();
var
  c:Integer;
  s:string;
begin
 DrawMenu;
 IDXENG_SetBlendMode(FX_BLEND_NORMAL);

  IDXENG_Draw2d_Rectangle(0,0,gameWND.Width,gameWND.Height,0,100,0,True);

  IDXENG_Draw2d_Text(MySysfont[2],'Музыка',gameWND.DWidth-100 ,gameWND.DHeight-20,$CDCDCD,255);
  IDXENG_Draw2d_Text(MySysfont[2],'Звуки',gameWND.DWidth-100,gameWND.DHeight+30,$CDCDCD,255);

 s:='- Выключена';
 if gameSound.PlayMusic then s:='- Включена';
   c:=$CDCDCD;
  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth-25,gameWND.DHeight-20-20,120,28) then c:=$FFFFFF;
  IDXENG_Draw2d_Text(MySysfont[2],s,gameWND.DWidth-20 ,gameWND.DHeight-20,c,255);

 s:='- Выключены';
 if gameSound.PlaySound then s:='- Включены';
    c:=$CDCDCD;

  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth-25,gameWND.DHeight+30-20,120,28) then c:=$FFFFFF;
  IDXENG_Draw2d_Text(MySysfont[2],s,gameWND.DWidth-20,gameWND.DHeight+30,c,255);

  c:=$CDCDCD;
  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth-125,gameWND.DHeight+100-20,70,28) then c:=$FFFFFF;
  IDXENG_Draw2d_Text(MySysfont[2],'Назад',gameWND.DWidth-120,gameWND.DHeight+100,c,255);

  c:=$CDCDCD;
  if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.DWidth+55,gameWND.DHeight+100-20,105,28) then c:=$FFFFFF;
  IDXENG_Draw2d_Text(MySysfont[2],'Применить',gameWND.DWidth+60,gameWND.DHeight+100,c,255);

end;


procedure ProccesMenu();
var
  i:Integer;
begin

 if gameWND.Keys[VK_ESCAPE] then
 begin
      gameWND.Keys[VK_ESCAPE]:=False;
      if PauseGame then
      begin
       GameStatus:=sGame;
       PauseGame:=False;
       Exit;
      end;  

 end;

  if gameWND.Mouse.bLeft then
  begin
    gameWND.Mouse.bLeft:=False;

   for i:=0 to High(Titul_menu) do
    begin
     if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.Width-205 ,gameWND.DHeight+i*30-20,205,28) then
     begin

       if Titul_menu[i]='Новая игра' then
       begin
         PauseGame:=False;
         GameStatus:=sGame;
         Scene_Reset;
         Break;
       end;

       if Titul_menu[i]='Настройки' then
       begin
         GameStatus:=sOptions;
         Break;
       end;

       if Titul_menu[i]='Рекорды' then
       begin
         GameStatus:=sRecords;
         Break;
       end;


       if Titul_menu[i]='Автор' then
       begin
         GameStatus:=sAuthor;
         Break;
       end;

       if Titul_menu[i]='Выход' then
       begin
         GameStatus:=sExit;
         Break;
       end;


     end;
    end;
  end;
end;

procedure DrawMenu();
var
 i,c:Integer;
begin

 IDXENG_SetBlendMode(FX_BLEND_NORMAL);

 IDXENG_Draw2d_Texture(0,0,gameWND.Width,gameWND.Height,TitulGFX);

  for i:=0 to High(Titul_menu) do
  begin
   //c:=$FF0000;
   if GameStatus=sGMenu then
   if IDXENG_PointInRect(gameWND.Mouse.X,gameWND.Mouse.Y,gameWND.Width-205 ,gameWND.DHeight+i*30-20,205,28) then //c:=$FF;
      IDXENG_Draw2d_Rectangle(gameWND.Width-175 ,gameWND.DHeight+i*30-20,175,28,$FFFFFF);

   IDXENG_Draw2d_Text(MySysfont[2],Titul_menu[i],gameWND.Width-140 ,gameWND.DHeight+i*30,$CDCDCD,255);
  end;

  IDXENG_Draw2d_Text(MySysfont[2],'Рекорд',gameWND.Width-80 ,20,$CDCDCD,255);
  IDXENG_Draw2d_Text(MySysfont[2],IDXUtils_IntToStr(highscore),gameWND.Width-length(IDXUtils_IntToStr(highscore))*10-20 ,40,$CDCDCD,255);




end;

procedure AddtoScreenLog(text:string);
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glPushMatrix;

   glDisable( GL_TEXTURE_2D );
  IDXENG_Enable2d(true);

  IDXENG_Draw2d_Text(MySysfont[0],text,10,45,Rgb(255,0,0),255);

 glPopMatrix;
 SwapBuffers(gameWND.DC);
end;

Procedure GameInit;
var
  i:Integer;
begin
 //OpenLog;

 IDXSound_Init;

 //ParamStr(0);

  IDXENG_SetClearColor(0);

  MySysfont[0]:=IDXENG_SysFont_Create('Courier New',10,18,FW_BOLD,gameWND.DC);
  MySysfont[1]:=IDXENG_SysFont_Create('Arial',24,48,FW_BOLD,gameWND.DC);
  MySysfont[2]:=IDXENG_SysFont_Create('Arial',8,24,FW_BOLD,gameWND.DC);
  MySysfont[3]:=IDXENG_SysFont_Create('Arial',6,18,FW_BOLD,gameWND.DC);

  GameStatus:=sGMenu;//sGMenu;//sLogo;

  AddtoScreenLog('Загрузка ресурсов. Ждите...');

  IDXENG_TextureLoad(appPath+'data\mylogo.png',MyLogoGFX);
  IDXENG_TextureLoad(appPath+'data\gfx\TestAtlas.png',TestGFX);
  IDXENG_TextureLoad(appPath+'data\gfx\Boss.png',TestGFX2);
  IDXENG_TextureLoad(appPath+'data\gfx\Titul.jpg',TitulGFX);


  IDXSound_Load(appPath+'data\Sound\3.wav',bullet_sfx,1000);
  IDXSound_Load(appPath+'data\Sound\2.wav',enemy_die_sfx,0);
  IDXSound_Load(appPath+'data\Sound\8.wav',player_die_sfx,0);
  IDXSound_Load(appPath+'data\Sound\8.wav',weapon_upgrade_sfx,0);
  IDXSound_Load(appPath+'data\Sound\4.wav',player_hit_sfx,300);
  IDXSound_Load(appPath+'data\Sound\0.wav',get_powerup_sfx,0);
  IDXSound_Load(appPath+'data\Sound\6.wav',enemy_bullet_sfx,0);

  IDXSound_LoadMusic(appPath+'data\Music\Anubis.mod',level0song);




  MyLogoTimer:=GetTickCount;



  Scene_Init;
  IDXSound_PlayMusic(level0song);
end;

Procedure GameClose;
var
  i:Integer;
begin


 for i:=0 to High(MySysfont) do
  IDXENG_SySFont_Free(MySysfont[i]);

 IDXENG_TexturesListClear;

  IDXSound_Close;

  gameWND.isFinished:=True;
end;


procedure GameProcces;
var
  i:integer;
begin
  case GameStatus of
   sLogo:       ProccesLogo;
   sOptions:    ProccesOptions;
   sGMenu:      ProccesMenu;
   sGame:       ProccesGame;
   sExit:       ProccesExit;
  else

  end;
end;

procedure DrawGame();
begin

 if PauseGame then
  IDXENG_Draw2d_Text(MySysfont[0],'Пауза',10,gameWND.Height-40,Rgb(255,0,0),255);

 Scene_Draw;
end;

procedure ProccesGame();
begin

   if gameWND.Keys[ord('P')] then
   begin
     PauseGame:=not PauseGame;
     gameWND.Keys[ord('P')]:=false;
   end;

    if gameWND.Keys[VK_ESCAPE] then
    begin
      gameWND.Keys[VK_ESCAPE]:=False;
      PauseGame:=True;
      GameStatus:=sGMenu;
    end;



  if PauseGame then Exit;

 Scene_Update;

end;

procedure GameRender;
begin

   glDisable( GL_TEXTURE_2D );
  IDXENG_Enable2d(true);

 // IDXENG_Draw2d_Text(MySysfont[0],inttostr(gameWND.Mouse.X)+' : '+inttostr(gameWND.Mouse.Y),10,45,Rgb(255,0,0),255);

  case GameStatus of
   sLogo:       DrawLogo;
   sOptions:    DrawOptions;
   sGMenu:      DrawMenu;
   sGame:       DrawGame;
   sExit:       DrawExit;
  else

  end;

 IDXENG_Enable2d(false);

end;

end.






