unit Decor;

interface

uses
  perso,
  System.Collections,
  System.ComponentModel,
  System.Data,
  System.Drawing,
  System.Windows.Forms,
  System.IO,
  Microsoft.DirectX,
  Microsoft.DirectX.DirectInput,
  Microsoft.DirectX.Direct3D;

type
  TSpheres = record
    pos : TPos;
    scale : double;
  end;
  TDecor = class
    spheres : array[0..19] of TSPheres;
    mesh : mesh;
    materialarray : array of ExtendedMaterial;
    meshmaterials : array[0..0] of material;
    meshtextures : array[0..0] of texture;
    constructor create;
    procedure initialize( TailleX,TailleZ:integer;device : device);
    procedure ia(TailleX, TailleZ : integer);
  end;

implementation

constructor TDecor.create;
begin
  inherited;
end;

procedure TDecor.initialize( TailleX,TailleZ:integer;device:device);
var
  x : integer;
  vertices : vertexbuffer;
  stream : GraphicsStream;
begin
  x := 0;
  while x < 20 do
  begin
    spheres[x].pos.x := random(TailleX+40)-20;
    if (spheres[x].pos.x<tailleX) and (spheres[x].pos.x>0) then
      spheres[x].pos.z := (random(2)-1/2)*2*(random(20)+6+tailleX/2)+tailleX/2
    else
       spheres[x].pos.z := random(TailleZ+40)-20;
    spheres[x].pos.y := (random(40))-20;
    spheres[x].scale := 1/(random(6)+1);
    x:=x+1;
  end;
  mesh := Mesh.FromFile('texture/mesh/' + 'sphere.x', MeshFlags.Managed, device, materialarray);

  meshmaterials[0] := materialarray[0].Material3D;
  meshmaterials[0].Ambient := meshmaterials[0].Diffuse;

  if ((materialarray[0].TextureFilename <> nil) and (materialarray[0].TextureFilename <> '')) then
    meshtextures[0] := TextureLoader.FromFile(device,'texture/mesh/' + materialarray[0].TextureFilename);

  mesh := mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalTextured.Format, device);
  mesh.ComputeNormals();
  vertices := mesh.VertexBuffer;
  stream := vertices.Lock(0, 0, LockFlags.None);

  vertices.Unlock();
end;

procedure TDecor.ia(TailleX, TailleZ : integer);
var
  x : integer;
begin
  x := 0;
  while x < 20 do
  begin
    spheres[x].pos.y := spheres[x].pos.y + 0.1/spheres[x].scale;
    if spheres[x].pos.y > 20 then
    begin
      spheres[x].pos.x := random(TailleX+40)-20;
      if (spheres[x].pos.x<tailleX) and (spheres[x].pos.x>0) then
        spheres[x].pos.z := (random(2)-1/2)*2*(random(20)+6+tailleX/2)+tailleX/2
      else
         spheres[x].pos.z := random(TailleZ+40)-20;
      spheres[x].pos.y := -20;
      spheres[x].scale := 1/(random(4)+1);
    end;
    x:= x+1;
  end;

end;

end.
