Ooops
November 30, 2020, 01:36:55 AM

Author Topic: [bb] 2D fade by jhocking [ 1+ years ago ]  (Read 592 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2D fade by jhocking [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : 2D fade
Author : jhocking
Posted : 1+ years ago

Description : This code will fade an image to and from black.  Unlike doing fades with Gamma this works in both fullscreen and windowed mode.  Unlike the 3D graphics trick of varying alpha on a black sprite this works in 2D graphics mode.

This code is based on a function darklordz posted in the "2D Graphics" forum (but I/we have no idea who first wrote it.)  The posted function only worked in fullscreen; I modified it to work windowed too.


Code :
Code: BlitzBasic
  1. Graphics 800,600,32,2
  2. SetBuffer BackBuffer()
  3.  
  4. logo = LoadImage("facing.jpg")
  5.  
  6. Blend_Black(logo,0,0,0,1000)
  7. time=MilliSecs()+1000
  8. While MilliSecs()<time
  9.         DrawBlock logo,0,0
  10.         Flip
  11. Wend
  12. Blend_Black(logo,0,0,1,1000)
  13.  
  14. While Not KeyHit(1)
  15. Wend
  16. End
  17.  
  18. ;------------------------------------------------------------------
  19. ; Blends an Image with a Black Background (In & Out)
  20. ; -----------------------------------------------------------------
  21. ; Image = The Image to Blend
  22. ; x     = X Coordinate in the Screen
  23. ; y     = Y Coordinate in the Screen
  24. ;         50000 for both to center image
  25. ; Mode  = 0 Blend In
  26. ;       = 1 Blend Out
  27. ; Time  = Time in Milliseconds
  28. ;------------------------------------------------------------------
  29.  
  30. Function Blend_Black(image,x,y,mode,time)
  31.         source = ImageBuffer(image)
  32.         dest   = BackBuffer()
  33.         maxx   = ImageWidth(image)-1
  34.         maxy   = ImageHeight(image)-1
  35.         start  = MilliSecs()
  36.        
  37.         LockBuffer source
  38.        
  39.         If X = 50000 And y = 50000
  40.                 x = (GraphicsWidth()/2)-(ImageWidth(image)/2)
  41.                 y = (GraphicsHeight()/2)-(ImageHeight(image)/2)
  42.         EndIf
  43.        
  44.         While MilliSecs()-start<time
  45. Cls
  46.                 LockBuffer dest
  47.                 count=(count+1) Mod 4
  48.                 If count=0 Then minx=0: miny=0
  49.                 If count=1 Then minx=1: miny=1
  50.                 If count=2 Then minx=1: miny=0
  51.                 If count=3 Then minx=0: miny=1
  52.                 If mode=0 Then value=MilliSecs()-start
  53.                 If mode=1 Then value=time-MilliSecs()+start
  54.                 For ii=miny To maxy Step 2
  55.                         For i=minx To maxx Step 2
  56.                                 rgb=ReadPixelFast(i,ii,source)
  57.                                 r=(rgb And $FF0000)/$10000
  58.                                 g=(rgb And $FF00)/$100
  59.                                 b=rgb And $FF
  60.                                 rgb=r*value/time*65536+g*value/time*256+b*value/time
  61.                                 WritePixelFast x+i,y+ii,rgb,dest
  62.                         Next
  63.                 Next
  64.                 UnlockBuffer dest
  65.                 Flip
  66.         Wend
  67.         UnlockBuffer source
  68.         If mode=0 Then DrawBlock image,x,y
  69.         If mode=1 Then Color 0,0,0: Rect x,y,maxx+1,maxy+1
  70.         Flip
  71. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal