{*******************************************************}
{                                                       }
{       Small Game Engine                               }
{                                                       }
{       Copyright (C) 2010 IdeyaX studio                }
{       Developer: Amosow Evgeniy                       }
{                                                       }
{*******************************************************}

unit GMMap;

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

const
  MaxSnow=20;

type
 TsnowParticle=record
  x,y:Integer;
  dx,dy:Integer;
  sxy:Integer;
  pic:Integer;
  alpha:Byte; 
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();

procedure GameStartNew();
procedure GameLoadLevel(level:Integer);


var
  SnowWall:array[0..MaxSnow] of TsnowParticle;

implementation
        uses
        GMGame,
        GMWindow;


procedure InitSnow;
var
  i:Integer;
begin

  for i:=0 to MaxSnow do
  begin
   SnowWall[i].x:=Random(SCRW);
   SnowWall[i].y:=Random(SCRH);
   SnowWall[i].dx:=0;//Random(3)+1;
   SnowWall[i].dy:=Random(3)+1;
   SnowWall[i].sxy:=Random(54)+10;
   SnowWall[i].pic:=Random(3);
   SnowWall[i].alpha:=Random(105)+150;
  end;

end;

procedure UpdateSnow();
var
  i:Integer;
begin

  for i:=0 to MaxSnow do
  begin
    inc( SnowWall[i].x, SnowWall[i].dx);
    inc( SnowWall[i].y, SnowWall[i].dy);

    if (SnowWall[i].x>SCRW) or (SnowWall[i].y>SCRH) then
    begin
         SnowWall[i].x:=Random(SCRW);
         SnowWall[i].y:=-64;
         SnowWall[i].dx:=0;//Random(3)+1;
         SnowWall[i].dy:=Random(3)+1;
         SnowWall[i].sxy:=Random(54)+10;
         SnowWall[i].pic:=Random(3);
         SnowWall[i].alpha:=Random(105)+120;
    end;

  end;

end;

procedure DrawSnow();
var
  i:Integer;
begin
glBlend(1);
  for i:=0 to MaxSnow do
  begin
    DrawTextureBox( SnowWall[i].x, SnowWall[i].y,
                    SnowWall[i].sxy,SnowWall[i].sxy,
                    SnowTex[SnowWall[i].pic],0,True,
                    $ffffff,
                    SnowWall[i].alpha);
  end;

end;

procedure GameStartNew();
begin

end;

procedure GameLoadLevel(level:Integer);
begin

end;

procedure ProccesLogo();
begin

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

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

 end;




end;

procedure DrawLogo();
begin

  glBlend(1);
  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,$FFFFFF,255);

  c:=$FFFFFF;

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

  c:=$FFFFFF;

  if PointInRect(MousePos.X,MousePos.Y,SCR2W+45 ,SCR2H+40-20,30,28) then c:=$0011FFFF;
  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;

end;

procedure DrawAuthor();
begin

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

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,$FFFFFF,255);
  SText2D(MySysfont[2],'Звуки',SCR2W-100,SCR2H+30,$FFFFFF,255);

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

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

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

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


end;


procedure ProccesMenu();
var
  i:Integer;
begin

UpdateSnow;

 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;

   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
         MyGameMap.NewGame;
         GameStatus:=sGame;
         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

 glBlend(1);
 DrawTextureBox(0,0,scrw,scrh,TitulTex);
 DrawTextureBox(0,0,512,512,TitulTex2);


  SText2D(MySysfont[2],'Программирование и дизайн: Амосов Евгений aka Gambit_OZ(IdeyaX studio)',10 ,SCRH-10,$000076EC,255);

 DrawSnow;

  for i:=0 to High(Titul_menu) do
  begin
   //c:=$FF0000;
   if GameStatus=sGMenu then
   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,$0009FFFF);

   SText2D(MySysfont[2],Titul_menu[i],SCRW-200 ,SCR2H+i*30,$FFFFFF,255);
  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[2],text,10,45,Rgb(255,0,0),255);

 glPopMatrix;
 SwapBuffers(GWDC);
end;

Procedure GameInit;
var
  i:Integer;
begin
 OpenLog;

 InitSoundEngine;

 //ParamStr(0);

  glClearColor(255, 255, 255, 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;//sEditor;//sGame;//sGMenu;//sLogo;

  LoadTexture('data\mylogo.png',MyLogoGFX);

  LoadTexture('data\gfx\Titul.png',TitulTex);
  LoadTexture('data\gfx\Titul11.png',TitulTex2);


  LoadTexture('data\gfx\sn1.png',SnowTex[0]);
  LoadTexture('data\gfx\sn2.png',SnowTex[1]);
  LoadTexture('data\gfx\sn3.png',SnowTex[2]);

  WndSetCaption('Новогодний глюк');

  InitSnow;
  MyGameMap.Init;
  MyLogoTimer:=GetTickCount;
  AddtoScreenLog('Loading resources...');

end;

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


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

  end;


end;

procedure DrawGame();
begin


  MyGameMap.Draw;

 if isWinstate=1 then DrawTextureBox(SCR2W-256,SCR2H-256,512,512,WinPic[0]);
 if isWinstate=2 then DrawTextureBox(SCR2W-256,SCR2H-256,512,512,WinPic[1]);
 if isWinstate=3 then DrawTextureBox(SCR2W-256,SCR2H-256,512,512,WinPic[2]);


 if PauseGame then
  SText2D(MySysfont[2],'Пауза',100,SCRH-15,$000076EC,255);


end;

procedure ProccesGame();
begin

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

    if Keys[VK_ESCAPE] then
    begin
      //PostQuitMessage(0);
      Keys[VK_ESCAPE]:=False;
      PauseGame:=True;
      GameStatus:=sGMenu;
    end;



  if PauseGame then Exit;

 if isWinstate>0 then
 begin
   if Keys[13] or Keys[32] then
   begin
     Keys[13]:=False;
     Keys[32]:=False;

    if isWinstate=1 then
    begin
      MyGameMap.Refresh;
      isWinstate:=0;
      Exit;
    end;

    if isWinstate=2 then
    begin
       GameStatus:=sGMenu;
       isWinstate:=0;
       Exit;
    end;

    if isWinstate=3 then
    begin
       GameStatus:=sGMenu;
       isWinstate:=0;
       Exit;
    end;

   end;

 end;

 if isWinstate=0 then
 begin
   MyGameMap.Procces;
   MyPacMan.Update;
   UpdateGhost;
 end;

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);

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

  end;


   Enable2d(false);
   glPopMatrix;
   SwapBuffers(GWDC);

 end;

end.






