Drawing Textures (Images)

Introduction

The demonstration uses the images of the background wall and enemy. (The wall is also used for rendering on the faces of a cube in WebGL and the enemy is taken from George Wright's TowerOfArcher). The next page includes a demonstration of a motion graphic with the enemy moving along the ground.

Part of the Captured Image

Part of the Captured Image

The Code

program TextureDemo;
{$Apptype Gui}
uses
  SysUtils, SfmlGraphics, SfmlSystem, SfmlWindow;
const
  WINDOW_WIDTH = 512;
  WINDOW_HEIGHT = 512;
var
  Window: TSfmlRenderWindow;
  Background, GroundEnemy: PSfmlTexture;
  SpriteBackground, SpriteGroundEnemy: TSfmlSprite;
  OutputImage: PsfmlImage;
  GroundEnemyHeight: real;
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);
  // Display the sprites.
  Window.Draw(SpriteBackground);
  GroundEnemyHeight := SpriteGroundEnemy.LocalBounds.Height;
  SpriteGroundEnemy.Position := SfmlVector2f(200, WINDOW_HEIGHT - GroundEnemyHeight);
  Window.Draw(SpriteGroundEnemy);

  // Capture the output
  OutputImage := Window.Capture;
  SfmlImageSaveToFile(OutputImage, 'TextureOutputImage.png');

  Window.Display;
  sfmlSleep(sfmlSeconds(15));
  halt;
end.
    
Programming - a skill for life!

How to use the Simple Fast Media Library in Lazarus