unit Otshelm_A;

interface
uses
  Mform, GLScene, Setup, Forms, ghload, SysUtils,
  Variants, Classes,  Graphics, Controls,
  GLUtils, GLHudObjects, Sound, GLGeomObjects,
  GLWin32FullScreenViewer, GLWin32Viewer, GLMisc,
  GLTexture, GLCadencer, GLObjects, GLShadowPlane,
  GLSkyBox, GLLensFlare, GLGui, TGA;

var
  FCcontine : boolean = false;

  BigFL : tglPolygon;
  Scene : TGLScene;
  Viewer : TGLSceneViewer;
  ML1, ML2 : TGLMATERIALlIBRARY;

  OCamera, LGCamera : TGLCamera;
  GLWork, GameCube, LGGameCube, GamerSound2, GamerMusic, GamerSound, GLInterface : TGLDummyCube;

  Sun, Liy: TGLLightSource;
  LensFlare : TGLLensFlare;
  Sky  : TGLSkyBox;

  Shadow : TGLShadowPlane;

  GameCur : TGLHudSprite;

procedure CreatAll;
procedure pisecALL;

implementation
uses
  LFiles, Material;

Procedure CreatShadows;
begin
  if Shadow=nil then
  Shadow:=TGLShadowPlane(GameCube.AddNewChild(TGLShadowPlane));
  Shadow.Material.FrontProperties.Diffuse.Alpha:=0;
  Shadow.NoZWrite:=false;
  Shadow.Scale.SetVector(30,30,1);
  Shadow.ShadowColor.SetColor(0,0,0,0.57);
  Shadow.ShadowedLight:=Sun;
  Shadow.ShadowOptions:=[spoUseStencil,spoScissor,spoTransparent];
  Shadow.Position.Z:=0.02;
  
end;

procedure CreatViewer;
begin
  if Viewer=nil then
  Viewer:=TGLSceneViewer.Create(OForm);
  Viewer.Parent:=OForm;
  Viewer.Enabled:=true;
  Viewer.Align:=alClient;
  Viewer.Buffer.BackgroundColor:=clBlack;
  Viewer.Buffer.FogEnable:=true;
  Viewer.Buffer.FogEnvironment.FogColor.SetColor(1,0.3,0.3,0.1);

  if Setup.VerSync then
  Viewer.VSync:=vsmSync
  else
  Viewer.VSync:=vsmNoSync;

  if Setup.Shadows then
  Viewer.Buffer.ContextOptions:=[roDoubleBuffer,roStencilBuffer,roRenderToWindow]
  else Viewer.Buffer.ContextOptions:=[roDoubleBuffer,roRenderToWindow];
  Viewer.Camera:=OCamera;
  Viewer.OnMouseDown:=OForm.FullViewer.OnMouseDown;
  Viewer.OnMouseUp:=OForm.FullViewer.OnMouseUp;
  Viewer.OnMouseMove:=OForm.FullViewer.OnMouseMove;
end;

Procedure UseSetup;
begin
  if (Setup.Shadows) then
  CreatShadows
  else
  if Shadow<>nil then freeandnil(Shadow);

  If FullWin=true then
    begin
      OForm.FullViewer.Height:=WinH;
      OForm.FullViewer.Width:=WinW;
      if WinColors=32 then
      OForm.FullViewer.ScreenDepth:=sd32bits;
      if WinColors=16 then
      OForm.FullViewer.ScreenDepth:=sd16bits;
      OForm.FullViewer.Buffer.BackgroundColor:=clBlack;
      OForm.FullViewer.Buffer.FogEnable:=true;
      OForm.FullViewer.Buffer.FogEnvironment.FogColor.SetColor(1,0.3,0.3,0.1);

      if Setup.Shadows then
      OForm.FullViewer.Buffer.ContextOptions:=[roDoubleBuffer,roStencilBuffer,roRenderToWindow]
      else OForm.FullViewer.Buffer.ContextOptions:=[roDoubleBuffer,roRenderToWindow];

      OForm.FullViewer.Camera:=OCamera;
      OForm.BorderStyle:=bsNone;
      OForm.Width:=0;
      OForm.Height:=0;
      OForm.FullViewer.Active:=true;

      if Setup.VerSync then
      OForm.FullViewer.VSync:=vsmSync
      else
      OForm.FullViewer.VSync:=vsmNoSync;
    end
  else
    begin
      OForm.FullViewer.Active:=false;
      if Viewer=nil then CreatViewer;
      OForm.BorderStyle:=bsDialog;
      OForm.ClientWidth:=Setup.WinW;
      OForm.ClientHeight:=Setup.WinH;
      OForm.Position:=poDesktopCenter;
    end;
