SpriteAlphaLevel
SpriteAlphaLevel SpriteIndex, AlphaLevel#
 
Parameters:

    SpriteIndex = The identifier of the sprite you wish to alter of the Draw Mode of
    AlphaLevel# = The Alpha Blending level for this sprite.
Returns: NONE
 

      SpriteAlphaLevel sets the Alpha blending level property of a sprite. For this property to take any visible effect, the sprite must be is set to render in AlphaMode (see SpriteDrawMode).

      Alpha Blending is a process where each pixel in the sprites image is being scaling and then added to pixel it's going to overwrite, which has been inversely scaled. This process makes the blended sprite image appear to be translucent, rather than opaque. While the process blends with the backdrop, the sprite image isn't altered.


AlphaLevel Ranges (0.0 to 1.0)
      0.00 = Blend is completely the background
      0.25 = Blend is 75% background and 25% foreground
      0.50 = Blend is 50% back and foreground
      0.75 = Blend is 75% foreground and 25% background
      1.00 = Blend is completely the foreground


      Bellow is an example of ship blending with a blue-ish coloured background. The sprite is blending by 10 percent each time through the full range of 0.0 to 1.0 and back again to 0.0.






FACTS:


      * When AlphaLevel is set to zero. The sprite is not draw at all.

      * When AlphaLevel is set to one or above. The sprite will be drawn in normal BlitMode.

      * Drawing sprite in Alpha Mode requires that the sprites image to be stored in FX format. See PrepareFXImage, LoadFxImage

      * Drawing Alpha Blended sprites directly to the screen (in PlayBASIC V1.64 and bellow) can be a slow process. Due to PlayBasic needing to read video memory. This can be overcome by rendering to a screen size FX image, then transferring this image to the screen. (The attached example uses this approach)




Mini Tutorial:


      This example creates a blank FX image, creates a new sprite, sets the sprite to alpha blend mode with a Alpha Blend Level of %50 (0.5). It then displays the sprite and the blend level.

  
  
; Clear the Screen to a light blue colour
  Cls RGB(0,100,200)
  
  
; Create an blank Image. The pixels in the image will be black)
  MyImage=NewFXImage(300,200)
  
  
; Create Sprite
  MySprite=NewSprite(300,200,MyImage)
  
; Activate Alpha Blending as the Sprites draw mode
  SpriteDrawMode MySprite, 4
  
; Turn off transparent mode in the sprite
  SpriteTransparent mySprite, off
  
  
; Set this sprite to Alpha Blending Level 0.5 (50%)
  SpriteAlphaLevel MySprite,0.5
  
; Display the current Alpha Blending Level
  Print "Blend Level:"+Str$(GetSpriteAlphaLevel(MySprite))
  
; draw the sprite to the screen
  DrawSprite MySprite
  
  
; Display the screen and wait for a key press
  Sync
  WaitKey
  
  


This example would output.

  
  Blend Level:0.5
  




 
Example Source: Download This Example
` +===========================================*
` SpriteAlphaLevel.  (SpriteDrawModes)
` +===========================================*
  
  
; Create a type array to store some info
;  about our characters
  Type tBubble
     Sprite
     XSpeed#,YSpeed#
     FadeAngle#
  EndType
  
; Dim a link list called Bubble from type tBubble
  Dim Bubble As tBubble List
  
  
  
  For starsI=1 To 500
     
   ; Add new type ot the list
     Bubble = New tBubble
     
   ; init it's Speed values
     Bubble.xspeed           = RndRange(1,5)
     Bubble.yspeed           = RndRange(1,5)
     Bubble.FadeAngle     = Rnd(360)
     
     
   ; Create the sprite this bubble character will use
     x=Rnd(GetScreenWidth())
     y=Rnd(GetScreenWidth())
     img=CreateBallImage(32)
     Spr=NewSprite(x,y,img)
     CenterSpriteHandle Spr
     
   ;Set Sprite to Alpha Blend mode
     SpriteDrawMode starsi,4
     
     
   ; Remember this sprite
     Bubble.Sprite=Spr
     
     
  Next
  
  
; Create a Screen Sized FX image that we'll use
; for faster  Alpha blend in PlayBasic V1.63 an bellow
  FxScreen=NewFXImage(GetScreenWidth(),GetScreenHeight())
  
  
; Create the backdrop picture
  BackDrop=CreateBackDropIMage()
  
  
  SetFPS 60.7
  
; Start programs Main loop
  Do
     
   ; Direct All Rendering to the FX image.
     RenderToImage FxScreen
     
   ; Refresh the backdrop
     DrawImage BackDrop,0,0,false
     
     
   ; Manage the bubbles in the bubble list
     For Each Bubble()
        
      ; get this
        spr=Bubble.Sprite
        
      ; MOve the sprite
        x#=GetSpriteX(spr)+bubble.Xspeed
        y#=GetSpriteY(spr)+bubble.Yspeed
        
      ; Check if the sprite hits the bounds of the screen
        If Range(X#,0,800)=false  Then bubble.xspeed=-bubble.xspeed
        If Range(Y#,0,600)=false  Then bubble.yspeed=-bubble.yspeed
        
      ; Set sprites position
        PositionSprite Spr,X#,Y#
        
        Angle#=WrapAngle(Bubble.FadeAngle,3)
        
      ; Set this sprite to Alpha blend in and out
        SpriteAlphaLevel Spr,CosNewValue(0.5,angle#,0.45)
        
        Bubble.FadeAngle=Angle#
        
     Next
     
   ; DRaw All the Sprites
     DrawAllSprites
     
   ; Redirect Drawing to the SCREEN
     RenderToScreen
     
   ; DRaw the FX image Screen, to the real screen
     DrawImage FxScreen,0,0,false
     
   ; Display Frame rate
     CenterText GetScreenWidth()/2,20,"FRAMERATE: "+Str$(FPS())
     
   ; Refresh the display so the user can see it
     Sync
     
   ; loop back to the DO to keep the program running
  Loop
  
  
  
  
  
  
; =========================================================
; Create an Image with a shaded circle pattern on it.
; =========================================================
  
Function CreateBallImage(Size)
  ThisImage=NewFXImage(size,size)
  Colour=RGB(128+Rnd(127),128+Rnd(127),128+Rnd(127))
  RenderPhongImage ThisIMage,size/2,size/2,Colour,200,255.0/(size/2)
EndFunction ThisImage
  
  
  
Function CreateBackDropIMage()
  
; Create a backDrop image
  BackDrop=NewFXImage(GetScreenWidth(),GetScreenHeight())
  
  RenderToImage BackDrop
  
; Clear the Screen
  Cls RGB(0,0,45)
  
; Draw Some Colour Boxes to the screen
  For lp =0 To 20
     x1=Rnd(800)
     y1=Rnd(600)
     x2=Rnd(800)
     y2=Rnd(600)
     c1=RndRGB()
     c2=RndRGB()
     c3=RndRGB()
     c4=RndRGB()
     ShadeBox x1,y1,x2,y2,c1,c2,c3,c4
  Next
  
EndFunction BackDrop
  
 
Related Info: DrawAllSprites | DrawSprite | PrepareFXImage | SpriteDrawMode :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com