UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on February 20, 2025, 10:21:20 AM

Title: TheGameCreators Banner Animation Rendering Code (TGC)
Post by: kevin on February 20, 2025, 10:21:20 AM
  TheGameCreators Banner Animation Rendering Code  (TGC)


  This PlayBASIC program creates a dynamic banner display using images from a folder. The banners move toward the viewer (zooming down the Z-axis), while also fading in and gently oscillating using a sine wave effect. A scrolling version of the banners is displayed in the background for added visual interest.

How It Works:

1. Loading Banners
  - The program reads all image files from a specified folder.
  - Each image is loaded and stored in an array for use in the animation.

2. Setting Up the Scene
  - A camera is created to manage depth effects.
  - The main display area is prepared with a scrolling banner panel in the background.

3. Animating the Banners 
  - Each banner moves forward along the Z-axis.
  - A sine wave effect modifies their vertical position to create a floating motion.
  - Transparency (alpha) is adjusted based on depth to create a smooth fade effect.

4. Background Scrolling Panel
  - A separate function continuously scrolls banner images in the background.

5. Rendering the Scene
  - The banners are drawn in 3D perspective, adjusting size and position based on depth.
  - The program continuously updates and redraws the screen to create smooth animation.

6. User Interaction
  - Holding the mouse button speeds up the animation.
  - Pressing the spacebar exits the program.

  This program is a great example of using PlayBASIC for simple 3D-like effects with 2D images, using depth, transparency, and movement to create a visually engaging display.



 


  [pbcode]

    positionscreen -9,getscreenypos() 
    setmouse mousex(),100 

        //  INSERT THE LOCAtION HERE
    global path$="C:\Location_Of_Banner\images\"
    readdir path$,"",0,0
   

    Dim Vert#(256)

    FrameRate = 30
    z#=200

    type tBanner
                x#,y#
                file$
                image
                depth#       

                Alpha#
                Angle#
    Endtype

    dim Banner as tBanner list


    global finalimage =-1

    Dim ImageCache(10000)

      // display the results
    For Files=0 To GetDirSize()-1
        If GetDirFileType(files)=1
          file$=GetDirFile$(files)
                AddBannerFromFile(file$)
        EndIf
    Next

    Camera = newcamera()
    cameracls camera,off
   
    scenemaxzdepth 10000

    SceneDEpth#=7000
    ClipNearZ# = 400

    Baseypos#=GetScreenHeight()*1.10
   

    for each banner()
        FinalImage =Banner.image
        exit
    next

    setfps 30
    swingradius#=100
   
    do

        FRameStep=1
        if  mousebutton()=1
                FRameStep=25
        endif
        cls 0
       
            lockbuffer
            DrawBannerPanel(BaseXpos,BaseYpos#)
            unlockbuffer
       
        BaseYpos#-=(0.70*FRameStep)

        setcursor 0,0
        capturetoscene
        clsscene
       
       
            for each banner()
                image=banner.image
                y#=banner.y
                z#=banner.depth+8*FrameStep
               
                if Image=FinalImage
                    if z#>550
                        z#=550
                        swingradius#=curvevalue(0,swingradius#,20)
                    endif
                endif
               
               
                angle#=wrapangle(banner.angle+(2.5*FrameStep))
                y#=(cos(Angle#)*SwingRadius#)
                if Z#>50
                        Alpha#=1
                        if z#>(SceneDEpth#/2)
                            Alpha# = 1-((SceneDEpth#-z#)/(SceneDEpth#/2))
                        endif
                        if z#<ClipNearZ#
                            Alpha# = z#/ClipNearZ#
                        endif
                        capturedepth z#
                        DrawImageZ(IMage,0,y#,z#,alpha#)
                endif   


                banner.depth = z#

                banner.angle=angle#
                if Z#>SceneDEpth#
                    Banner = null
                    continue                   
                endif
            next



            drawcamera camera
       
        CurrentSprite=GetFirstSprite()
        while CurrentSprite
                NextSprite=GetNextSprite(CurrentSprite)
                deletesprite CurrentSprite
                CurrentSprite=NextSprite           
        EndWhile


        sync
    loop spacekey()


Function DrawBannerPanel(BaseXpos,BaseYpos)
        ScreenWidth=GetSurfaceWidth()
        Width=400
        Height=80
        Count=ImageCache(0)
        for lp =1 to Count-1
;;                if (BaseYpos+ypos)>=0
                    image =ImageCache(lp)
                    drawimage image,BaseXpos+Xpos,BaseYpos+Ypos,false
    ;;            endif
                Xpos+=Width
                if (Xpos>ScreenWidth)
                    Xpos=0
                        Ypos+=Height               
                endif
        next

EndFunction



Function AddBannerFromFile(File$)
    image = loadnewimage(path$+file$,true)

    // store this image in the cache of banner
    Index=ImageCache(0)+1
    ImageCache(index) = image
    ImageCache(0)=Index

    Static Z,angle#
    Z-=200
    angle#+=10
    AddBanner(Image,0,0,Z,angle#)   

ENdfunction


Function AddBanner(Image,X#,y#,Depth#,angle#)
    Banner = new tBanner
    Banner.image =image
    banner.x=    x#
    banner.y=    y#
    banner.depth = depth#   
    banner.angle#    =wrapangle(angle#)
   
ENdfunction




Function DrawImageZ(IMage,x#,y#,z#,Alpha#)
   
        ProjectX#=2000
        ProjectY#=600
        cx=getsurfacewidth()/2
        cy=getsurfaceheight()/2

        TextureWidth =GetimageWidth(image)
        TextureHeight =GetimageHeight(image)
       
        AR#        = (2080.0/1920)
        Width        =1920
        Height    = Width*AR#

        Index=0
        Vert#(Index) = 0 - (Width/2)  : index++
        Vert#(Index) = y# - (Height/2)  : index++

        Vert#(Index) = 0 + (Width/2)  : index++
        Vert#(Index) = Y# - (Height/2)  : index++
   
        Vert#(Index) = 0 + (Width/2)  : index++
        Vert#(Index) = y# + (Height/2)  : index++

        Vert#(Index) = 0 - (Width/2)  : index++
        Vert#(Index) = y# + (Height/2)  : index++

        for lp =0 to 3
                index = lp *2
                wx#=Vert#(Index)           
                wy#=Vert#(Index+1)           
                sx#=(wx#*ProjectX#)/z#
                sy#=(wy#*ProjectY#)/z#
                Vert#(Index)        = sx#   
                Vert#(Index+1)        = sy#
        next

        x1#=Vert#(0)
        y1#=Vert#(1)

        x2#=Vert#(2)
        y2#=Vert#(3)

        x3#=Vert#(4)
        y3#=Vert#(5)

        x4#=Vert#(6)
        y4#=Vert#(7)

        ThisRGB = RgbDepthCue(-1, rgb(0,0,0), z#, 4000, 6000)
        if ThisRGB
            spr=newsprite(cx+(x1#+x2#)/2,cy+(y1#+y4#)/2,image)
            spritedrawmode spr,2+04   
            spritetint spr,ThisRGB
            spritealphalevel spr,Alpha#
            spritefilter spr,true
            scalespritex spr, (x2#-X1#) /Width     
            scalespritey spr, (y4#-y1#) /Height   
            centerspritehandle spr
            drawsprite spr
        endif
ENdFunction




  [/pbcode]

  Note: This code requires a series of banners (shown in video) but are NOT supplied !