end;


procedure CreatAll;
begin
  Setup.LoadSetupFile;
  
  Scene:=TglScene.Create(OForm);

  OForm.Cadencer.Scene:=Scene;

  ML1 := TGLMaterialLibrary.Create(OForm);
  ML2 := TGLMaterialLibrary.Create(OForm);
  Sky:=TGLSkyBox(Scene.Objects.AddNewChild(TGLSkyBox));
  Sky.PitchAngle:=90;
  Material.LoadJPG;

  GLWork:=TGLDummyCube(Scene.Objects.AddNewChild(TGLDummyCube));

  Sun:=TGLLightSource(GLWork.AddNewChild(TGLLightSource));
  LensFlare:=TGLLensFlare(Sun.AddNewChild(TGLLensFlare));
  Sun.Position.SetPoint(37,7,11);



  GameCube:=TGLDummyCube(Scene.Objects.AddNewChild(TGLDummyCube));
  GamerMusic:=TGLDummyCube(GameCube.AddNewChild(TGLDummyCube));
  GamerSound:=TGLDummyCube(GameCube.AddNewChild(TGLDummyCube));
  GamerSound2:=TGLDummyCube(GameCube.AddNewChild(TGLDummyCube));

  LgGameCube:=TGLDummyCube(Scene.Objects.AddNewChild(TGLDummyCube));
  GLInterface:=TGLDummyCube(Scene.Objects.AddNewChild(TGLDummyCube));

  SetCurrentDir(ExtractFilePath(Application.ExeName)+'\Data\');
  OForm.GLBitmapFont1.Glyphs.LoadFromFile('font.bmp');

  ghload.GLGLayout:= TGLGuiLayout.Create(OForm);
  ghload.GLGLayout.BitmapFont:=OForm.GLBitmapFont1;
  ghload.GLGLayout.Material.Texture.Image.LoadFromFile('Skin.bmp');
  ghload.GLGLayout.Material.Texture.Disabled:=false;
  ghload.GLGLayout.FileName:='Skin';
  ghload.GLGUIWork:=GLInterface;
  ghload.Height:=Setup.WinH;
  ghload.Width:=Setup.WinW;
  ghload.glgLoad('1.gxt');

  LFiles.work;

  OCamera:=TGLCamera(GameCube.AddNewChild(TGLCamera));

  OCamera.Up.SetVector(0,0,1);
  OCamera.Position.Z:=4;
  OCamera.Position.Y:=-2.7;
  OCamera.TargetObject:=GameCube;

  LGCamera:=TGLCamera(LGGameCube.AddNewChild(TGLCamera));
  LGCamera.Up.SetVector(0,0,1);
  LGCamera.TargetObject:=LGGameCube;
  UseSetup;

  GameCur:=TGLHudSprite(Scene.Objects.AddNewChild(TGLHudSprite));
  GameCur.Width:=32;
  GameCur.Height:=32;

  GameCur.Material.MaterialLibrary:=Oform.ML3;
  GameCur.Material.LibMaterialName:='curs';
  OForm.GLSMBASS1.Listener:=Ocamera;

  Sound.LMusic('str');
  Sound.LMusic('rla');
  Sound.LMusic('par');
  Sound.LMusic('mz1');
  Sound.LMusic('win');
  Sound.LMusic('mtd');

  FCcontine:=true;
end;

procedure pisecALL;
begin
  if Otshelm_A.Viewer<>nil then
  FreeandNil(Otshelm_A.Viewer);

  FreeandNil(Otshelm_A.ML1);
  FreeandNil(Otshelm_A.ML2);

  FreeandNil(Oform.FullViewer);
  FreeandNil(Oform.Cadencer);

  FreeandNil(OForm.GLSMBASS1);
  FreeandNil(OForm.GLSoundLibrary1);

  if (Otshelm_A.Scene<>nil)
  and (Oform.FullViewer=nil)
  and (Oform.Cadencer=nil)
  and (Otshelm_A.Viewer=nil)
  then FreeandNil(Otshelm_A.Scene);
end;


end.
