AnimatedGIFSprite

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AnimatedGIFSprite

Post by infratec »

Can someone check with my small tool if on macOS the PB GIFDecoder returns correct results for the FrameDelay?
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

This Mac is crazy, and very limited for use with PB... I removed the Invisibility Flag on the Window, trying to figure why the image become only a black frame and why I don't see the real frame with the dog. UAU...: - The Mac completedly Frozed!!! I had to cold boot it from Power button. It was a completely crash out, no ESC, no Task Kill, no Force Quit, nothing... Power-OFF, Power-ON.

And my Windows machine is not home now... BUAAaaa!
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

infratec wrote: Sun Jan 22, 2023 12:33 pm Can someone check with my small tool if on macOS the PB GIFDecoder returns correct results for the FrameDelay?
Well, I'm getting Zero, but most animated gifs have a zero-delay! I'll re-check!
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AnimatedGIFSprite

Post by infratec »

On Windows:
animated-dog-image-0175.gif

Size: 145x72
Frames: 7
FrameDelay: 100ms
If FrameDelay = 0, the code never draws the image on the sprite.
So you see only a black area.
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

You have to check every frame for delays.
Very quick and fast change on your small tool and I get different values, because the first frame seems to be the one considered when you check the ImageDelay, but after that you get different values.

I've looped 20 times, but we can change the 20 to Number of Frames!

With the next code (yours with small changes), I got this answer:

"animated-dog-image-0175.gif
Size: 145x72
Frames: 7
FrameDelay: 0ms , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100ms"

NOTE: With 20 times, it repeats the last value until loop ends!

Hope it helps!

Code: Select all

EnableExplicit

Define Filename$, Message$, X

UseGIFImageDecoder()

