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

unit GMMap;

interface
     uses
     OpenGl,
     Windows,
     GMVars,
     GMUtilite,
     Textures,
     log,
     Classes,
     IDX_sound,
     fmoddyn,
     Math,
     SysUtils;

type
  TGameLevel=record

 Lives:Integer;
 Score:Integer;

 TotalCookies:Integer;
 TotalGhosts:Integer;
 TotalTime:Integer;

 LevelNumber:Integer;

 IsSingleLevel:Boolean;
 m_Round:Integer;
end;


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

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

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

implementation
        uses GMWindow,GMTetris,GMHallFame;

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
  glBlend(2);
  DrawTextureBox(SCR2W,SCR2H,256,256,MyLogoGFX,0,True,$FFFFFF,MyLogoAlpha);

end;

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

 if Keys[MouseLB] then
 begin
    Keys[MouseLB]:=False;

  if PointInRect(MousePos.X,MousePos.Y,SCR2W-155,SCR2H+40-20,40,28) then
  GameStatus:=sGMenu;

  if PointInRect(MousePos.X,MousePos.Y,SCR2W+45 ,SCR2H+40-20,30,28) then
  PostQuitMessage(0);

 end;

end;

procedure DrawExit();
var
  c:Integer;
begin
DrawMenu;
glBlend(1);
  DrawRectangle2D(0,0,SCRW,SCRH,0,100,0,True);

  SText2D(MySysfont[2],'Вы действительно хотите выйти из игры?',SCR2W-200 ,SCR2H-20,$004080FF,255);

  c:=$004080FF;

  if PointInRect(MousePos.X,MousePos.Y,SCR2W-155,SCR2H+40-20,40,28) then c:=$FFFFFF;
  SText2D(MySysfont[2],'Нет',SCR2W-150 ,SCR2H+40,c,255);

  c:=$004080FF;

  if PointInRect(MousePos.X,MousePos.Y,SCR2W+45 ,SCR2H+40-20,30,28) then c:=$FFFFFF;
  SText2D(MySysfont[2],'Да',SCR2W+50 ,SCR2H+40,c,255);

end;



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

 if Keys[MouseLB] or Keys[MouseRB] then
 begin
   Keys[MouseLB]:=False;
   Keys[MouseRB]:=False;
    GameStatus:=sGMenu;
 end;

end;

procedure DrawAuthor();
begin
  glBlend(2);
  DrawTextureBox(SCR2W,SCR2H,256,256,MyLogoGFX,0,True,$FFFFFF,255);

end;



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

end;

procedure DrawRecords();
begin
 DrawTextureBox( 0,0, scrw,scrh,MyPanelGFX[6]);
 MyHallFame.draw;

end;

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

 if Keys[MouseLB] then
 begin
    Keys[MouseLB]:=False;

   if PointInRect(MousePos.X,MousePos.Y,SCR2W-25,SCR2H-20-20,120,28) then
   begin
    playingmusic:=not playingmusic;
   end;  

  if PointInRect(MousePos.X,MousePos.Y,SCR2W-25,SCR2H+30-20,120,28) then
  begin
    playingsound:=not playingsound;
  end;



  if PointInRect(MousePos.X,MousePos.Y,SCR2W-125,SCR2H+100-20,70,28) then GameStatus:=sGMenu;
  if PointInRect(MousePos.X,MousePos.Y,SCR2W+55,SCR2H+100-20,105,28) then
  begin
   SaveOptions('Options.ini');
   GameStatus:=sGMenu;
  end;
 end;
  
end;

procedure DrawOptions();
var
  c:Integer;
  s:string;
