unit menu;


interface

uses
  SysUtils,
  system.IO,
  System.Drawing,
  system.Threading,
  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;

  TMenu = class( system.windows.forms.form )
  strict private
    // Our rendering device
    FVertexBuffer : VertexBuffer;
    FPause : boolean;
    clavier : Microsoft.DirectX.DirectInput.Device;
    i:integer;
    launch : boolean;
    FTexture:texture;
  public
    constructor Create;
    procedure Initialize(device:device);
    procedure Initialise_clavier;
    procedure Render(device:device);
    procedure main(device:device);
    procedure OnCreateVertexBuffer(sender: TObject; e: TEventArgs);
    procedure Lire_clavier(sender : Tobject ; e:TEventArgs);
  strict protected
    procedure OnKeyDown( E: TKeyEventArgs ); override;
    procedure OnResize(e: TEventArgs); override;
  end;



implementation


constructor TMenu.Create;
begin
  inherited;

end;

procedure TMenu.Initialize(device:device);
begin
  Self.FVertexBuffer := VertexBuffer.Create( TypeOf( CustomVertex.Positiontextured ), 36, device, Usage( 0 ), CustomVertex.Positiontextured.Format, Pool.Default );
  self.OnCreateVertexBuffer(self.FVertexBuffer, nil);
  Self.FPause := False;
  i:=0;
  launch := false;
  Initialise_clavier;
  Ftexture:= TextureLoader.FromFile( device, 'menu1.bmp' );
end;








procedure TMenu.OnCreateVertexBuffer(sender: TObject; e: TEventArgs);
var
  verts: array[ 0..35 ]  of CustomVertex.Positiontextured;
  stm: GraphicsStream;
  vb: VertexBuffer;
begin
  vb := VertexBuffer( sender ) ;
  stm := vb.Lock( 0, 0, LockFlags(0) );

  // FACE DE DEVANT

  verts[ 0 ] := CustomVertex.Positiontextured.Create( -1, -1, -1, 0 ,1 );
  verts[ 1 ] := CustomVertex.Positiontextured.Create(  1, -1, -1, 1 , 1 );
  verts[ 2 ] := CustomVertex.Positiontextured.Create( -1,  1, -1, 0 , 0 );
  verts[ 3 ] := verts [ 2 ];
  verts[ 4 ] := verts [ 1 ];
  verts[ 5 ] := CustomVertex.Positiontextured.Create(  1,  1, -1, 1 , 0);

  // FACE DE DERRIERE

  verts[ 6 ] := CustomVertex.Positiontextured.Create( -1, -1, 1, 0 , 1 );
  verts[ 7 ] := CustomVertex.Positiontextured.Create(  1, -1, 1, 1 , 1 );
  verts[ 8 ] := CustomVertex.Positiontextured.Create( -1,  1, 1, 0 , 0 );
  verts[ 9 ] :=  verts [ 8];
  verts[ 10 ] := verts [ 7 ];
  verts[ 11 ] := CustomVertex.Positiontextured.Create( 1, 1, 1, 1 , 0 );

  // FACE DU DESSUS

  verts[ 12 ] := CustomVertex.Positiontextured.Create( -1, 1, -1, 0 , 1 );
  verts[ 13 ] := CustomVertex.Positiontextured.Create( 1,  1, -1, 1 , 1 );
  verts[ 14 ] := CustomVertex.Positiontextured.Create( -1, 1, 1, 0 , 0 );
  verts[ 15 ] :=  verts [ 14 ];
  verts[ 16 ] :=  verts [ 13 ];
  verts[ 17 ] := CustomVertex.Positiontextured.Create(  1, 1, 1, 1 , 0 );

    // FACE DU DESSOUS

  verts[ 18 ] := CustomVertex.Positiontextured.Create( -1, -1, -1, 0 , 1 );
  verts[ 19 ] := CustomVertex.Positiontextured.Create(  1, -1, -1, 1 , 1 );
  verts[ 20 ] := CustomVertex.Positiontextured.Create( -1, -1,  1, 0 , 0 );
  verts[ 21 ] := verts [ 20 ];
  verts[ 22 ] := verts [ 19 ];
  verts[ 23 ] := CustomVertex.Positiontextured.Create(  1, -1,  1, 1 , 0 );

    // FACE DE GAUCHE

  verts[ 24 ] := CustomVertex.Positiontextured.Create(  1,  -1, -1, 0 , 1 );
  verts[ 25 ] := CustomVertex.Positiontextured.Create(  1,   1, -1, 1 , 1 );
  verts[ 26 ] := CustomVertex.Positiontextured.Create(  1,  -1,  1, 0 , 0 );
  verts[ 27 ] := verts [ 26 ];
  verts[ 28 ] := verts [ 25 ];
  verts[ 29 ] := CustomVertex.Positiontextured.Create(  1,   1,  1, 1 , 0 );

    // FACE DE DROITE

  verts[ 30 ] := CustomVertex.Positiontextured.Create(  -1,  -1, -1, 0 , 1 );
  verts[ 31 ] := CustomVertex.Positiontextured.Create(  -1,   1, -1, 1 , 1 );
  verts[ 32 ] := CustomVertex.Positiontextured.Create(  -1,  -1,  1, 0 , 0 );
  verts[ 33 ] := verts [ 32 ];
  verts[ 34 ] := verts [ 31 ];
  verts[ 35 ] := CustomVertex.Positiontextured.Create(  -1,   1,  1, 1 , 0 );

  stm.Write( verts );
  vb.Unlock;
