program images02;

// Example for creating sprites with animation

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

uses SysUtils,

     fwLogging,
     fwCustomEngine,
     fwPlattform,

     fwAnimation,
     fwSprite;

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

var
  Viewpoint        : TWorldRect;
  Screen           : TfwCESurface;
  ShipImage        : TfwCESurface;
  ShipAnim         : TfwAnimSequences;
  SpriteEngine     : TfwSpriteEngine;
  ShipSprite       : TfwSprite;
begin
  // setup graphic engine
  SetHAL(TfwEngine.Create(false, false));
  try
    Screen := HAL.Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
    if Assigned(Screen) then begin
       // video, audio and control init done

       // Loading image
       ShipImage := HAL.CreateSurface('../Media/ip_ship2.bmp', $00FF00FF);
       if not Assigned(ShipImage) then begin
         Exit;
       end;

       // Creating animation for ship. Multiple sprites can sharing
       // one animation object.
       ShipAnim := TfwAnimSequences.Create;
       try
         // define one animation sequence. A sprite can have more
         // than one sequence. Each sequence contains one or
         // more sub images.
         with ShipAnim.AddSequence do begin
           // add frames to this animation sequence
           AddFrame( 0,  0, 24, 24);
           AddFrame(27,  0, 24, 24);
           AddFrame(48,  0, 24, 24);
           AddFrame(72,  0, 24, 24);
           AddFrame(96,  0, 24, 24);
           AddFrame(96, 26, 24, 24);
           AddFrame(96, 48, 48, 24);
           AddFrame(96, 70, 48, 24);
           AddFrame(96, 96, 48, 24);
           AddFrame(72, 96, 24, 24);
           AddFrame(48, 96, 24, 24);
           AddFrame(27, 96, 24, 24);
           AddFrame( 0, 96, 24, 24);
           AddFrame( 0, 70, 48, 24);
           AddFrame( 0, 48, 48, 24);
           AddFrame( 0, 26, 24, 24);
         end;

         Viewpoint.Left   := 0;
         Viewpoint.Top    := 0;
         Viewpoint.Right  := 319;
         Viewpoint.Bottom := 219;

         // Creating the world for all sprites
         SpriteEngine := TfwSpriteEngine.Create;
         try
           // Create one sprite
           ShipSprite := TfwSprite.Create(SpriteEngine);
           try
             // set image and animation stuff
             ShipSprite.Image := ShipImage;
             ShipSprite.AnimController.Animation := ShipAnim;
             ShipSprite.AnimController.AutoLoop  := True;
             ShipSprite.Show;

             ShipSprite.PosX := 148;
             ShipSprite.PosY := 108;

             // Add sprite to render pipeline
             SpriteEngine.Add(ShipSprite);

             while (not HAL().ExitKeyPressed()) do begin

               // paint background black
               HAL.ClearScreen($00000000);
               
               // render all srpite to screen
               SpriteEngine.Render(Screen, Viewpoint);

               // show graphics
               HAL.Flip(Screen);

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

               // do all animations 
               SpriteEngine.Move(HAL.FrameTime);
             end;
           finally
             FreeAndNil(ShipSprite);
           end;
        finally
          FreeAndNil(SpriteEngine);
        end;
      finally
        FreeAndNil(ShipAnim);
      end;
    end;
  except
    on E : Exception do begin
      fwLog(E.Message);
    end;
  end;
end.
