program Keyboard01;

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}

uses SysUtils,

{$IFDEF WIN32}
     SDL,
{$ENDIF}

     fwLogging,
     fwCustomEngine,
     fwPlattform;

const
  SCREEN_WIDTH     = 320;
  SCREEN_HEIGHT    = 240;
  SCREEN_BPP       = 16;

var
  Screen           : TfwCESurface;
  Font             : TfwCEFont;
  LeftKey          : TfwCEInputState;
  UpKey            : TfwCEInputState;
  RightKey         : TfwCEInputState;
  DownKey          : TfwCEInputState;
  FireKey          : TfwCEInputState;
begin
  fwLOG('Starting');
  // setup grafik engine, look at fwPlattform
  SetHAL(TfwEngine.Create(false, false));
  try
    Screen := HAL().Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
    if Assigned(Screen) then begin
      Font := HAL().CreateFont('../Media/arial.ttf', 14);

      LeftKey := HAL().AddKey(TfwCEInputState.Create('Left',  'Move left'));
      if Assigned(LeftKey) then begin
        LeftKey.AddSensor(TfwCEKeyControl.Create(LEFT_BUTTON));
{$IFDEF WIN32}
        LeftKey.AddSensor(TfwCEKeyControl.Create(SDLK_A));
{$ENDIF}
      end;

      UpKey := HAL().AddKey(TfwCEInputState.Create('Up',    'Move up'));
      if Assigned(UpKey) then begin
        UpKey.AddSensor(TfwCEKeyControl.Create(UP_BUTTON));
{$IFDEF WIN32}
        UpKey.AddSensor(TfwCEKeyControl.Create(SDLK_W));
{$ENDIF}
      end;

      RightKey := HAL().AddKey(TfwCEInputState.Create('Right', 'Move right'));
      if Assigned(RightKey) then begin
        RightKey.AddSensor(TfwCEKeyControl.Create(RIGHT_BUTTON));
{$IFDEF WIN32}
        RightKey.AddSensor(TfwCEKeyControl.Create(SDLK_D));
{$ENDIF}
      end;

      DownKey := HAL().AddKey(TfwCEInputState.Create('Down',  'Move down'));
      if Assigned(DownKey) then begin
        DownKey.AddSensor(TfwCEKeyControl.Create(DOWN_BUTTON));
{$IFDEF WIN32}
        DownKey.AddSensor(TfwCEKeyControl.Create(SDLK_S));
{$ENDIF}
      end;

      FireKey := HAL().AddKey(TfwCEInputState.Create('Fire',  'Fire'));
      if Assigned(FireKey) then begin
        FireKey.AddSensor(TfwCEKeyControl.Create(FIRE_BUTTON));
{$IFDEF WIN32}
        FireKey.AddSensor(TfwCEKeyControl.Create(SDLK_RCTRL));
{$ENDIF}
      end;

	  fwLOG('Starting main loop');
      while (not HAL().ExitKeyPressed()) do begin
	    HAL().ClearScreen($00000000);

        Font.RenderTo(Screen, 10, 10, 'Controls', $00FFFFFF);

        Font.RenderTo(Screen, 20, 30, 'Simple', $00FFFFFF);
		
        // Simple access control
        if HAL().Controls.KeyPressed(LEFT_BUTTON)  then Font.RenderTo(Screen,  30, 50, 'Left', $00FFFFFF);
        if HAL().Controls.KeyPressed(UP_BUTTON)    then Font.RenderTo(Screen,  70, 50, 'Up', $00FFFFFF);
        if HAL().Controls.KeyPressed(RIGHT_BUTTON) then Font.RenderTo(Screen, 110, 50, 'Right', $00FFFFFF);
        if HAL().Controls.KeyPressed(DOWN_BUTTON)  then Font.RenderTo(Screen, 150, 50, 'Down', $00FFFFFF);
        if HAL().Controls.KeyPressed(FIRE_BUTTON)  then Font.RenderTo(Screen, 190, 50, 'Fire', $00FFFFFF);

        // More flexible, using multiple keys as input 
        Font.RenderTo(Screen, 20, 90, 'Using multiple keys', $00FFFFFF);
        if LeftKey.Down  then Font.RenderTo(Screen,  30, 110, 'Left', $00FFFFFF);
        if UpKey.Down    then Font.RenderTo(Screen,  70, 110, 'Up', $00FFFFFF);
        if RightKey.Down then Font.RenderTo(Screen, 110, 110, 'Right', $00FFFFFF);
        if DownKey.Down  then Font.RenderTo(Screen, 150, 110, 'Down', $00FFFFFF);
        if FireKey.Down  then Font.RenderTo(Screen, 190, 110, 'Fire', $00FFFFFF);

        // Show graphics
        HAL().Flip(Screen);

        // update input like keyboard, mouse or otjer
        HAL().Idle();
        HAL().UpdateInput();
	  end;
    end;
  except
    on E : Exception do begin
      fwLog(E.Message);
    end;
  end;
end.
