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
  const
  taille = 35 ;
  public

  Vecteur : array[0..(taille-1)] of string;
  FVertexBuffer : VertexBuffer;
  constructor create;
  procedure initialize ( Filename : string );
  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);
  i := 0;
  while (i< taille) do
    begin
        readln( F , line );
        self.Vecteur[i] := line;
        i := i+1
    end;
  close(F);
end;

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

begin

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

end;

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

var

verts: array[0..((taille*taille*36)-1)] of CustomVertex.PositionColored ;
stm: GraphicsStream;
x,z : integer;

begin

  z:=0;
  Self.FVertexBuffer := VertexBuffer( sender ) ;
  stm := Self.FVertexBuffer.Lock( 0, 0, LockFlags( 0 ) );
 while (z < taille) do
  begin
  x:=0;

    while (x < taille) do
         begin
         if  (  self.vecteur[z][x+1] = '1') then

         begin

  // 1er face

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

  // 2eme face

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

  // 3eme face : face du  haut

  verts[ self.Numverts(z,x) +12 ] := verts [ self.Numverts(z,x) + 2 ];
  verts[ self.Numverts(z,x) +13 ] := verts [ self.Numverts(z,x) + 5 ];
  verts[ self.Numverts(z,x) +14 ] := verts [ self.Numverts(z,x) + 8 ];
  verts[ self.Numverts(z,x) +15 ] := verts [ self.Numverts(z,x) + 8 ];
  verts[ self.Numverts(z,x) +16 ] := verts [ self.Numverts(z,x) + 5 ];
  verts[ self.Numverts(z,x) +17 ] := verts [ self.Numverts(z,x) + 11 ];

    // 4eme face : face du dessous

  verts[ self.Numverts(z,x) +18 ] := verts [ self.Numverts(z,x) + 0 ];
  verts[ self.Numverts(z,x) +19 ] := verts [ self.Numverts(z,x) + 1 ];
  verts[ self.Numverts(z,x) +20 ] := verts [ self.Numverts(z,x) + 6 ];
  verts[ self.Numverts(z,x) +21 ] := verts [ self.Numverts(z,x) + 6 ];
  verts[ self.Numverts(z,x) +22 ] := verts [ self.Numverts(z,x) + 1 ];
  verts[ self.Numverts(z,x) +23 ] := verts [ self.Numverts(z,x) + 7 ];

    // 5eme face

  verts[ self.Numverts(z,x) +24 ] := verts [ self.Numverts(z,x) + 1 ];
  verts[ self.Numverts(z,x) +25 ] := verts [ self.Numverts(z,x) + 5 ];
  verts[ self.Numverts(z,x) +26 ] := verts [ self.Numverts(z,x) + 7 ];
  verts[ self.Numverts(z,x) +27 ] := verts [ self.Numverts(z,x) + 7 ];
  verts[ self.Numverts(z,x) +28 ] := verts [ self.Numverts(z,x) + 5 ];
  verts[ self.Numverts(z,x) +29 ] := verts [ self.Numverts(z,x) + 11 ];

    // 6eme face

  verts[ self.Numverts(z,x) +30 ] := verts [ self.Numverts(z,x) + 0 ];
  verts[ self.Numverts(z,x) +31 ] := verts [ self.Numverts(z,x) + 2 ];
  verts[ self.Numverts(z,x) +32 ] := verts [ self.Numverts(z,x) + 6 ];
  verts[ self.Numverts(z,x) +33 ] := verts [ self.Numverts(z,x) + 6 ];
  verts[ self.Numverts(z,x) +34 ] := verts [ self.Numverts(z,x) + 2 ];
  verts[ self.Numverts(z,x) +35 ] := verts [ self.Numverts(z,x) + 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 ), (taille*taille*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.

