News:

Function Finder  Find all the functions within source code files

Main Menu

PlayBASIC Code - AFX Alpha Channel Blending - Cross Fading Dogs AVA and GODA

Started by kevin, May 09, 2022, 12:34:22 PM

Previous topic - Next topic

kevin

  PlayBASIC Source Code - AFX Alpha Channel Blending - Cross Fading Dogs AVA and GODA


    This demo creates a waving alpha channel on the foreground image which is then rendered over the background image.  Since the foreground is an AFX image it'll be alpha blended over the background.. Looks a lot more interesting than just a fixed variable alpha blend.   

     To do this in PlayBASIC V1.65 we have to create a strip image for the Alpha channel blending levels.  This is then added to the foreground image.  This is a bit tricky as DrawIMAGE doesn't seem to support Alpha Addition; so we have to resort to using sprites here.   Which do have Alpha addition rendering modes.   Which works none the less.

    NOTE: For this demo; you'll need to supply two Images that are same size..   



PlayBASIC Code: [Select]
openscreen 1200,800,32,1

positionscreen GetScreenXpos(),GetScreenYpos()*0.2


IMage =LoadNewaFxImage("Image1 - 1200by900.png") // INSERT THE FILE NAMES HERE

IMage2=LoadNewaFxImage("Image2 - 1200by900.png")




// CReate a strip we'll render alpha channel levels to
Strip=newfximage(GetImageWidth(Image),2)

// CReate sprite using out strip image, so we can render it
// using Alpha Addition
Sprite=Newsprite(0,0,Strip)
spritedrawmode Sprite, 2+ 16



// CReate an FX size image for our screen
Screen=NewImage(GetScreenWidth(),getScreenHeight(),true)

Do


// Compute the a strip of alpha blend levels
// These are just


rendertoimage Strip
w=getimagewidth(Image)
AStep#=255.0/W
for xlp=0 to w-1
Alpha=ClipRange(128+sin(Xlp*Astep#+Angle)*128,0,255)
Colour=Argb(Alpha,00,00,00)
dotc xlp,0,Colour
next
Angle++


// Mask off the Alpha channel from the foreground image
rendertoimage image
inkmode 1+256
boxc 0,0,GetSurfaceWidth(),GetSurfaceHeight(),true, Argb(0,255,255,255)


// Render the strip image (of alpha channel) over the foreground
// this is ADDED to the foreground image setting the alpha
// channel levesl for each picture in the image
For ylp=0 to GetSurfaceheight()-1
Positionsprite Sprite,0,ylp
drawsprite sprite
next
inkmode 1



// Render the background and foreground image to our
// FX screen image
rendertoimage Screen
drawimage Image2,0,0,false ; Draw backdrop SOLID
drawimage Image,0,-50,true ; Draw foreground Transparent Alpha BLENDED

// Render to screen to the PlayBASIC Screen
rendertoscreen
drawimage screen,0,0,false


sync
loop spacekey()=true







   



[ Credits ]

    Music: Synapse by Shane Ivers - https://www.silvermansound.com