program Template;

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

uses SysUtils,
     Math,
     
     fwLogging,
     fwCustomEngine,
     fwPlattform,
     fwResource,
     fwAnimation,
     fwSprite;

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

// -----------------------------------------------------------------------
// Main
// -----------------------------------------------------------------------
var
  Viewpoint        : TWorldRect;         // Screen width and height
  Screen           : TfwCESurface;
  Resources        : TfwResourceManager;
  GameImages       : TfwCESurface;
  SpriteEngine     : TfwSpriteEngine;
begin
  // setup graphic engine
  SetHAL(TfwEngine.Create(false, false));
  try
    HAL().Caption := 'SpaceInvaders (c) Thomas Seban';
    Screen := HAL.Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
    if Assigned(Screen) then begin
      // video, audio and control init done with default values for
      // each plattform.

      // Setup world viewport
      Viewpoint.Left   := 0;
      Viewpoint.Top    := 0;
      Viewpoint.Right  := 319;
      Viewpoint.Bottom := 239;

      // Adding resource manager, which free all loaded stuff at
      // end of program.
      Resources := TfwResourceManager.Create();
      try
        // Load image for non moveable sprite as visible fixpoint
        // on screen
        GameImages := HAL.CreateSurface('../Media/spaceinvaders.bmp', $00FF00FF);
        if not Assigned(GameImages) then begin
          Exit;
        end;
        Resources.Add('GAMEDATA01', GameImages);

        // Creating the world for all sprites
        SpriteEngine := TfwSpriteEngine.Create;
        if not Assigned(SpriteEngine) then begin
          Exit;
        end;
        Resources.Add('SPRITEENINGE', SpriteEngine);

        while (not HAL().ExitKeyPressed()) do begin
          // paint background black
          HAL.ClearScreen($00000000);

          // render all sprite to screen
          SpriteEngine.Render(Screen, Viewpoint);

          // show graphics
          HAL.Flip(Screen);

          // update control all
          HAL.Idle();
          HAL.UpdateInput();

          // remove all killed sprites
          SpriteEngine.RemoveSprites();

          Sleep(4);
        end;
      finally
        FreeAndNil(Resources);
      end;
    end;
  except
    on E : Exception do begin
      fwLog(E.Message);
    end;
  end;
end.