Fade Transition

BlitzPlus Forums/BlitzPlus Beginners Area/Fade Transition

One way of doing a blend:


Graphics 640,480,16,1

img%=LoadImage("X:\sprite.png")
bck%=LoadImage("X:\background.jpg")
MaskImage img%,0,255,0

While Not KeyDown(1)
   For t#=0 To 1 Step 0.1
      DrawImage bck%,0,0
      Flip
      draw_alpha(img%,MouseX(),MouseY(),0.3)
      Flip
   Next 
Wend

Function draw_alpha(image%, x%, y%, alpha#)
  ; if the opacity is 100% then no point in rendering the opaque image
  ; just draw original image

  If alpha# >= 1 Then
    han_x% = ImageXHandle(image%)
    han_y% = ImageYHandle(image%)
    HandleImage image%, 0, 0
    DrawImage image%, x%, y%
    HandleImage image%, han_x%, han_y%

  ; else if the opacity is greater than zero (if less the 0 don't bother drawing it!)
  Else If alpha# > 0 Then
    nalpha# = 1 - alpha# 
    ; Lock the buffers
    LockBuffer BackBuffer()
    LockBuffer ImageBuffer(image%)
    ; Loop through each pixel in the image, 
    ; To create New opaque image pixel by pixel
    For xx% = 0 To ImageWidth(image%)
      For yy% = 0 To ImageHeight(image%)
        ; Get colors from sprite pixel
        rgb1% = ReadPixelFast(xx%, yy%, ImageBuffer(image%))
        If rgb1% <> 0 Then
          r1% = ((rgb1 And 16711680) Shr 16)
          g1% = ((rgb1 And 65280) Shr 8)
          b1% = (rgb1 And 255)
          rgb2% = ReadPixelFast(xx + x, yy + y, BackBuffer())
          r2% = ((rgb2 And 16711680) Shr 16)
          g2% = ((rgb2 And 65280) Shr 8)
          b2% = (rgb2 And 255)

          ; Calculate the color of the new opaque pixel and draw pixel to new image
          r3 = r1%*alpha# + r2%*nalpha#
          g3 = g1%*alpha# + g2%*nalpha#
          b3 = b1%*alpha# + b2%*nalpha# 
          WritePixelFast(xx + x, yy + y, ((r3 Shl 16) + (g3 Shl 8) + b3), BackBuffer())
        End If
      Next
    Next
    ; Unlock the buffers
    UnlockBuffer BackBuffer()
    UnlockBuffer ImageBuffer(image%) 
  End If
End Function