unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,thorvg_capi, Math;
type
{ TForm1 }
TForm1 = class(TForm)
Label1: TLabel;
PaintBox1: TPaintBox;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure PaintBox1Paint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
tvgCanvas: Tvg_Canvas;
tvgPaint: Tvg_Paint;
tvgAnimation: Tvg_Animation;
progress: Single;
duration: Single;
frame_count: Single;
b: TBitmap;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.FormCreate(Sender: TObject);
var
r: Tvg_Result;
Mask1: TFPUExceptionMask;
begin
b:=TBitmap.Create;
b.PixelFormat := pf32bit;
//b.SetSize(PaintBox1.Width, PaintBox1.Height);
b.Width:= PaintBox1.Width;
b.Height:= PaintBox1.Height;
Mask1 := SetExceptionMask(GetExceptionMask + [exOverflow,exZeroDivide,exInvalidOp]);
try
r := tvg_engine_init(4);
finally
SetExceptionMask(Mask1);
end;
tvgCanvas := tvg_swcanvas_create(TVG_ENGINE_OPTION_DEFAULT);
tvg_swcanvas_set_target(tvgCanvas, PUInt32(b.RawImage.Data), PaintBox1.Width, PaintBox1.Width, PaintBox1.Height, TVG_COLORSPACE_ABGR8888S);
tvgAnimation:=tvg_lottie_animation_new();
tvgPaint:=tvg_animation_get_picture(tvgAnimation);
tvg_picture_load(tvgPaint, 'cat.json');
tvg_animation_get_duration(tvgAnimation, @duration);
tvg_animation_get_total_frame(tvgAnimation, @frame_count);
tvg_picture_set_size(tvgPaint, PaintBox1.Width, PaintBox1.Height);
tvg_canvas_add(tvgCanvas, tvgPaint);
Timer1.Enabled:=True;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
b.Free;
end;
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
Mask2: TFPUExceptionMask;
begin
b.BeginUpdate;
Mask2 := SetExceptionMask(GetExceptionMask + [exOverflow,exZeroDivide,exInvalidOp]);
try
tvg_animation_set_frame(tvgAnimation, frame_count * progress/duration);
finally
SetExceptionMask(Mask2);
end;
tvg_canvas_update(tvgCanvas);
tvg_canvas_draw(tvgCanvas, True);
tvg_canvas_sync(tvgCanvas);
b.EndUpdate(True);
PaintBox1.Canvas.Draw(0,0,b);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
progress:=progress + Timer1.Interval*(1.0/1000.0);
if progress>duration then progress:=0.0;
PaintBox1.Invalidate;
end;
end.