Lottie animations - [x64][x86]

Share your advanced PureBasic knowledge/code with the community.
punak
Enthusiast
Enthusiast
Posts: 107
Joined: Tue Sep 07, 2021 12:08 pm

Lottie animations - [x64][x86]

Post by punak »

Hi all,
With the help of AI and rlottie.dll x64 - rlottie.dll x86 file, I have prepared for you an example of loading Jason animations.
of course, I don't know why the animation speed doesn't change and we haven't found a solution yet.

GitHub repository address:
https://github.com/Samsung/rlottie

Code: Select all

; ======================================
;   Lottie Viewer - rlottie.dll + PureBasic
; ======================================

; === DLL Function Prototypes ===
PrototypeC.i lottie_animation_from_file(filename.p-utf8)
PrototypeC.i lottie_animation_get_totalframe(animation.i)
PrototypeC.i lottie_animation_get_framerate(animation.i)
PrototypeC.i lottie_animation_get_size(animation.i, *width, *height)
PrototypeC.i lottie_animation_render(animation.i, frame.i, buffer.i, width.i, height.i, stride.i)
PrototypeC.i lottie_animation_destroy(animation.i)

; === Load DLL ===
Global rlottieDLL = OpenLibrary(#PB_Any, "rlottie.dll")
If rlottieDLL = 0
  MessageRequester("Error", "rlottie.dll file not found.")
  End
EndIf

Global rlottie_load.lottie_animation_from_file = GetFunction(rlottieDLL, "lottie_animation_from_file")
Global rlottie_totalframe.lottie_animation_get_totalframe = GetFunction(rlottieDLL, "lottie_animation_get_totalframe")
Global rlottie_framerate.lottie_animation_get_framerate = GetFunction(rlottieDLL, "lottie_animation_get_framerate")
Global rlottie_size.lottie_animation_get_size = GetFunction(rlottieDLL, "lottie_animation_get_size")
Global rlottie_render.lottie_animation_render = GetFunction(rlottieDLL, "lottie_animation_render")
Global rlottie_destroy.lottie_animation_destroy = GetFunction(rlottieDLL, "lottie_animation_destroy")

; === Global Variables ===
Global animation.i, *buffer, stride.i
Global winW.i = Val(InputRequester("Image Width", "Enter desired width:", "500"))
Global winH.i = Val(InputRequester("Image Height", "Enter desired height:", "500"))
If winW <= 0 Or winH <= 0 : MessageRequester("Error", "Invalid size.") : End : EndIf
stride = winW * 4
*buffer = AllocateMemory(stride * winH)
If *buffer = 0 : MessageRequester("Error", "Not enough memory.") : End : EndIf

Global frame.i = 0, playing = #False, SpeedFactor.f = 1.0
Global nextFrameTime.q, frameDuration.f, totalFrames.i, framerate.f

; === Load Animation Procedure ===
Procedure LoadLottie(file.s)
  Shared animation, totalFrames, framerate, frameDuration
  If animation : rlottie_destroy(animation) : EndIf
  animation = rlottie_load(file)
  If animation = 0 : MessageRequester("Error", "Failed to load animation.") : ProcedureReturn #False : EndIf
  totalFrames = rlottie_totalframe(animation)
  framerate = rlottie_framerate(animation) : If framerate <= 0 : framerate = 30 : EndIf
  frameDuration = 1000.0 / framerate
  ProcedureReturn #True
EndProcedure

; === Select Initial File ===
Define file.s = OpenFileRequester("Select Lottie File", "", "Lottie JSON|*.json", 0)
If file = "" Or Not LoadLottie(file)
  FreeMemory(*buffer)
  CloseLibrary(rlottieDLL)
  End
EndIf

; === Create Window ===
If OpenWindow(0, 0, 0, winW, winH + 100, "Lottie Viewer", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ImageGadget(0, 0, 0, winW, winH, 0)
  ButtonGadget(1, 10, winH + 10, 80, 30, "Start")
  ButtonGadget(6, 100, winH + 10, 80, 30, "Open")
  TrackBarGadget(2, 190, winH + 10, winW - 200, 30, 0, totalFrames - 1)
  TextGadget(3, 10, winH + 50, 80, 20, "Speed:")
  TrackBarGadget(4, 80, winH + 50, winW - 180, 20, 25, 200)
  SetGadgetState(4, 100)
  TextGadget(5, winW - 90, winH + 50, 80, 20, "1.00x", #PB_Text_Right)
  CreateImage(10, winW, winH, 32, #PB_Image_Transparent)
  
  Repeat
    Define Event = WindowEvent()
    If Event = #PB_Event_Gadget
      Select EventGadget()
        Case 1 ; Start/Stop
          playing = Bool(Not playing)
          If playing
            SetGadgetText(1, "Stop")
            nextFrameTime = ElapsedMilliseconds() + frameDuration / SpeedFactor
          Else
            SetGadgetText(1, "Start")
          EndIf
          
        Case 6 ; Open
          file = OpenFileRequester("Select New File", "", "Lottie JSON|*.json", 0)
          If file <> "" And LoadLottie(file)
            frame = 0
            SetGadgetState(2, 0)
          EndIf
          
        Case 2 ; Seek
          If Not playing : frame = GetGadgetState(2) : EndIf
          
        Case 4 ; Speed
          SpeedFactor = GetGadgetState(4) / 100.0
          If SpeedFactor < 0.25 : SpeedFactor = 0.25 : EndIf
          SetGadgetText(5, StrF(SpeedFactor, 2) + "x")
          nextFrameTime = ElapsedMilliseconds() + frameDuration / SpeedFactor
      EndSelect
    EndIf
    
    ; === Playback ===
    If playing And ElapsedMilliseconds() >= nextFrameTime
      frame + 1 : If frame >= totalFrames : frame = 0 : EndIf
      SetGadgetState(2, frame)
      nextFrameTime = ElapsedMilliseconds() + frameDuration / SpeedFactor
      ; === Render Frame ===
      rlottie_render(animation, frame, *buffer, winW, winH, stride)
      If IsImage(10)
        StartDrawing(ImageOutput(10))
        Define *dest = DrawingBuffer()
        For y = 0 To winH - 1
          CopyMemory(*buffer + (winH - 1 - y) * stride, *dest + y * stride, stride)
        Next
        StopDrawing()
        SetGadgetState(0, ImageID(10))
      EndIf      
    EndIf
    
    Delay(1)
  Until Event = #PB_Event_CloseWindow
EndIf

; === Cleanup ===
rlottie_destroy(animation)
FreeMemory(*buffer)
CloseLibrary(rlottieDLL)
End
User avatar
idle
Always Here
Always Here
Posts: 5970
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Lottie animations - [x64][x86]

Post by idle »

That looks interesting thanks for sharing it
Post Reply