program Snake;

uses
  Windows,
  Messages,
  OpenGL,
  _OpenGL,
  _Geometry,
  _SysUtils,
  _Open_Format,
  _Forms,
  _world,
  _bass,
  _2d;

// {$R *.res}

var
 //Screen Controls
 AX,AY:single;//rotate
 WL,AL:single;//scale

 //test
 test:single;

{$REGION 'Start/stop'}
procedure Start_game();
begin
  World.game_type:=0;
  Menu.Visible_Group:=1;
  World.Show_Text('Уровень '+IntToStr(World.cLev));
  _bass.Play_Sound(World._AAA);
end;

procedure Stop_game();
begin
  World.game_type:=1;
  Menu.Visible_Group:=0;
  _bass.Stop_Sound(World._AAA);
end;
{$ENDREGION}

{$REGION 'Kill_program'}
procedure kill_prog;
var i:integer;
begin
 KillTimer(0,TactH);     {destroy timer}

 ShowCursor(True);        {make kursor visible}
 //meshes
 _bass.Kill_Sounds;
 CloseGL(glhRC,glhDC,hWnd);
 PostQuitMessage(0);
end;
{$ENDREGION}

{$REGION 'Keys'}
procedure Check_Keys;       //key press proc
begin
 //mouse move rotation
 AX:=Get_R(((PW div 2) - MX)/8);
 AY:=Get_R(((PH div 2) - MY)/10+60);
 //mouse wheel move
 If MW>0 then WL:=WL+(WL)*0.3;
 If MW<0 then WL:=WL-(WL)*0.3;
 If WL>400 then WL:=400;
 If WL<1 then WL:=1;
 AL:=WL*0.1+AL*0.9;
 MW:=0;
 //move
  if (key[87] or key[38]) then begin world.Control(1){KY:=KY+1;key[87]:=false;key[38]:=false;}end;
  if (key[83] or key[40]) then begin world.Control(3){KY:=KY-1;key[83]:=false;key[40]:=false;}end;
  if (key[65] or key[37]) then begin world.Control(2){KX:=KX-1;key[65]:=false;key[37]:=false;}end;
  if (key[68] or key[39]) then begin world.Control(0){KX:=KX+1;key[68]:=false;key[39]:=false;}end;
 //Menu check
 Menu.Check(mbl or mbr);
 //mouse click
 if mbl then //left mouse button
  begin
  // mbl:=false;
  end;
 //keys
 if key[32] then //space
  begin
   //do someth
  end;
 if key[27] then  //esc
  begin
   key[27]:=false;
   if World.game_type=0 then
    begin
     stop_game;
    end
    else
    begin
     start_game;
    end
  // Kill_Prog;
  end;
end;
{$ENDREGION}

{$REGION 'Render'}
procedure Render;    {render}
var
  i,j,count:integer;
begin
 glclear(GL_Color_Buffer_Bit xor GL_Depth_Buffer_Bit);  {clear}
 glLoadIdentity();     {'zero' projection matrix}
 world.Render;
 //text
 Menu.Render;
 //render
 glFlush();
 SwapBuffers(glhDC);
end;
{$ENDREGION}

{$REGION 'Timer'}
procedure OnTimer(hWnd:LongWord;uMsg:UINT;idEvent:UINT;dwTime:DWORD);stdcall;
begin
 Check_Keys;    {проверка нажатий и дрыганий мыши}
 Render;        {отрисовка}
 World.Move;
end;
{$ENDREGION}

{$REGION 'Create_prog'}
procedure Open_Menu();
begin
  Menu.Visible_Group:=0;
end;

procedure Close_Menu();
begin
  Menu.Visible_Group:=2;
end;

procedure Show_();
begin
  Menu.Visible_Group:=2;
end;

procedure Diff_menu();
begin
  Menu.Visible_Group:=3;
end;

procedure Diff_menu_easy();
begin
 world.period:=16;
 Menu.Visible_Group:=0;
 start_game;
end;

procedure Diff_menu_med();
begin
 world.period:=12;
 Menu.Visible_Group:=0;
 start_game;
end;

procedure Diff_menu_hard();
begin
 world.period:=9;
 Menu.Visible_Group:=0;
 start_game;
end;

procedure create_prog;{начальные настройки}
var i:integer;
begin
 _bass.Init_Sounds(hwnd);
 Randomize;
 glEnable(GL_Texture_2d);
 glDisable(GL_lighting);
 world:=TWorld.Create;
 world.clev:=1;
 //menu
 Menu:=TMenu.Create;
 Menu.add_element('Уровень 1',45,50,1);
 Menu.add_element('Новая игра',45,50,0,@Diff_menu);
 Menu.add_element('Легко',65,40,3,@Diff_menu_easy);
 Menu.add_element('Средне',65,50,3,@Diff_menu_med);
 Menu.add_element('Тяжело',65,60,3,@Diff_menu_hard);
// Menu.add_element('Новая игра',50,50,0,@Level_Menu);
// Menu.add_element('Автор',50,50,0);
 Menu.add_element('Выход',45,55,0,@Close_Menu);
 Menu.add_element('Да ',45,60,2,@kill_prog);
 Menu.add_element('Нет',55,60,2,@Open_Menu);
// Menu.add_element('Выход',50,55,0,@kill_prog);
end;
{$ENDREGION}

begin
 PT:=0;//top/left value
 PL:=0;
 PW:=GetSystemMetrics(SM_CXSCREEN);//get Desktop resolution
 PH:=GetSystemMetrics(SM_CYSCREEN);
 Main_Proc('Snake',@create_prog,@kill_prog,@onTimer,PT,PL,PW,PH);
end.
