Motion Graphics

Introduction

The best place to look for motion graphics is the supplied example Pong, which covers a range of PasSFML features. You can see the source code on GitHub. This first example is a much simpler demonstration of a second hand rotating six degrees each second. It is the equivalent of our Smart Pascal demo. The second example is a motion graphic based on the texture demonstration on the preceding page.

See also Using OpenGL for Motion Graphics and using PasSFML with the Box2D physics engine for a moving chassis and marionette.

Code of SecondHand

program SecondHand;
{$Apptype GUI}
uses
  SysUtils, SfmlGraphics, SfmlSystem, SfmlWindow;
const
  HAND_WIDTH = 5;
  WINDOW_WIDTH = 220;
  WINDOW_HEIGHT = 220;
var
  Window: TSfmlRenderWindow;
  Rect: TSfmlRectangleShape;
  HandLength, HandLong: real;
  Event: TsfmlEvent;
begin
  Window := TSfmlRenderWindow.Create(SfmlVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32),
    AnsiString('Second Hand'), [sfTitleBar, sfClose], nil);
  Window.SetVerticalSyncEnabled(True);
  HandLength := WINDOW_WIDTH * 2 / 5;
  HandLong := HandLength * 4 / 5; // Distance from the spindle to the tip
  Rect := TSfmlRectangleShape.Create;
  Rect.FillColor := SfmlGreen;
  Rect.Size := SfmlVector2f(HandLength, HAND_WIDTH);
  Rect.Origin := SfmlVector2f(HandLength - HandLong, HAND_WIDTH / 2);
  Rect.Position := SfmlVector2f(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
  Rect.Rotate(-90);
  while Window.isOpen do
    begin
      while SfmlRenderWindowPollEvent(Window.Handle, Event) do
        begin
          if Event.EventType = sfEvtClosed then  // Window closed
            SfmlRenderWindowClose(Window.Handle);
          // Escape key pressed
          if (Event.EventType = sfEvtKeyPressed) and (Event.Key.Code = sfKeyEscape) then
            SfmlRenderWindowClose(Window.Handle);
        end;
      Window.Clear(SfmlWhite);
      Window.Draw(Rect);
      Window.Display;
      sfmlSleep(sfmlSeconds(1));
      Rect.Rotate(6);
    end;
end.
    

Code of MovingEnemy

program MovingEnemy;
{$Apptype Gui}
uses
  SysUtils, SfmlGraphics, SfmlSystem, SfmlWindow;
const
  WINDOW_WIDTH = 512;
  WINDOW_HEIGHT = 512;
var
  Window: TSfmlRenderWindow;
  Event: TsfmlEvent;

  Background, GroundEnemy: PSfmlTexture;
  SpriteBackground, SpriteGroundEnemy: TSfmlSprite;
  OutputImage: PsfmlImage;
  GroundEnemyHeight: real;
  FrameCounter: integer;
begin
  Window := TSfmlRenderWindow.Create(SfmlVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32),
    AnsiString('TextureDemo'), [sfTitleBar, sfClose], nil);
  Window.SetVerticalSyncEnabled(True);
  // Load the background sprite to display
  Background := SfmlTextureCreateFromFile('Bricks0.png', nil);
  if not Assigned(Background) then
    raise Exception.Create('Background texture error');
  SpriteBackground := TsfmlSprite.Create;
  SpriteBackGround.SetTexture(Background, True);
  // Load the enemy sprite to display
  GroundEnemy := SfmlTextureCreateFromFile('GroundEnemy.png', nil);
  if not Assigned(GroundEnemy) then
    raise Exception.Create('Enemy texture error');
  SpriteGroundEnemy := TsfmlSprite.Create;
  SpriteGroundEnemy.SetTexture(GroundEnemy, True);

  while Window.isOpen do
    begin
      while SfmlRenderWindowPollEvent(Window.Handle, Event) do
        begin
          if Event.EventType = sfEvtClosed then  // Window closed
            SfmlRenderWindowClose(Window.Handle);
          // Escape key pressed
          if (Event.EventType = sfEvtKeyPressed) and (Event.Key.Code = sfKeyEscape) then
            SfmlRenderWindowClose(Window.Handle);
        end;
      // Display the sprites.
      Window.Draw(SpriteBackground);
      GroundEnemyHeight := SpriteGroundEnemy.LocalBounds.Height;
      SpriteGroundEnemy.Position := SfmlVector2f(WINDOW_WIDTH - (FrameCounter mod 580),
                                                 WINDOW_HEIGHT - GroundEnemyHeight);
      Window.Draw(SpriteGroundEnemy);
      Window.Display;
      sfmlSleep(sfmlMilliSeconds(20));
      inc(FrameCounter);
    end;
end.
    
Programming - a skill for life!

How to use the Simple Fast Media Library in Lazarus