end;


procedure TMenu.Render(device:device);
begin
  //Clear the backbuffer to a blue color
  Device.Clear( ClearFlags.Target , TColor.White, 1.0, 0 );
  Device.Clear( ClearFlags.ZBuffer , TColor.White, 1.0, 0 );

  //Begin the scene
  Device.BeginScene;
  // Setup the world, view, and projection matrices

  {Device.Transform.View := Matrix.LookAtLH( Vector3.Create( 0, 0, -3.41 ), Vector3.Create( 0.0, 0.0, 0.0 ), Vector3.Create( 0, 1, 0 ) );

  device.SetTexture(0, Ftexture);
  Device.SetStreamSource( 0, Self.FVertexBuffer, 0 );
  Device.VertexFormat := CustomVertex.Positiontextured.Format;
  Device.DrawPrimitives( PrimitiveType.TriangleList, 0, 12 );

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

  //End the scene
  Device.EndScene;
  Device.Present;
end;

procedure TMenu.OnKeyDown( E: TKeyEventArgs );
begin
  inherited;
  // Esc was pressed
  if ( e.KeyCode = TKeys.Escape ) then
    Self.Close;
end;

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

procedure TMenu.Initialise_clavier;
begin
  clavier := Microsoft.DirectX.DirectInput.Device.Create(SystemGuid.keyboard);
  clavier.SetCooperativeLevel(Self, (CooperativeLevelFlags.Background
                                      or CooperativeLevelFlags.NonExclusive));
  clavier.Acquire;

end;

procedure TMenu.Lire_clavier (sender : Tobject ; e:TEventArgs);
var
  keys: KeyboardState;
  dev : device;
begin
  keys := clavier.GetCurrentKeyboardState;
  dev := Device( sender );
  if keys[Key.up] then
  begin
    if i=0 then
    i:= 4
    else
    i:=(i-1);


    if i=0 then

     Ftexture:= TextureLoader.FromFile( dev, 'menu1.bmp' );

    if i=1 then
     Ftexture:= TextureLoader.FromFile( dev, 'menu2.bmp' );

    if i=2 then
     Ftexture:= TextureLoader.FromFile( dev, 'menu3.bmp' );

    if i=3 then
     Ftexture:= TextureLoader.FromFile( dev, 'menu4.bmp' );

    if i=4 then
     Ftexture:= TextureLoader.FromFile( dev, 'menu5.bmp' );

    sleep(200);
  end;

  if keys[Key.down] then
  begin
  if i = 4 then
    i:= 0
  else
    i:=i+1;

    if i=0 then

     Ftexture:= TextureLoader.FromFile( dev, 'menu1.bmp' );

    if i=1 then
     Ftexture:= TextureLoader.FromFile( dev, 'menu2.bmp' );

    if i=2 then
     Ftexture:= TextureLoader.FromFile( dev, 'menu3.bmp' );

    if i=3 then
     Ftexture:= TextureLoader.FromFile( dev, 'menu4.bmp' );

    if i=4 then
     Ftexture:= TextureLoader.FromFile( dev, 'menu5.bmp' );

    sleep(200);
  end;

  if keys[Key.return] then
    if i=0 then
      launch:=true;



end;


procedure TMenu.main(device:device);

begin
  while not launch do
  begin
    Lire_clavier(device , nil);
    render(device);
  end;
end;

end.

