unit essai_map;

interface

uses

  sysutils,
  System.Drawing,
  System.Windows.Forms,
  Microsoft.DirectX,
  Microsoft.DirectX.DirectInput,
  Microsoft.DirectX.Direct3D;

type
  TColor = system.drawing.Color;


  TMap= class

  public

  tailleX:integer;
  tailleZ:integer;
  Number_triangle:integer;
  Vecteur : array of string;
  FVertexBuffer : VertexBuffer;
  constructor create;
  procedure initialize ( Filename : string );
  procedure Triangle;
  function NumVerts ( i : integer ; j:integer) : integer;
  procedure Cube_Create( sender : system.object ; e: EventArgs );
  procedure OnCreateDevice(sender: System.Object; e: EventArgs);
  procedure OnResetDevice(sender: TObject; e: EventArgs);
  end;

implementation
constructor TMap.create;
begin
  inherited;
end;

procedure TMap.initialize ( Filename : string );
var
i : integer;
F: Textfile ;
line : string;
begin
  AssignFile(F, Filename);
  Reset(F);
  readln(F,tailleX);
  readln(F,tailleZ);
  setlength(Vecteur,tailleZ);
  i := 0;
  while (i< tailleZ) do
    begin
        readln( F , line );
        self.Vecteur[i] := line;
        i := i+1
    end;
  close(F);
end;


procedure TMap.Triangle ;
var
x,z:integer;
begin
Number_triangle:=0;
z:=0;
while (z < tailleZ) do
 begin
  x:=0;

  while (x < tailleX) do
  begin
    if  (self.vecteur[z][x+1] > '0') then
      begin
        Number_triangle:=Number_triangle + 1;
      end;
      x:=x+1
  end;
   z:=z+1;
 end;

 Number_triangle:=Number_triangle*12;

end;





function TMap.NumVerts ( i : integer ; j : integer) :integer ;

begin

result := i*36*tailleZ + (j-1)*36;

end;

procedure TMap.Cube_Create( sender : system.object ; e: EventArgs );

var
y:integer;
verts: array of CustomVertex.PositionColored ;
stm: GraphicsStream;
x,z : integer;
Num_Verts: integer;
begin
  setlength(verts,(tailleX*tailleZ*36));
  z:=0;
  Self.FVertexBuffer := VertexBuffer( sender ) ;
  stm := Self.FVertexBuffer.Lock(0, 0, LockFlags( 0 ));
 while (z < tailleZ) do
 begin
  x:=0;

  while (x < tailleX) do
  begin

    if  (self.vecteur[z][x+1] <> '0') then
    begin

      if self.Vecteur[z][x+1] = '1' then
      y:=0
      else
      y:=5;


  Num_verts := self.Numverts(z,x);
  // 1er face

  verts[ Num_verts +0] := CustomVertex.PositionColored.Create( 0+x , y, 0+z , TColor.gray.ToArgb );
  verts[ Num_verts +1 ] := CustomVertex.PositionColored.Create( 1+x , y, 0+z , TColor.Blue.ToArgb );
  verts[ Num_verts +2 ] := CustomVertex.PositionColored.Create(  0+x,  -1,0+z, TColor.Blue.ToArgb );
  verts[ Num_verts +3 ] := verts [ Num_verts + 2 ];
  verts[ Num_verts +4 ] := verts [ Num_verts + 1 ];
  verts[ Num_verts +5 ] := CustomVertex.PositionColored.Create( 1+x,  -1,  0+z, TColor.Blue.ToArgb );

  // 2eme face

  verts[ Num_verts +6 ] := CustomVertex.PositionColored.Create( 0+x, y , 1+z , TColor.Gray.ToArgb );
  verts[ Num_verts +7 ] := CustomVertex.PositionColored.Create(  1+x, y, 1+z , TColor.Gray.ToArgb );
  verts[ Num_verts +8 ] := CustomVertex.PositionColored.Create( 0+x,  -1 , 1+z , TColor.Gray.ToArgb );
  verts[ Num_verts +9 ] :=  verts [ Num_verts + 8];
  verts[ Num_verts +10 ] := verts [ Num_verts + 7 ];
  verts[ Num_verts +11 ] := CustomVertex.PositionColored.Create(  1+x, -1 , 1+z , TColor.Gray.ToArgb );

  // 3eme face : face du  haut

  verts[ Num_verts +12 ] := verts [ Num_verts + 2 ];
  verts[ Num_verts +13 ] := verts [ Num_verts + 5 ];
  verts[ Num_verts +14 ] := verts [ Num_verts + 8 ];
  verts[ Num_verts +15 ] := verts [ Num_verts + 8 ];
  verts[ Num_verts +16 ] := verts [ Num_verts + 5 ];
  verts[ Num_verts +17 ] := verts [ Num_verts + 11 ];

    // 4eme face : face du dessous

  verts[ Num_verts +18 ] := verts [ Num_verts + 0 ];
  verts[ Num_verts +19 ] := verts [ Num_verts + 1 ];
  verts[ Num_verts +20 ] := verts [ Num_verts + 6 ];
  verts[ Num_verts +21 ] := verts [ Num_verts + 6 ];
  verts[ Num_verts +22 ] := verts [ Num_verts + 1 ];
  verts[ Num_verts +23 ] := verts [ Num_verts + 7 ];

    // 5eme face

  verts[ Num_verts +24 ] := verts [ Num_verts + 1 ];
  verts[ Num_verts +25 ] := verts [ Num_verts + 5 ];
  verts[ Num_verts +26 ] := verts [ Num_verts + 7 ];
  verts[ Num_verts +27 ] := verts [ Num_verts + 7 ];
  verts[ Num_verts +28 ] := verts [ Num_verts + 5 ];
  verts[ Num_verts +29 ] := verts [ Num_verts + 11 ];

    // 6eme face

  verts[ Num_verts +30 ] := verts [ Num_verts + 0 ];
  verts[ Num_verts +31 ] := verts [ Num_verts + 2 ];
  verts[ Num_verts +32 ] := verts [ Num_verts + 6 ];
  verts[ Num_verts +33 ] := verts [ Num_verts + 6 ];
  verts[ Num_verts +34 ] := verts [ Num_verts + 2 ];
  verts[ Num_verts +35 ] := verts [ Num_verts + 8 ];



         end;

  x:=x+1;

         end;
  z:=z+1;
  end;
 stm.Write(verts);
 Self.FVertexBuffer.Unlock;
end;


procedure TMap.OnCreateDevice(sender: System.Object; e: EventArgs);
var
  dev: Device;
begin
  dev := Device( sender );
  // Now Create the VB
  Self.FVertexBuffer := VertexBuffer.Create( TypeOf( CustomVertex.PositionColored ), (tailleX*tailleZ*36), dev, Usage( 0 ), CustomVertex.PositionColored.Format, Pool.Default );
  //Include( Self.FVertexBuffer.Created, TPerso.OnCreateVertexBuffer );
  Self.cube_create( Self.FVertexBuffer, nil );
end;

procedure TMap.OnResetDevice(sender: TObject; e: EventArgs);
var
  dev: Device;
begin
  dev := Device( sender );
  // Turn off culling, so we see the front and back of the triangle
  dev.RenderState.CullMode := Cull.None;
  // Turn off D3D lighting, since we are providing our own vertex colors
  dev.RenderState.Lighting := false;
end;

end.

