Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.