AlphaDrawing functions

BlitzPlus Forums/BlitzPlus Programming/AlphaDrawing functions

InvincibleWall(Posted 2011) [#1]
okay from the help on my last post I think I have got this transparency thing down.... could you guys tell me how I did??

Conv (ConvR,ConvG,ConvB... ext) functions were provided by Yasha


these are the same thing


Const ScreenWidth = 800
Const ScreenHeight = 600

;-------------------------------------------------------------------------------------------------
;Small Demo
Graphics ScreenWidth,ScreenHeight


While Not KeyHit(1)
   MX = MouseX()
   MY = MouseY()
   Angle = Angle + 5
   If Angle > 360
       Angle = 0
   End If
   Cls
   Color 255,255,255
   Rect 200,200,100,100
   LockBuffer()
   RectAlpha(250,250,100,100,100,200,200,.5)
   FadedCircleAlpha(50 * (Cos(Angle)) + MX,50 * (Sin(Angle)) + MY,30,255,160,0,.5)
   FadedCircleAlpha(50 * (Cos(Angle + 90)) + MX,50 * (Sin(Angle + 90)) + MY,30,255,255,0,.5)
   FadedCircleAlpha(50 * (Cos(Angle + 180)) + MX,50 * (Sin(Angle + 180)) + MY,30,200,0,0,.5)
   FadedCircleAlpha(50 * (Cos(Angle + 270)) + MX,50 * (Sin(Angle + 270)) + MY,30,0,0,170,.5)
   UnlockBuffer()
   Flip
Wend

EndGraphics
End
;----------------------------------------------------------------------------------------------------


;-----------------------------------------------------------------------------------------
;uses PlotAlpha(x,y,r,g,b,a) to alter the color of a  Faded Circle on the screen accorrding to the rgb entered by the percent of Alpha (a value between 1 and 0)
;Because of PlotAlpha() this Function requires the buffer to be locked with LockBuffer()
;Because of PlotAlpha() this Function also requires the Height and Width of the screen stored to referencing
Function FadedCircleAlpha(x,y,ra,r,g,b,a#)
   Local alpha#,d
   For x1 = 0 To ra * 2
       For y1 = 0 To ra * 2
           d = (x1 - ra) * (x1 - ra) + (y1 - ra) * (y1 - ra)
           Alpha = (1 - (Float(d)/Float(ra * ra))) * a
           If Alpha > 0
               If d <= ra * ra
                   PlotAlpha(x1 + x - ra,y1 + y - ra,r,g,b,alpha)
               End If
           End If
       Next
   Next
End Function

;-----------------------------------------------------------------------------------------
;uses PlotAlpha(x,y,r,g,b,a) to alter the color of a Circle on the screen accorrding to the rgb entered by the percent of Alpha (a value between 1 and 0)
;Because of PlotAlpha() this Function requires the buffer to be locked with LockBuffer()
;Because of PlotAlpha() this Function also requires the Height and Width of the screen stored to referencing
Function CircleAlpha(x,y,ra,r,g,b,a#)
   Local d
   For x1 = 0 To ra * 2
       For y1 = 0 To ra * 2
           d = (x1 - ra) * (x1 - ra) + (y1 - ra) * (y1 - ra)
           If d <= ra * ra
               PlotAlpha(x1 + x - ra,y1 + y - ra,r,g,b,a)
           End If
       Next
   Next
End Function

;-----------------------------------------------------------------------------------------
;uses PlotAlpha(x,y,r,g,b,a) to alter the color of a rectangle on the screen accorrding to the rgb entered by the percent of Alpha (a value between 1 and 0)
;Because of PlotAlpha() this Function requires the buffer to be locked with LockBuffer()
;Because of PlotAlpha() this Function also requires the Height and Width of the screen stored to referencing
Function RectAlpha(x,y,w,h,r,g,b,a#)
   For x1 = 0 To w
       For y1 = 0 To h
           PlotAlpha(x1 + x,y1 + y,r,g,b,a)
       Next
   Next
End Function

;-----------------------------------------------------------------------------------------
;alters the color of a pixel on the screen accorrding to the rgb entered by the percent of Alpha (a value between 1 and 0)
;This Function requires the buffer to be locked with LockBuffer()
;This Function also requires the Height and Width of the screen stored to referencing
Function PlotAlpha(x,y,r,g,b,a#)
   If x < 0 Then Return False
   If x >= ScreenWidth Then Return False
   If y < 0 Then Return False
   If y >= ScreenHeight Then Return False
   Local argb = ReadPixelFast(x,y,BackBuffer())
   Local red1 = ConvR(argb)
   Local green1 = ConvG(argb)
   Local blue1 = ConvB(argb)
   Local red2 = red1 + ((r - red1) * a)
   Local green2 = green1 + ((g - green1) * a)
   Local blue2 = blue1 + ((b - blue1) * a)
   argb = ConvRGBInt(red2,green2,blue2,255)
   WritePixelFast(x,y,argb,BackBuffer())
End Function

;-----------------------------------------------------------------------------------------
;get Red value out of color integer
Function ConvR%(argb%) 
   Return (argb And $FF0000) Shr 16
End Function

;-----------------------------------------------------------------------------------------
;get Green value out of color integer
Function ConvG%(argb%)
   Return (argb And $FF00) Shr 8
End Function

;-----------------------------------------------------------------------------------------
;get Blue value out of color integer
Function ConvB%(argb%)
   Return (argb And $FF)
End Function

;-----------------------------------------------------------------------------------------
;get Alpha value out of color integer
Function ConvA%(argb%)
   Return (argb And $FF000000) Shr 24
End Function

;-----------------------------------------------------------------------------------------
;Convert Red,Green,Blue,Alpha into argb color integer
Function ConvRGBInt(r,g,b,a)
   Return (a Shl 24) Or (r Shl 16) Or (g Shl 8) Or b
End Function

;----------------------------------------------------------------------------------------- 


Last edited 2011

Last edited 2011


InvincibleWall(Posted 2011) [#2]
Um? Did anyone see this?


Andy_A(Posted 2011) [#3]
When it comes to displaying graphics, faster is almost always better.

Ditching the RGB functions, and in-lining the one line statements in the PlotAlpha() function, gives a 10% speed increase.


By the way, the ConvA() function is never used.

Other than those nitpiks, the alpha-blending works fine.


GregH(Posted 2011) [#4]
InvincibleWall,

Good job. Alpha Blending isn't something you see every day in BlitzPlus. Its good to see I'm not the only one still using BlitzPlus. The only thing I changed in your demo was:

;Small Demo
Graphics ScreenWidth,ScreenHeight,0,2

I did this so BlitPlus would run in a window at the native screen depth. BlitzPlus just seems to me like it should be run in a window because of what it is...BlitzBasic with a Windows GUI.

I'm still working on getting a handle of BlitzMax and OOP but I keep going back to BlitzPlus to whip out simple Windows apps.


InvincibleWall(Posted 2011) [#5]
Cool, yah faster is always better...
Do you think these are fast enough to be used in a game as engine exhaust, planet glow and lasers?

I made a small test with them with 3 missiles with engine exhaust and a background of 100 pulsating stars (radii of 1-3) and it worked fine on my computer (which isn't very fast) until I started using circles with a radius over 25

And I'm not quite sure how to test speed of a program (accurately)... So I don't know how much faster it is to use CircleAlpha instead of FadedCircleAlpha

Lol GregH I use BlitzPlus cuz that's all I have... I'm Using it as an elective in Highschool :)
Though I am thinking a great deal about getting Bmax or B3d because of B+ limitations (such as this slow alpha blending thing)

Thx for the input guys


Andy_A(Posted 2011) [#6]
It's your demo but with timing code to see which is faster, FadeCircle or Circle (and the tweak to PlotAlpha).

L-click to toggle between Fade and Circle.



Last edited 2011


InvincibleWall(Posted 2011) [#7]
Cool thx for the code Andy_A


Andy_A(Posted 2011) [#8]
no prob.

Now, get to work on that game! :)


InvincibleWall(Posted 2011) [#9]
well The alpha functions cause exsesive amounts of lag, so they have been ditched =(

the effects they made in small amounts were amazing though =)


Andres(Posted 2011) [#10]
is your space background black? if it is, you could for example try draw the planet glow as a plain image (no alpha) and then the stars with alpha.

this would be much faster, if it could be done like that.

edit:
here's my TGA functions with alpha: http://www.blitzbasic.com/codearcs/codearcs.php?code=1814

it only draws the alpha pixels with plotting and the no alpha pixels are drawn with blitzes DrawImage(), maybe it will help you too in some cases.

Last edited 2011