Filename$ = OpenFileRequester("Choose an animated GIF", "", "GIF|*.gif", 0)
If Filename$
  If LoadImage(0, Filename$)
    Message$ = GetFilePart(Filename$) + #LF$ + #LF$
    Message$ + "Size: " + Str(ImageWidth(0)) + "x" + Str(ImageHeight(0)) + #LF$
    Message$ + "Frames: " + Str(ImageFrameCount(0)) + #LF$
    Message$ + "FrameDelay: " + Str(GetImageFrameDelay(x)) + "ms"
    For X=0 To ImageFrameCount(0)
       SetImageFrame(0,X)
       Message$ + " , " + Str(GetImageFrameDelay(0))
    Next X
    Message$ + "ms" 
    MessageRequester("GIF Info", Message$)
    FreeImage(0)
  Else
    MessageRequester("Error", "Error loading GIF" + #LF$ + #LF$ + Filename$)
  EndIf
EndIf
Last edited by Kamarro on Sun Jan 22, 2023 1:57 pm, edited 2 times in total.
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

It looks that on Windows you read the Last Frame and on Mac I read the First Frame!!!
I changed the last code of yours to FrameCount instead of 20.

The result is now correct:
animated-dog-image-0175.gif

Size: 145x72
Frames: 7
FrameDelay: 0ms , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100ms
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AnimatedGIFSprite

Post by infratec »

you read that the file has 7 frames.
And my code shows only one FrameDelay.

You are showing 9 FrameDelays :?: :?: :?:
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

Code: Select all

   *AnimatedGIFSprite = AddMapElement(AnimatedGIFSprite\GIFInfoMap(), Str(Sprite))
      *AnimatedGIFSprite\OrgImage = Image
      *AnimatedGIFSprite\Sprite = Sprite
      *AnimatedGIFSprite\Frames = ImageFrameCount(Image)
      *AnimatedGIFSprite\FrameDelay = GetImageFrameDelay(Image)
      If *AnimatedGIFSprite\FrameDelay = 0 : *AnimatedGIFSprite\FrameDelay = 1 : EndIf
I just added a quick change and now I have Dogs running very fast and well...
I just changed the last line; it was an added line, not a real change!
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

With this changes, I get 8 values, like if the image as one more frame considered the EndGif or something like that.
And now I don't have the Zero
animated-dog-image-0175.gif

Size: 145x72
Frames: 7
FrameDelay: 100 - 100 - 100 - 100 - 100 - 100 - 100 - 100 - ms

Code: Select all

EnableExplicit

Define Filename$, Message$, X

UseGIFImageDecoder()

Filename$ = OpenFileRequester("Choose an animated GIF", "", "GIF|*.gif", 0)
If Filename$
  If LoadImage(0, Filename$)
    Message$ = GetFilePart(Filename$) + #LF$ + #LF$
    Message$ + "Size: " + Str(ImageWidth(0)) + "x" + Str(ImageHeight(0)) + #LF$
    Message$ + "Frames: " + Str(ImageFrameCount(0)) + #LF$
    Message$ + "FrameDelay: "
    For X=0 To ImageFrameCount(0)
       SetImageFrame(0,X)
       Message$ + Str(GetImageFrameDelay(0)) + " - " 
    Next X
    Message$ + "ms" 
    MessageRequester("GIF Info", Message$)
    FreeImage(0)
  Else
    MessageRequester("Error", "Error loading GIF" + #LF$ + #LF$ + Filename$)
  EndIf
EndIf
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AnimatedGIFSprite

Post by infratec »

Yur modification is still wrong.
You need:

Code: Select all

For X=0 To ImageFrameCount(0) - 1
Since the index starts from 0
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AnimatedGIFSprite

Post by infratec »

I changed the code in the first post.

Now it reflects that each frame can have its own delay.
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

This Mac is crazy... If I read the Frame Delay of the image it reads 0, but if I read the frame 0, it says 100. It's like if he couldn't read the frame delay without setting the frame, or if there was a -1 frame, before frame 0.

I believe it's a small incompatibility with my MacOS, or something else. I tested with one of my big GIfs, with 343 frames, and it happens the same!
Crazy thing with FrameCount!!!
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

infratec wrote: Sun Jan 22, 2023 2:12 pm Yur modification is still wrong.
You need:

Code: Select all

For X=0 To ImageFrameCount(0) - 1
Since the index starts from 0
Of course... stupid guy.... If frames 7, starting on 0... YES!
infratec
Always Here
Always Here
Posts: 7619
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: AnimatedGIFSprite

Post by infratec »

Changed the code from first post:

Now the first frame is shown and then wait the delay befor the next frame is shown.
Also the different behaviour from windows and macOS is solved by first set the (first) frame
before getting the frame delay.
User avatar
Kamarro
User
User
Posts: 65
Joined: Thu Mar 19, 2015 7:55 pm
Location: England

Re: AnimatedGIFSprite

Post by Kamarro »

Yes, it's perfect now, even on a very old Mac... Wonderful work, man!

I had to do this, to test this things on a lot of GIFs with different sizes, frames and delays... It's completely working fine, thanks to your Small Tool, with just a little bit of repeatable options. I am talking about the code at the end of this Reply. Thanks.

Now, I'm cheking if I can use something like your procedures on my game, but I believe that it will be slower than I wish, but I can try it. Problem is that I have gifs, like this one:
ScanLand.gif

Size: 588x440
Frames: 121
FrameDelay: 30 - 30 - 20 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 40 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 30 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 20 - 30 - 30 - 30 - 30 - 20 - 30ms.
Or this one:
Radar.gif

Size: 800x600
Frames: 299
FrameDelay: 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20 - 20ms.
See my problem? And I also have to put Sprites on top of this GIFs while running... Maybe, I'll have to put aside the idea of using Animated Gifs!
Thanks a lot for your help.

Code: Select all

EnableExplicit
Define Filename$, Message$, X, FIN
UseGIFImageDecoder()

Repeat
Filename$ = OpenFileRequester("Choose an animated GIF", "", "GIF|*.gif", 0)
If Filename$
   If LoadImage(0, Filename$)
      Message$ = GetFilePart(Filename$) + #LF$ + #LF$
      Message$ + "Size: " + Str(ImageWidth(0)) + "x" + Str(ImageHeight(0)) + #LF$
      Message$ + "Frames: " + Str(ImageFrameCount(0)) + #LF$
      Message$ + "FrameDelay: "
      If MessageRequester("Loop or Not","Use the LOOP?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
         For X=0 To ImageFrameCount(0)-1
            SetImageFrame(0,X)
            Message$ + Str(GetImageFrameDelay(0))
            If X<ImageFrameCount(0)-1 : Message$ + " - " : EndIf
         Next X
         Message$ + "ms."
      Else
         Message$ + Str(GetImageFrameDelay(0)) + "ms"
      EndIf       
      MessageRequester("GIF Info", Message$)
      FreeImage(0)
   Else
      MessageRequester("Error", "Error loading GIF" + #LF$ + #LF$ + Filename$)
   EndIf
EndIf
If MessageRequester("AGAIN?","Do it again with another image?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_No
   FIN=1
Else
   FreeImage(#PB_All)
EndIf

Until FIN=1
Last edited by Kamarro on Sun Jan 22, 2023 3:54 pm, edited 1 time in total.
Post Reply