begin
 DrawMenu;
 glBlend(1);
  DrawRectangle2D(0,0,SCRW,SCRH,0,100,0,True);

  SText2D(MySysfont[2],'Музыка',SCR2W-100 ,SCR2H-20,$004080FF,255);
  SText2D(MySysfont[2],'Звуки',SCR2W-100,SCR2H+30,$004080FF,255);

 s:='- Выключена';
 if playingmusic then s:='- Включена';
   c:=$004080FF;
  if PointInRect(MousePos.X,MousePos.Y,SCR2W-25,SCR2H-20-20,120,28) then c:=$FFFFFF;
  SText2D(MySysfont[2],s,SCR2W-20 ,SCR2H-20,c,255);

 s:='- Выключены';
 if playingsound then s:='- Включены';
    c:=$004080FF;
  if PointInRect(MousePos.X,MousePos.Y,SCR2W-25,SCR2H+30-20,120,28) then c:=$FFFFFF;
  SText2D(MySysfont[2],s,SCR2W-20,SCR2H+30,c,255);

  c:=$004080FF;
  if PointInRect(MousePos.X,MousePos.Y,SCR2W-125,SCR2H+100-20,70,28) then c:=$FFFFFF;
  SText2D(MySysfont[2],'Назад',SCR2W-120,SCR2H+100,c,255);

  c:=$004080FF;
  if PointInRect(MousePos.X,MousePos.Y,SCR2W+55,SCR2H+100-20,105,28) then c:=$FFFFFF;
  SText2D(MySysfont[2],'Применить',SCR2W+60,SCR2H+100,c,255);

end;


procedure ProccesMenu();
var
  i:Integer;
begin

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

 end;

  if Keys[MouseLB] then
  begin
    Keys[MouseLB]:=False;

   if MenuLevel=0 then
   for i:=0 to High(Titul_menu) do
    begin
     if PointInRect(MousePos.X,MousePos.Y,SCRW-205 ,SCR2H+i*30-20,205,28) then
     begin

       if Titul_menu[i]='Новая игра' then
       begin
         MenuLevel:=1;
         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
    else
    if MenuLevel=1 then
    begin
     for i:=0 to High(Game_style) do
     begin
      if PointInRect(MousePos.X,MousePos.Y,SCRW-205 ,SCR2H+i*30-20,205,28) then
      begin

       if Game_style[i]='Клаccический вариант' then
       begin
         MenuLevel:=0;
         StopMusic;
         PlayMusic('gamemusic.mp3');
         player.newgame(TETRIS_MODE_CLASSIC);
         GameStatus:=sGame;
         Break;
       end;

       if Game_style[i]='Бомбер' then
       begin
         MenuLevel:=0;
         StopMusic;
         PlayMusic('gamemusic.mp3');
         player.newgame(TETRIS_MODE_BOMBER);
         GameStatus:=sGame;
         Break;
       end;

       if Game_style[i]='Логистик' then
       begin
         MenuLevel:=0;
         //player.newgame(TETRIS_MODE_LOGIC);
         GameStatus:=sGame;
         Break;
       end;


       if Game_style[i]='Назад' then
       begin
         MenuLevel:=0;
         Break;
       end;

      end;
     end;




    end;
  end;
end;

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

glBlend(1);
  DrawTextureBox( 0,0, scrw,scrh,MyPanelGFX[6]);

  SText2D(MySysfont[2],'Амосов Евгений aka Gambit_oz[IdeyaX studio]',10 ,SCRH-40,$00C5C5C5,255);
  SText2D(MySysfont[2],'Специально для конкурса IGDC.ru',10 ,SCRH-15,$00BEBEBE,255);

  if MenuLevel=0 then
  for i:=0 to High(Titul_menu) do
  begin

   if GameStatus=sGMenu then

   DrawRectangle2D(SCRW-205 ,SCR2H+i*30-20,205,28,$0,200,0,True);
   SText2D(MySysfont[2],Titul_menu[i],SCRW-200 ,SCR2H+i*30,$004080FF,255);

    if PointInRect(MousePos.X,MousePos.Y,SCRW-205 ,SCR2H+i*30-20,205,28) then //c:=$FF;
      DrawRectangle2D(SCRW-205 ,SCR2H+i*30-20,205,28,$004080FF);

  end;

  if MenuLevel=1 then
  begin


      for i:=0 to High(Game_style) do
      begin
       //c:=$FF0000;
       if GameStatus=sGMenu then

        DrawRectangle2D(SCRW-205 ,SCR2H+i*30-20,205,28,$0,200,0,True);
       SText2D(MySysfont[2],Game_style[i],SCRW-200 ,SCR2H+i*30,$004080FF,255);

       if PointInRect(MousePos.X,MousePos.Y,SCRW-205 ,SCR2H+i*30-20,205,28) then //c:=$FF;
        DrawRectangle2D(SCRW-205 ,SCR2H+i*30-20,205,28,$004080FF);

      end;

  end;  


