Skip to content

Instantly share code, notes, and snippets.

@michaliskambi
Created March 16, 2017 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaliskambi/f79eeaeec9e8768e6885a6bb3b15f6fd to your computer and use it in GitHub Desktop.
Save michaliskambi/f79eeaeec9e8768e6885a6bb3b15f6fd to your computer and use it in GitHub Desktop.
Play animation from an image sequence (like a GIF animation) using Castle Game Engine
uses SysUtils,
CastleControls, CastleWindow, CastleTimeUtils, CastleUIControls,
CastleGLImages, CastleVideos;
type
TMap = class(TUIControlSizeable)
private
Video: TGLVideo2D;
AnimationTime: TFloatTime;
public
procedure GLContextOpen; override;
procedure GLContextClose; override;
procedure Render; override;
procedure Update(const SecondsPassed: Single;
var HandleInput: boolean); override;
end;
procedure TMap.GLContextOpen;
begin
inherited;
Video := TGLVideo2D.Create('giphy4.gif', true);
{ Adjust FPS if you like.
Alternatively, you could scale the time passage by scaling added SecondsPassed
in the Update method. }
//Video.FramesPerSecond := 4;
Video.TimeLoop := true;
end;
procedure TMap.GLContextClose;
begin
FreeAndNil(Video);
inherited;
end;
procedure TMap.Render;
begin
inherited;
{ display 1st animation }
Video.GLImageFromTime(AnimationTime).Draw(10, 10);
{ display 2nd animation, alwats 1 second later, just because we can }
Video.GLImageFromTime(AnimationTime + 1).Draw(400, 10);
end;
procedure TMap.Update(const SecondsPassed: Single;
var HandleInput: boolean);
begin
inherited;
AnimationTime += SecondsPassed;
end;
var
Map: TMap;
Window: TCastleWindowCustom;
begin
LoadAnimatedGifs := true;
Window := TCastleWindowCustom.Create(Application);
Window.Controls.InsertFront(TCastleSimpleBackground.Create(Application));
Map := TMap.Create(Application);
Map.FullSize := true; // capture mouse clicks from the entire window
Window.Controls.InsertFront(Map);
Window.OpenAndRun;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment