'
' GD library creating an animated GIF
'
' (c) PvE, May 2010 - GPL
'-----------------------------------------------

INCLUDE "gd.bac"

CONST xrange = 640
CONST yrange = 400

CONST speed = 10

DECLARE im[xrange + 40]
DECLARE x, y_1, y_2, y_3 TYPE FLOATING

' Create first frame
im[0] = gdImageCreate(xrange, yrange)

' Allocate colors. First color is background color.
white = gdImageColorAllocate(im[0], 255, 255, 255)
black = gdImageColorAllocate(im[0], 0, 0, 0)
blue = gdImageColorAllocate(im[0], 0, 0, 255)
red = gdImageColorAllocate(im[0], 255, 0, 0)
yellow = gdImageColorAllocate(im[0], 255, 255, 0)

' We write our frames to this GIF file
OPEN "gd-anim.gif" FOR WRITING AS out

' Write header, 1=global colormap, 0=infinite looping
gdImageRectangle(im[0], 0, 0, xrange-1, yrange-1, black)
gdImageGifAnimBegin(im[0], out, 1, 0)
gdImageGifAnimAdd(im[0], out, 0, 0, 0, speed, gdDisposalNone, 0)

' Create animation
FOR x = 1 TO xrange + 40 STEP 5

    ' We need an integer type to index our array
    idx = x

    ' Create next image
    im[idx] = gdImageCreate(xrange, yrange)
    gdImageColorAllocate(im[idx], 255, 255, 255)
    gdImagePaletteCopy(im[idx], im[0])

    ' Square around our animation
    gdImageRectangle(im[idx], 0, 0, xrange-1, yrange-1, black)

    ' Calculate circles
    y_3 = SIN((x-40)/50) * (yrange/2 - 10)
    gdImageFilledArc(im[idx], x - 40, yrange/2 - y_3, 20, 20, 0, 360, yellow, gdArc)
    gdImageArc(im[idx], x - 40, yrange/2 - y_3, 20, 20, 0, 360, black)

    y_2 = SIN((x-20)/50) * (yrange/2 - 15)
    gdImageFilledArc(im[idx], x - 20, yrange/2 - y_2, 30, 30, 0, 360, red, gdArc)
    gdImageArc(im[idx], x - 20, yrange/2 - y_2, 30, 30, 0, 360, black)

    y_1 = SIN(x/50) * (yrange/2 - 20)
    gdImageFilledArc(im[idx], x, yrange/2 - y_1, 40, 40, 0, 360, blue, gdArc)
    gdImageArc(im[idx], x, yrange/2 - y_1, 40, 40, 0, 360, black)

    ' Add this image to animation
    gdImageGifAnimAdd(im[idx], out, 0, 0, 0, speed, gdDisposalRestoreBackground, im[idx-1])

NEXT

' Write the end marker
gdImageGifAnimEnd(out)

CLOSE FILE out