end;

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

   glDisable( GL_TEXTURE_2D );
  Enable2d(true);

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

 glPopMatrix;
 SwapBuffers(GWDC);
end;

Procedure GameInit;
begin
 OpenLog;

 InitSoundEngine;

 //ParamStr(0);

  glClearColor(0, 0, 0, 255);

  MySysfont[0]:= CreateSystemFont2D(GWDC,'Courier New',10,18);
  MySysfont[1]:=CreateSystemFont2D(GWDC,'Arial',24,48);

  MySysfont[2]:=CreateSystemFont2D(GWDC,'Arial',8,24);
  MySysfont[3]:=CreateSystemFont2D(GWDC,'Arial',6,18);

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

  LoadTexture(appPath+'data\Gfx\mylogo.png',MyLogoGFX);
  LoadTexture(appPath+'data\Gfx\fig1.png',MyTileGFX[0]);
  LoadTexture(appPath+'data\Gfx\fig0.png',MyTileGFX[1]);
  LoadTexture(appPath+'data\Gfx\bomb.png',MyTileGFX[2]);

  LoadTexture(appPath+'data\Gfx\Linepnl.png',MyPanelGFX[0]);
  LoadTexture(appPath+'data\Gfx\Levelpnl.png',MyPanelGFX[1]);
  LoadTexture(appPath+'data\Gfx\Timepnl.png',MyPanelGFX[2]);
  LoadTexture(appPath+'data\Gfx\Scorepnl.png',MyPanelGFX[3]);
  LoadTexture(appPath+'data\Gfx\Recordpnl.png',MyPanelGFX[4]);
  LoadTexture(appPath+'data\Gfx\Nextpnl.png',MyPanelGFX[5]);
  LoadTexture(appPath+'data\Gfx\Back0.jpg',MyPanelGFX[6]);


  MyLogoTimer:=GetTickCount;

  AddtoScreenLog('Loading resources...');

  PlayMusic('menumusic.mp3');
end;

Procedure GameClose;
begin
  FreeSoundEngine;
  FreeAllTextures;
  CloseLog;
end;


procedure GameProcces;
var
  i:integer;
begin

//  WndSetCaption('[FPS:'+inttostr(Trunc(FPS))+'] '+inttostr(MousePos.X)+' : '+inttostr(MousePos.Y));

  case GameStatus of
   sLogo:       ProccesLogo;
   sOptions:    ProccesOptions;
   sGMenu:      ProccesMenu;
   sGame:       ProccesGame;
   sRecords:    ProccesRecords;
   sAuthor:     ProccesAuthor;
   sExit:       ProccesExit;
  else

  end;

   for i:=0 to High(Keys) do Keys[i]:=False;

end;

procedure DrawGame();
begin

  DrawTextureBox( 0,0, scrw,scrh,MyPanelGFX[6]);

//  SText2D(MySysfont[0],'Режим игра',10,SCRH-20,Rgb(255,0,0),255);
 if PauseGame then
  SText2D(MySysfont[0],'Пауза',10,SCRH-40,Rgb(255,0,0),255);

  player.Draw;


end;

procedure ProccesGame();
begin

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

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





  if PauseGame then Exit;

  player.Procces;

end;

procedure GameRender;
 Var
 s:string;
 i,id,m,W:integer;
 Pt: TPoint;
 x,y:Integer;
 mm:Single;
 begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glPushMatrix;

   glDisable( GL_TEXTURE_2D );
  Enable2d(true);

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

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

  end;



  //glblend(2);
 // DrawRectangle2D(0,0,256,100,$FFFFFF,220,0,true,false);


 //SText2D(MySysfont,inttostr(Mymap.xoffset)+' : '+inttostr(Mymap.yoffset),10,70,Rgb(255,0,0),255);

//  glEnable( GL_TEXTURE_2D );
//
//
//
//  glBlend(1);

 Enable2d(false);
 //

 //SText2D(MySysfont,s,600,200,Rgb(255,255,255),255);

 glPopMatrix;
 SwapBuffers(GWDC);

 end;

end.






