Ooops
November 28, 2020, 11:29:09 AM

Author Topic: [bb] 2d Alpha for b3d by Nate the Great [ 1+ years ago ]  (Read 525 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2d Alpha for b3d by Nate the Great [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : 2d Alpha for b3d
Author : Nate the Great
Posted : 1+ years ago

Description : This program allows alpha blending on images.  It comes with two types of blending, addative and something that I am still figuring out a name for.  I would have found this very useful when I used to program 2d games.

Code :
Code: BlitzBasic
  1. Graphics 640,480,0,2
  2.  
  3. Dim aray(60,60,3)
  4.  
  5.  
  6. img = CreateImage(50,50)
  7. img1 = CreateImage(50,50)
  8. SetBuffer ImageBuffer(img)
  9. ClsColor 255,0,0
  10. Cls
  11. Color 0,255,0
  12. Rect 20,20,15,10
  13. SetBuffer ImageBuffer(img1)
  14. ClsColor 0,0,255
  15. Cls
  16.  
  17. SetBuffer BackBuffer()
  18. ClsColor 0,0,0
  19.  
  20. While Not KeyDown(1)
  21. Cls
  22.  
  23. DrawImage img1,MouseX(),MouseY()
  24.  
  25. DrawImage1(img,320,240,0,.5,1)
  26.  
  27. Flip
  28. Wend
  29. End
  30.  
  31. Function DrawImage1(name,x,y,frm = 0,alpha# = 1,alphabmode = 0,red = 255,green = 255,blue = 255)
  32.  
  33. If alphabmode > 0 Then
  34.  
  35.  
  36.  
  37. SetBuffer ImageBuffer(name,frm)
  38.  
  39. LockBuffer ImageBuffer(name,frm)
  40.  
  41. For x1 = 1 To ImageWidth(name)-1
  42.         For y1 = 1 To ImageHeight(name)-1
  43.                 rgb = ReadPixelFast(x1,y1)
  44.                 aray(x1,y1,0) = getred(rgb)
  45.                 aray(x1,y1,1) = getgreen(rgb)
  46.                 aray(x1,y1,2) = getblue(rgb)
  47.         Next
  48. Next
  49.  
  50. UnlockBuffer ImageBuffer(name,frm)     
  51.  
  52. SetBuffer BackBuffer()
  53.  
  54. LockBuffer BackBuffer()
  55.  
  56. For x1 = 1 To ImageWidth(name)-1
  57.         For y1 = 1 To ImageHeight(name)-1
  58.                 If alphabmode = 1 Then
  59.                         If x1 + x > 0 And x1+x < GraphicsWidth()-1 And y1 + y > 0 And y1 + y < GraphicsHeight()-1 Then
  60.                                 rgb = ReadPixelFast(x1+x,y1+y)
  61.                                 WritePixelFast(x1+x,y1+y,getrgb(getred(rgb)+aray(x1,y1,0)*alpha#*(red/255),getgreen(rgb)+aray(x1,y1,1)*alpha#*(green/255),getblue(rgb)+aray(x1,y1,2)*alpha#*(blue/255)))
  62.                         EndIf
  63.                 ElseIf alphabmode = 2 Then
  64.                         If x1 + x > 0 And x1+x < GraphicsWidth()-1 And y1 + y > 0 And y1 + y < GraphicsHeight()-1 Then
  65.                                 rgb = ReadPixelFast(x1+x,y1+y)
  66.                                 brt = getred(rgb)+getgreen(rgb)+getblue(rgb)
  67.                                 WritePixelFast(x1+x,y1+y,getrgb(getred(rgb)+aray(x1,y1,0)*alpha#*(red/255)*brt/765,getgreen(rgb)*aray(x1,y1,1)*alpha#*(green/255)*brt/765,getblue(rgb)*aray(x1,y1,2)*alpha#*(blue/255)*brt/765))
  68.                         EndIf
  69.                 EndIf
  70.         Next
  71. Next
  72. Else
  73. DrawImage name,x,y,frm
  74. EndIf
  75.  
  76. UnlockBuffer BackBuffer()
  77. End Function
  78.  
  79. Function GetRed(rgb)
  80.         Return rgb Shr 16 And %11111111
  81. End Function
  82. Function GetGreen(rgb)
  83.         Return rgb Shr 8 And %11111111
  84. End Function
  85. Function GetBlue(rgb)
  86.         Return rgb And %11111111
  87. End Function
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. Function GetRGB(red,green,blue)
  95.         Return blue Or (green Shl 8) Or (red Shl 16)
  96. End Function


Comments :


Nate the Great(Posted 1+ years ago)

 Sorry. I think there was a flaw in it earlier.  I fixed it now though.


Nate the Great(Posted 1+ years ago)

 Hmmm...Now it should workcould someone try this and tell me if it works


LamptonWorm(Posted 1+ years ago)

 Hi, works for me... good stuff.


Ryudin(Posted 1+ years ago)

 Thanks. This'll work perfectly for my semi-transparent cursor.By the way, what to the red, green, and blue values do? When I use them, my image disappears completely.


Nate the Great(Posted 1+ years ago)

 <a href="codearcs2fe9.html?code=2348#comments" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=2348#comments[/url]Ryudin... if you are using blitz3d then try this instead it is much faster and more flexible :)


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal