program Main;

uses
  Windows,
  Messages,
  OpenGL,
  _OpenGL,
  _Geometry,
  _SysUtils,
  _Forms,
  _2d,
  _World,
  _planet;

// {$R *.res}
const
  grid_size = 5;


var
 //Screen Controls
 WL:single;//scale
 RS:single;//rotate speed


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

 ShowCursor(True);        {make kursor visible}

 CloseGL(glhRC,glhDC,hWnd);
 PostQuitMessage(0);
end;
{$ENDREGION}

{$REGION 'sun'}
procedure Set_Sun;
Var
 p:TPlane;
begin
 p:=Plane(0,1,0,0);
 glLightFV(Gl_LIGHT0,Gl_Position,@p);          {ligth pozition}
end;
{$ENDREGION}

{$REGION 'Keys'}
procedure Check_Keys;       //key press proc
Var mc:single;
begin
 if (world.game_mode=0) then
 begin        
  If MW>0 then Planet.WL:=Planet.WL+(Planet.WL)*0.3;
  If MW<0 then Planet.WL:=Planet.WL-(Planet.WL)*0.3;
  If Planet.WL>100 then Planet.WL:=100;
  If Planet.WL<1 then Planet.WL:=1;
  Planet.AL:=Planet.WL*0.15+Planet.AL*0.85;
  MW:=0;

  Planet.AY:=Get_R(Planet.AY-(MX-omx)/5);
  Planet.AX:=Planet.AX+(MY-omy)/5;
  If Planet.AX<0 then Planet.AX:=0;
  If Planet.AX>180 then Planet.AX:=180;
  SetCursorPos(omx,omy); 
  omx:=pw div 2;
  omy:=ph div 2;
        
 if key[27] then  //esc
  begin
   key[27]:=false;
   Kill_Prog;
  end;


 end else
 begin
  //mouse wheel move
 If MW>0 then WL:=WL+(WL)*0.3;
 If MW<0 then WL:=WL-(WL)*0.3;
 If WL>60 then WL:=60;
 MW:=0;
{ If WL<1 then WL:=1;
 World.AL:=WL*0.1+World.AL*0.9;  }
 if key[32] then //space
  begin
   world.jump(world.player_bot);
  // key[32]:=false;
  end;
 mc:=0.12;
 //move
  if (key[87] or key[38]) then begin World.Control(-mc,0,0);end;
  if (key[83] or key[40]) then begin World.Control( mc,0,0);end;
  if (key[65] or key[37]) then begin World.Control(0,-mc/2,0);end;
  if (key[68] or key[39]) then begin World.Control(0, mc/2,0);end;

{  if (key[69] or key[107] or key[187] or key[221]) then
  begin
   World.Control(0,0,-mc/5);
  end;
  if (key[81] or key[109] or key[189] or key[219]) then
  begin
   World.Control(0,0,mc/5);
  end;    }
 //rotate
 World.PY:=Get_R(World.PY-(MX-omx)/10);
 World.PX:=World.PX-(MY-omy)/10;
 If World.PX<0 then World.PX:=0;
 If World.PX>180 then World.PX:=180;
 SetCursorPos(omx,omy);
 omx:=pw div 2;
 omy:=ph div 2;
 //Menu check
 Menu.Check(mbl or mbr);
 //mouse click
 if mbl then //left mouse button - bound
  begin
   World.Fire;
  // mbl:=false;
  end;
 //mouse move rotation
 if mbr then //left mouse button
  begin
   World.Granade;
  end;
 //keys
 world.map_view:=key[192];// ` tilda
 World.Sit:=key[17];//ctrl - sit

 if key[82] then // r
  begin
  world.reload;
   key[82]:=false;
  end;
 if key[49] then // 1
  begin
   key[49]:=false;
  end;
 if key[50] then // 2
  begin
   key[50]:=false;
  end;
 if key[27] then  //esc
  begin
   key[27]:=false;
   World.on_planet;
  end;
 end;
end;
{$ENDREGION}

{$REGION 'Render'}
procedure Render;    {render}
begin
 glclear(GL_Color_Buffer_Bit xor GL_Depth_Buffer_Bit);  {clear}
 glLoadIdentity();     {'zero' projection matrix}
 glEnable(GL_DEPTH_TEST);
 if (world.game_mode=0) then
 begin
  glTranslatef(0,0,planet.AL+5); //scale
  //rotate above center
  glRotatef(planet.AX,1,0,0);
  glRotatef(planet.AY,0,0,1); //rotate
  Set_Sun;
  planet.render;
 end else
//Render
 begin
  //rotate above center
  glTranslatef(0,0,World.AL-22.8); //scale
  glRotatef(World.AX+180,1,0,0); //rotate
  glRotatef(World.AY,0,0,1);
  GlColor4f(0.5,0.5,0.5,1);
  World.Render;
 end;
//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;    {проверка нажатий и дрыганий мыши}         
 if world.game_mode>0 then 
  World.Move;
 Render;        {отрисовка}
 World.mess;
end;
{$ENDREGION}

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

procedure create_prog;{начальные настройки}
var i:integer;
begin
 ShowCursor(false);        {make kursor visible}
 Randomize;
 glDisable(GL_Texture_2d);
 glEnable(gl_Cull_face);
 glCullFace(gl_Back);
 glDepthFunc(GL_LEQUAL);
 //menu
 Menu:=TMenu.Create;
 Menu.Visible_Group:=0;
 Menu.add_element(' ',85,85,0);
 Menu.add_element(' ',85,90,0);
 Menu.add_element(' ',50,50,0);
 Menu.add_element('100',10,5,0);
 Menu.add_element('100',35,5,0);
 Menu.add_element('100',65,5,0);
 Menu.add_element('100',90,5,0);
 Menu.add_element(' ',50,25,0);
 Menu.add_element(' ',50,90,0);
 Menu.add_element('Синие: :100',10,05,0);
 Menu.add_element('Красные:100',10,10,0);
 Menu.add_element('Зеленые:100',10,15,0);
 Menu.add_element('Желтые :100',10,20,0);
 planet:=TPlanet.Create(0,12);
 planet.WL:=50;
 planet.ax:=80;
 planet.ay:=310;
 World:=TWorld.Create;
 glLineWidth(3);
// world.game_mode:=1;
{
 Menu.add_element('1111111111',50,50,0,@Open_Menu);
 Menu.add_element('2222222222',50,60,0,@Open_Menu);
 Menu.add_element('3333333333',50,70,0,@Open_Menu); }
 Preload(100);
end;
{$ENDREGION}

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