UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on January 20, 2006, 11:41:01 PM

Title: Water Line Effect
Post by: kevin on January 20, 2006, 11:41:01 PM
This example requires PlayBASIC V1.11 or above

(original version of code)
[pbcode]


 Makebitmapfont 1,$ffffff


; ================================
; Create Water Shape  ( WAVE :))
; ================================
   WaterLineShape=GetFreeshape()
 
 Createshape WaterLineShape, 100,100
 
 WaveHeight=20
 Xpos=0
 FirstVertex=10
 Vertex=FirstVertex
 For angle=0 to (360*4) step 5
    ypos=cos(Baseangle#+angle)*WaveHeight
    SetShapeVertex Waterlineshape,Vertex,xpos,ypos     
    xpos=xpos+20
    inc vertex
 next    
 lastVertex=Vertex-1
 
 x=GetShapeVertexX(Waterlineshape,FirstVertex)
 y=GetShapeVertexy(Waterlineshape,FirstVertex)
 SetShapeVertex Waterlineshape,1,x,y     
 SetShapeVertex Waterlineshape,2,x,1000     

 SetShapeVertex Waterlineshape,3,xpos,y     
 SetShapeVertex Waterlineshape,4,xpos,1000     

 EdgeINdex=1
 SetShapeedge WaterlineShape,EdgeINdex,1,2
 inc edgeindex
 SetShapeedge WaterlineShape,EdgeINdex,3,4

 for Vertex=FirstVertex to lastVertex-1
    inc Edgeindex     
    SetShapeedge WaterlineShape,EdgeINdex,Vertex,Vertex+1
 next  
   

   




;=========================================================================
   `CReate a Series of Tiles To make the blob Animation blocks.
;=========================================================================
 
   Frames=10
   BlockSize=64
; Create the Animated blocks
   Block_Image=CreateBlocks(BlockSize,frames)  


; Create a Map with provision For 2 level maps To be created using it's Blocks
   Thismap=NewMap(2)
   
; Import the above Image created into our Map as BLocks
   MakeMapGFX ThisMap,Block_image,BlockSize,BlockSize,frames+1,RGB(0,0,0)


; Prepare the Block graphics for FX rendering.  This Allows Maps to be
; drawn to the images converted to FX format.  
   PrepareFXMap ThisMap
   

; =========================
; Create 3 Animations
; =========================
   MaxAnims=6
   Create_Map_Anims(ThisMap,MAxAnims,Frames)


; CREATE A LEVEL filled with ranomd blocks
   Constant z=$80000000
   Width=100
   height=100
   CreateLevel ThisMap,1,Width,height
   For ylp=0 To Width
  For xlp=0 To height
    PokeLevelTile ThisMap,1,xlp,ylp,z Or RndRange(1,MaxAnims)
  Next xlp
   Next ylp
   
; SET THIS LEVEL As Transparent + animated..
   LevelAnimated ThisMap,1




;=========================================================================
; Create an FX Image the size of the screen.
;=========================================================================
   
; This image is used as render target.  So we render all our games
; elements to this image, then copy it to the real screen.
   
   sw=getscreenwidth()
   sh=getscreenheight()

   FXScreen=NEwImage(sw,sh)
   PrepareFXIMage FXscreen
   Rendertoimage FXscreen
    
   createcamera 1


  ShowControls=timer()+10000


;=========================================================================
; MAIN LOOp
;=========================================================================

; Start of Main Loop
   rendertoscreen   
   Do   

; Tell pPB to capture the following drawn items to the scene buffer
    capturetoscene
      ; clear the buffer
    clsscene
      ; draw our map to the scene buffer
    DrawMap thisMap,1,0,0

      ; draw the camera.  This will process the items in the scene buffer
      ; to the cameras position and render what it can see to the image
      ; it's attached to.  In this case it's attached to the FXscreen
      ; image
    DrawCamera 1


; Redirect drawing to the FX screen image
    RendertoIMage FXScreen

; set the INK mode to Alpha 50%
    inkmode 1+32

; set the ink colour to a bluey colour
    ink rgb(35,75,125)

; draw the wave shape over the map
    drawshape WaterLineShape,0,sh/2,2
    
      ; restore the ink mode to normal
        inkmode 1

      ; restore rendering to the screen
    rendertoscreen


; ================================================
; Cycle water vertex
; ================================================
    Vertex=FirstVertex
    Xpos=0
    vertex=10
    For angle=0 to (360*4) step 10   
   ypos=cos(baseAngle#+angle)*(WaveHeight*sin(angle))
   ypos=ypos+(cos((baseAngle#)+(angle*3) )*(WaveHeight/2))
   SetShapeVertex Waterlineshape,Vertex,xpos,ypos     
   xpos=xpos+20
   inc vertex
    next    

    sh=getscreenheight()
    x=GetShapeVertexX(Waterlineshape,FirstVertex)
    y=GetShapeVertexy(Waterlineshape,FirstVertex)
    SetShapeVertex Waterlineshape,1,x,y     
    SetShapeVertex Waterlineshape,2,x,sh+1  
    SetShapeVertex Waterlineshape,3,xpos,y     
    SetShapeVertex Waterlineshape,4,xpos,sh+1

    Baseangle#=Wrapangle(Baseangle#,1)

;
    drawimage FXscreen,0,0,0
    
    if upkey() then MoveCamera 1,0,-5
    if Downkey() then MoveCamera 1,0,5
    if Rightkey() then MoveCamera 1,5,0
    if Leftkey() then MoveCamera 1,-5,0


    
    CenterText GetScreenWidth()/2,0,"Rendering Water Effect Over FX Map"

    text 0,0,fps()


; if the demo has been running for less than 10 second then show
; the controls message
     if ShowControls>timer()
        centertext sw/2,sh/2,"Use Arrows to Scroll Window"
     EndiF

; updtae the map animations
    UpdateMapAnims Thismap

    Sync
 Loop

   End
   

;=========================================================================
; Create the Colour Blocks
;=========================================================================
   

Function CreateBlocks(BlockSize,frames)  
; Create a Image to Use to Store out Blob tiles upon
   Map_Image=NewImage(BlockSize*(Frames+1),BlockSize)
; CReate the set of Blob
   For lp=0 To frames
 i=NewImage(BlockSize,BlockSize)   
 c=rndrgb()
 RenderPhongImage i,GetImageWidth(i)/2,GetImageHeight(i)/2,RGBFade(c,50+((40.0/frames)*lp)),200,255.0/(blocksize-lp)
 RenderToImage Map_image
 DrawImage i,blocksize*(lp+1),0,0     
 Deleteimage i  
   Next
   RenderToScreen  
EndFunction Map_image


;=========================================================================
; Create the Mpa Animations
;=========================================================================
   


FUnction Create_Map_Anims(ThisMap,MAxAnims,Frames)
; Create provision For 3 Animations
 MapAnimQuantity ThisMap,MaxAnims

 For Anims=1 To MaxAnims
 For lp=1 To frames
    MapAnim_Add_Block ThisMap,Anims,lp
 Next  
 For lp=frames To 1 Step -1
    MapAnim_Add_Block ThisMap,Anims,lp
 Next  
 MapAnimFrameRate ThisMap,Anims,anims
 MapAnimType ThisMap,Anims,1
   Next Anims
EndFunction


;=========================================================================
; Helper function to treat ading map anims like a que
;=========================================================================

Function MapAnim_Add_Block(ThisMap,ThisAnim,ThisBlock)
   If GetMapAnimStatus(ThisMap,ThisAnim)=False
 CreateMapAnim ThisMap,ThisAnim,1
 PokeMapAnim ThisMap,ThisAnim,0,ThisBlock
   Else
 size=GetMapAnimSize(thisMap,thisAnim)
 PokeMapAnim ThisMap,ThisAnim,Size,ThisBlock
 ResizeMapAnim ThisMap,ThisAnim,Size+1
   EndIf
EndFunction



[/pbcode]



  Updated version of code for PlayBASIC V1.64 editions.  You can also find this in the project packs (see PlayBASIC.com (http://www.playbasic.com/downloads.php))

[pbcode]

 Makebitmapfont 1,$ffffff,8


; ================================
; Create Water Shape  ( WAVE :))
; ================================
 WaterLineShape=Newshape(150,150)
 
 WaveHeight=20
 Xpos=0
 FirstVertex=10
 Vertex=FirstVertex
 For angle=0 to (360*4) step 5   
   if Vertex <150
        ypos=cos(Baseangle#+angle)*WaveHeight
        SetShapeVertex Waterlineshape,Vertex,xpos,ypos     
        xpos=xpos+20
        inc vertex
   endif
   
 next    
 lastVertex=Vertex-1
 
 x=GetShapeVertexX(Waterlineshape,FirstVertex)
 y=GetShapeVertexy(Waterlineshape,FirstVertex)
 SetShapeVertex Waterlineshape,1,x,y     
 SetShapeVertex Waterlineshape,2,x,1000     

 SetShapeVertex Waterlineshape,3,xpos,y     
 SetShapeVertex Waterlineshape,4,xpos,1000     

 EdgeINdex=1
 SetShapeedge WaterlineShape,EdgeINdex,1,2
 inc edgeindex
 SetShapeedge WaterlineShape,EdgeINdex,3,4

 for Vertex=FirstVertex to lastVertex-1
    inc Edgeindex     
    SetShapeedge WaterlineShape,EdgeINdex,Vertex,Vertex+1
 next  
   

   




;=========================================================================
   `CReate a Series of Tiles To make the blob Animation blocks.
;=========================================================================
 
   Frames=10
   BlockSize=64
; Create the Animated blocks
   Block_Image=CreateBlocks(BlockSize,frames)  


; Create a Map with provision For 2 level maps To be created using it's Blocks
   Thismap=NewMap(2)
   
; Import the above Image created into our Map as BLocks
   MakeMapGFX ThisMap,Block_image,BlockSize,BlockSize,frames+1,RGB(0,0,0),2


; =========================
; Create 3 Animations
; =========================
   MaxAnims=6
   Create_Map_Anims(ThisMap,MAxAnims,Frames)


; CREATE A LEVEL filled with ranomd blocks
   Width=100
   height=100
   CreateLevel ThisMap,1,Width,height
   For ylp=0 To Width
      For xlp=0 To height
           PokeLevelTile ThisMap,1,xlp,ylp,PBMapAnim_Mask Or RndRange(1,MaxAnims)
      Next xlp
   Next ylp
   
; SET THIS LEVEL As Transparent + animated..
   LevelAnimated ThisMap,1



;=========================================================================
; Create an FX Image the size of the screen.
;=========================================================================
   
; This image is used as render target.  So we render all our games
; elements to this image, then copy it to the real screen.
   
   sw=getscreenwidth()
   sh=getscreenheight()

   FXScreen=NEwImage(sw,sh,2)
   Rendertoimage FXscreen
    
   createcamera 1


  ShowControls=timer()+10000


;=========================================================================
; MAIN LOOp
;=========================================================================

; Start of Main Loop
   rendertoscreen   
   Do   

    ; Tell pPB to capture the following drawn items to the scene buffer
        capturetoscene
          ; clear the buffer
        clsscene
          ; draw our map to the scene buffer
        DrawMap thisMap,1,0,0

      ; draw the camera.  This will process the items in the scene buffer
      ; to the cameras position and render what it can see to the image
      ; it's attached to.  In this case it's attached to the FXscreen
      ; image
        DrawCamera 1


    ; Redirect drawing to the FX screen image
        RendertoIMage FXScreen

    ; set the INK mode to Alpha 50%
        inkmode 1+32

    ; set the ink colour to a bluey colour
        ink rgb(35,75,125)

    ; draw the wave shape over the map
        drawshape WaterLineShape,0,sh/2,2
    
      ; restore the ink mode to normal
        inkmode 1
          ; restore rendering to the screen
        rendertoscreen



;
    drawimage FXscreen,0,0,0



; ================================================
; Cycle water vertex
; ================================================
    Vertex=FirstVertex
    Xpos=0
    vertex=10
    For angle=0 to (360*4) step 10   
      if Vertex<150
       ypos=cos(baseAngle#+angle)*(WaveHeight*sin(angle))
       ypos=ypos+(cos((baseAngle#)+(angle*3) )*(WaveHeight/2))
       SetShapeVertex Waterlineshape,Vertex,xpos,ypos     
       xpos=xpos+20
       inc vertex
      endif
    next    

    sh=getscreenheight()
    x=GetShapeVertexX(Waterlineshape,FirstVertex)
    y=GetShapeVertexy(Waterlineshape,FirstVertex)
    SetShapeVertex Waterlineshape,1,x,y     
    SetShapeVertex Waterlineshape,2,x,sh+1  
    SetShapeVertex Waterlineshape,3,xpos,y     
    SetShapeVertex Waterlineshape,4,xpos,sh+1

    Baseangle#=Wrapangle(Baseangle#,1)

    
    if upkey() then MoveCamera 1,0,-5
    if Downkey() then MoveCamera 1,0,5
    if Rightkey() then MoveCamera 1,5,0
    if Leftkey() then MoveCamera 1,-5,0


    ink -1
    CenterText GetScreenWidth()/2,0,"Rendering Water Effect Over FX Map"

    text 0,0,fps()



; if the demo has been running for less than 10 second then show
; the controls message
     if ShowControls>timer()
        centertext sw/2,sh/2,"Use Arrows to Scroll Window"
     EndiF

; updtae the map animations
    UpdateMapAnims Thismap

    Sync
 Loop

   End
   

;=========================================================================
; Create the Colour Blocks
;=========================================================================
   

Function CreateBlocks(BlockSize,frames)  
; Create a Image to Use to Store out Blob tiles upon
   Map_Image=NewImage(BlockSize*(Frames+1),BlockSize)
; CReate the set of Blob
   For lp=0 To frames
 i=NewImage(BlockSize,BlockSize,2)   
 c=rndrgb()
 RenderPhongImage i,GetImageWidth(i)/2,GetImageHeight(i)/2,RGBFade(c,50+((40.0/frames)*lp)),200,255.0/(blocksize-lp)
 RenderToImage Map_image
 DrawImage i,blocksize*(lp+1),0,0     
 Deleteimage i  
   Next
   RenderToScreen  
EndFunction Map_image


;=========================================================================
; Create the Mpa Animations
;=========================================================================
   


FUnction Create_Map_Anims(ThisMap,MAxAnims,Frames)
; Create provision For 3 Animations
 MapAnimQuantity ThisMap,MaxAnims

 For Anims=1 To MaxAnims
 For lp=1 To frames
    MapAnim_Add_Block ThisMap,Anims,lp
 Next  
 For lp=frames To 1 Step -1
    MapAnim_Add_Block ThisMap,Anims,lp
 Next  
 MapAnimFrameRate ThisMap,Anims,anims
 MapAnimType ThisMap,Anims,1
   Next Anims
EndFunction


;=========================================================================
; Helper function to treat ading map anims like a que
;=========================================================================

Function MapAnim_Add_Block(ThisMap,ThisAnim,ThisBlock)
   If GetMapAnimStatus(ThisMap,ThisAnim)=False
 CreateMapAnim ThisMap,ThisAnim,1
 PokeMapAnim ThisMap,ThisAnim,0,ThisBlock
   Else
 size=GetMapAnimSize(thisMap,thisAnim)
 PokeMapAnim ThisMap,ThisAnim,Size,ThisBlock
 ResizeMapAnim ThisMap,ThisAnim,Size+1
   EndIf
EndFunction

[/pbcode]


Related Articles

    * Applying a Classic Sine wave effect to an image  (http://www.underwaredesign.com/forums/index.php?topic=320.0)