unit D3D;

interface

uses
  AI,
  perso,
  gameplay,
  map,
  System.Drawing,
  System.Windows.Forms,
  Microsoft.DirectX,
  Microsoft.DirectX.DirectInput,
  Microsoft.DirectX.Direct3D;

type

  TEventHandler      = System.EventHandler;
  TEventArgs         = System.EventArgs;
  TKeyEventArgs      = System.Windows.Forms.KeyEventArgs;
  TMouseEventArgs    = System.Windows.Forms.MouseEventArgs;
  TPaintEventArgs    = System.Windows.Forms.PaintEventArgs;
  TKeys              = System.Windows.Forms.Keys;
  TForm              = System.Windows.Forms.Form;
  TColor             = System.Drawing.Color;
  TIcon              = System.Drawing.Icon;


  TMG = class (TForm)
    private
      FPause : boolean;
      FPresentParams: PresentParameters;
    public
      FDevice : Device;
      procedure SetupMatrices(pos : Tpos);
      constructor create;
      function Initialize:boolean ;
      procedure Render(pos: Tpos; Cube : TPerso; AI : TAI ; FVertexBuffer1 : vertexbuffer ; FVertexBuffer2 : VertexBuffer ; Num_triangle:integer ; FTexture1 : texture ; Ftexture2: texture);



    strict protected
//      procedure OnPaint(e: TPaintEventArgs); override;
      procedure OnKeyDown( E: TKeyEventArgs ); override;
      procedure OnResize(e: TEventArgs); override;
  end;


implementation


constructor TMG.create;
begin
   inherited;
   Self.ClientSize  := System.Drawing.Size.Create(800, 800);
  // And it's caption
  Self.Text := 'Stickman : The Deep Trip';
  // Load our icon from the resources of the .exe
  //Self.Icon := TIcon.Create( Self.GetType, 'icone.ico');
end;

function TMG.Initialize: Boolean;
begin
    // Now let's setup our D3D stuff
    Self.FPresentParams := PresentParameters.Create;
    // We don't want to run fullscreen
    Self.FPresentParams.Windowed := True;
    // Discard the frames
    Self.FPresentParams.SwapEffect := SwapEffect.Discard;
    // anti crénelage
    Self.FPresentParams.MultiSample := MultiSampleType.None;
    // Z-Buffer
    Self.FPresentParams.EnableAutoDepthStencil := true;
    Self.FPresentParams.AutoDepthStencilFormat := DepthFormat.D16;
    //Create a device
    Self.FDevice := Device.Create( 0, DeviceType.Hardware , Self, CreateFlags.SoftwareVertexProcessing, [Self.FPresentParams] );
    //Include(Self.FDevice, Self.cube.OnCreateDevice);
    Result := True;
end;

procedure TMG.Render(pos : Tpos ; Cube : TPerso; AI : TAI ; FVertexBuffer1 : vertexbuffer ; FVertexBuffer2 : VertexBuffer ; Num_triangle:integer ; Ftexture1 : texture ; Ftexture2: texture);
var
  i,iTime : integer;
begin

  Self.FDevice.Clear(ClearFlags.Target or ClearFlags.ZBuffer, System.Drawing.Color.Purple, 1, 0);
  Self.FDevice.RenderState.MultiSampleAntiAlias := true;
  Self.FDevice.BeginScene;

  Self.fdevice.Transform.Projection := Matrix.PerspectiveFovLH((Single((Math.PI/ 4)))
                                                     ,(Width / Height), 1, 1000);
  Self.FDevice.Transform.View := Matrix.LookAtLH( Vector3.Create( pos.x + 20*cos(pos.angle), 10, pos.z + 20*sin(pos.angle) ), Vector3.Create( pos.x - 20*cos(pos.angle), pos.y, pos.z - 20*sin(pos.angle) ), Vector3.Create( 0, 1, 0 ) );

  Self.FDevice.Transform.World := matrix.Multiply(matrix.rotationX(-math.pi/2),matrix.Multiply(matrix.Scaling(0.005,0.005,0.005),matrix.Multiply(matrix.RotationY(-pos.angle - math.pi/ 2),matrix.Translation(pos.x, pos.y+ 1.4, pos.z))));
  self.Fdevice.SetTexture(0, cube.meshtextures[0]);
  cube.mesh.DrawSubset(0);

  i:=0;
  while i < AI.nbCops do
  begin
    Self.FDevice.Transform.World := matrix.RotationY(AI.Cops[i].pos.angle+math.pi)*matrix.Translation(AI.Cops[i].pos.x+0.4,AI.Cops[i].pos.y +0.8,AI.Cops[i].pos.z+0.5);
    self.Fdevice.SetTexture(0, AI.meshtextures[0]);
    AI.mesh.DrawSubset(0);
    i := i+1;
  end;




  Self.FDevice.Transform.World := matrix.get_Identity;
  self.fdevice.SetTexture(0,Ftexture1);
  Self.FDevice.SetStreamSource( 0, FVertexBuffer1, 0 );
  Self.FDevice.VertexFormat := CustomVertex.Positiontextured.Format;
  Self.FDevice.DrawPrimitives( PrimitiveType.TriangleList, 0, Num_triangle); //24

  //Self.FDevice.Transform.World := matrix.RotationY(-pos.angle)*matrix.Translation(pos.x, 0 , pos.y);
  self.fdevice.SetTexture(0,Ftexture2);
  Self.FDevice.SetStreamSource( 0, FVertexBuffer2, 0 );
  Self.FDevice.VertexFormat := CustomVertex.Positiontextured.Format;
  Self.FDevice.DrawPrimitives( PrimitiveType.TriangleList, 0, 12 );


  Self.FDevice.Transform.Projection := Matrix.PerspectiveFovLH( ( ( Math.PI / 4 ) ), 1.0, 1.0, 100.0 );

  Self.FDevice.EndScene;
  Self.FDevice.Present;
end;

//procedure TD3D.OnPaint(e: TPaintEventArgs);
//begin
//  Self.Render(pos , FVertexBuffer1 , FVertexBuffer2 )
//end;

procedure TMG.OnKeyDown( E: TKeyEventArgs );
begin
  inherited;
  if (e.KeyCode = TKeys.Escape) then
    Self.Close;
end;

procedure TMG.OnResize(e: TEventArgs);
begin
  FPause := ( ( Self.WindowState = FormWindowState.Minimized ) or ( not Self.Visible ) );
end;

procedure TMG.SetupMatrices(pos : Tpos);
var
  iTime: Integer;

begin

  // For our world matrix, we will just rotate the object about the y-axis.

  // Set up the rotation matrix to generate 1 full rotation (2*PI radians)
  // every 1000 ms. To avoid the loss of precision inherent in very high
  // floating point numbers, the system time is modulated by the rotation
  // period before conversion to a radian angle.
  //iTime := ( Environment.TickCount mod 10000 );
  //fAngle := ( iTime * ( ( 2 * ( Math.PI ) ) / 10000 ) );



  // Set up our view matrix. A view matrix can be defined given an eye point,
  // a point to lookat, and a direction for which way is up. Here, we set the
  // eye five units back along the z-axis and up three units, look at the
  // origin, and define "up" to be in the y-direction.

  // For the projection matrix, we set up a perspective transform (which
  // transforms geometry from 3D view space to 2D viewport space, with
  // a perspective divide making objects smaller in the distance). To build
  // a perpsective transform, we need the field of view (1/4 pi is common),
  // the aspect ratio, and the near and far clipping planes (which define at
  // what distances geometry should be no longer be rendered).

end;


end.

