The gif animation freezes

Just starting out? Need help? Post your questions and find answers here.
punak
User
User
Posts: 63
Joined: Tue Sep 07, 2021 12:08 pm

The gif animation freezes

Post by punak »

In pb original example, I added a loop to it, but the gif animation freezes until the loop is finished. I tried it with thread but I couldn't, what should I do to prevent the gif from freezing?

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - ImagePlugin GIF Viewer example file
;
;    (c) Fantaisie Software
;
; ------------------------------------------------------------
;
Global a

Procedure myLoop(ar)
  Repeat
    a+1
  Until a = 100000000
EndProcedure


; Enable the GIF decoder
UseGIFImageDecoder()

; Loading a GIF file
If LoadImage(0, #PB_Compiler_Home+"Examples/Sources/Data/PureBasicLogo.gif")

  OpenWindow(0, 100, 100, ImageWidth(0), ImageHeight(0), "GIF viewer")
 
  CanvasGadget(0, 0, 0, ImageWidth(0), ImageHeight(0))
  
  ; Add a timer to animate the GIF, starts immediately to display the first frame witout delay
  AddWindowTimer(0, 0, 1) 
  
  myLoop(0) 
  ;WaitThread(CreateThread(@myLoop(),0))
  
  Repeat
    Event = WaitWindowEvent()
   
    If Event = #PB_Event_Timer
      SetImageFrame(0, Frame)
     
      ; Each GIF frame can have its own delay, so change the timer accordingly
      ;
      RemoveWindowTimer(0, 0)
      AddWindowTimer(0, 0, GetImageFrameDelay(0))
     
      If StartDrawing(CanvasOutput(0))
        DrawImage(ImageID(0), 0, 0)
        StopDrawing()
      EndIf
      
      ; Go to next frame
      Frame+1
      If Frame >= ImageFrameCount(0) ; Cycle back to first frame, to play in loop
        Frame = 0
      EndIf
    EndIf
   
  Until Event = #PB_Event_CloseWindow
Else
  Debug "Impossible to load the file: " + Filename$
EndIf
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: The gif animation freezes

Post by BarryG »

I'd just show the GIF in a WebGadget() and not worry about having to manually loop it:

Code: Select all

OpenWindow(0,200,200,400,400,"",#PB_Window_SystemMenu)
gif$=#PB_Compiler_Home+"Examples/Sources/Data/PureBasicLogo.gif"
WebGadget(0,10,10,400,400,"about:<html><body scroll='no' leftmargin='0' topmargin='0'><img src='"+gif$+"'></img></body></html>")
DisableGadget(0,#True)
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
punak
User
User
Posts: 63
Joined: Tue Sep 07, 2021 12:08 pm

Re: The gif animation freezes

Post by punak »

Thanks :)
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: The gif animation freezes

Post by StarWarsFan »

Is there any way to show an animated .GIF without using the Webgadget()?
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
firace
Addict
Addict
Posts: 899
Joined: Wed Nov 09, 2011 8:58 am

Re: The gif animation freezes

Post by firace »

Here's my super secret alternative(tested on Windows)

Code: Select all


Procedure AnimGifGadget(g, x, y, w, h, path$)
  
  InitMovie()
  
  LoadMovie(1, path$)
  
  ResizeMovie(1, x, y, w, h)
  
  PlayMovie(1,WindowID(1)) 
  
EndProcedure   


OpenWindow(1,0, 0, 640, 400,"")

AnimGifGadget(1, 0, 0, 320, 200, "C:\path\animatedimage.gif")

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: The gif animation freezes

Post by kenmo »

punak, it's no problem with the GIF code, you're just not using the thread correctly!

If you call myLoop(0), you're not creating a background thread, you're just running the whole function which takes ~10 seconds for me.

If you call WaitThread(CreateThread(@myLoop(),0)), you ARE creating a thread but then waiting the full 10 seconds for it to finish. Equivalent to the prevoius.

Just call CreateThread(@myLoop(),0) and the GIF will loop while your thread runs. Stick a Debug "Thread done" at the end of myLoop() to see it work.
User avatar
Inner
PureBasic Expert
PureBasic Expert
Posts: 714
Joined: Fri Apr 25, 2003 4:47 pm
Location: New Zealand

Re: The gif animation freezes

Post by Inner »

Was looking at this myself from what I've been able to understand with PB5.73, formats are limited GIF 8Bit animation/images ONLY, but 16/32Bit gifs just fails to load, so it's working but only half working.

:oops: I discovered after hex editing some sample image data that the .gif that I thought was a .gif was actually a webp file renamed to .gif, sorry my bad.
Last edited by Inner on Sun Oct 24, 2021 11:07 pm, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: The gif animation freezes

Post by netmaestro »

I wrote a combination gif decoder/animation gadget, it's here: viewtopic.php?f=12&t=57656

Here's a zip containing the needed code and a bunch of animated gifs to test. Download it to an empty folder, unzip then find the 'anigadget.pb' file and run it. It will make you turn off the debugger and turn threadsafe on. If you do that to begin with you won't have to look at annoying boxes yelling at you. https://lloydsplace.com/anitest.zip

Holy crap I can't believe it's been 8 years since I wrote that! I had to look twice at the date, I couldn't believe it. I was expecting it to be maybe 3 years or so :oops:
BERESHEIT
User avatar
Inner
PureBasic Expert
PureBasic Expert
Posts: 714
Joined: Fri Apr 25, 2003 4:47 pm
Location: New Zealand

Re: The gif animation freezes

Post by Inner »

Your link appears to be be dead, netmaestro
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: The gif animation freezes

Post by netmaestro »

I tested it before I posted and I just tested again, it works for me. I don't know what to say about that. Maybe try some more?
Update: my host seems to be spotty on the connection today, it'll probably work later. But the file is definitely there spelled correctly.
BERESHEIT
User avatar
Inner
PureBasic Expert
PureBasic Expert
Posts: 714
Joined: Fri Apr 25, 2003 4:47 pm
Location: New Zealand

Re: The gif animation freezes

Post by Inner »

netmaestro wrote: Mon Oct 25, 2021 12:02 am I tested it before I posted and I just tested again, it works for me. I don't know what to say about that. Maybe try some more?
Update: my host seems to be spotty on the connection today, it'll probably work later. But the file is definitely there spelled correctly.
Working now, we'll just blame all the people at home due to covid. :)

edit: just threw it all together, that's really impressive work there.
User avatar
ChrisR
Addict
Addict
Posts: 1127
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: The gif animation freezes

Post by ChrisR »

I had no problem downloading before either!
It works great
Image
Post Reply