alpha blending
alpha blending
I like Put + Alpha because it's more cpu-friendly than my own alpha sub.
Plus, my sub uses 0-128 for alpha. (it still looks amazing)
But using my sub, I can get away with just having a background image, as my 'foreground' is procedural
Is there a similar approach which FB currently supports?
Plus, my sub uses 0-128 for alpha. (it still looks amazing)
But using my sub, I can get away with just having a background image, as my 'foreground' is procedural
Is there a similar approach which FB currently supports?
I haven't fully understood what you are trying to do (are you going to procedurally generate a background image?)
Anyway, Put function can be customized:
http://www.freebasic.net/wiki/wikka.php ... gCustomgfx
Anyway, Put function can be customized:
http://www.freebasic.net/wiki/wikka.php ... gCustomgfx
To be more clear, I am curious if the alpha blend that Put uses might also be accessible at the pixel level
Here is my sans-Put method:
Here is my sans-Put method:
Code: Select all
scrPTR = ScreenPtr
For y = 0 To HgtM
Dim row As UInteger Ptr = pixels_BG + y * pitch_BG
For x = 0 To WidM
AlphaColor *scrPTR, mPalette(),sngMap(x,y), row[x]
scrPTR += 1
Next
Next
With the RGBA macro, you can define colors that include alpha channel; and you can use them with all freebasic commands (line, circle, pset....); so, all freebasic commands can use alpha level.
On the graphic page, the alpha level is stored (as red, green and blue), but not used; anyway, it could be useful if you are going to change the page into a texture (in my OpenB3d, it just happens: you can draw on a 2d screen, and section with alpha level 0 will allow you to see the 3d image behind it: if you increase alpha level, the 2d screen will cover the 3d rendered image).
Also, you can use it in custom ways: look at this: http://freebasic.net/forum/viewtopic.php?t=16237
On the graphic page, the alpha level is stored (as red, green and blue), but not used; anyway, it could be useful if you are going to change the page into a texture (in my OpenB3d, it just happens: you can draw on a 2d screen, and section with alpha level 0 will allow you to see the 3d image behind it: if you increase alpha level, the 2d screen will cover the 3d rendered image).
Also, you can use it in custom ways: look at this: http://freebasic.net/forum/viewtopic.php?t=16237
I had thought of the inverse-alpha assigned to background, but wanted to explore other possibilities. I like my code to be as straight-forward as possible. Thanks for the links.
What I think I might do, as power-meter-friendly engine is my goal (I will take a peek at your engine) is use Rel's EasyGl with fb image support. Even with all that's going on there, my laptop barely heats up.
Also I'm tempted to run some tests on the fast blend codes I've seen. They say for example Red * (Alpha+ 1) Shr 8 but all that does is shift the un-evenness upward like a slide rule.
That's why I use 0 - 128. Of course you can have a whole separate array for the alpha channel.. I'm exploring my options atm
What I think I might do, as power-meter-friendly engine is my goal (I will take a peek at your engine) is use Rel's EasyGl with fb image support. Even with all that's going on there, my laptop barely heats up.
Also I'm tempted to run some tests on the fast blend codes I've seen. They say for example Red * (Alpha+ 1) Shr 8 but all that does is shift the un-evenness upward like a slide rule.
That's why I use 0 - 128. Of course you can have a whole separate array for the alpha channel.. I'm exploring my options atm
gfxlib2's alpha blender is not the fastest and it very easy to write your own faster one. It will require learning a lot about low-level assembler coding but after 4-5 years of experience at that you should be able to do it. No method you try to do written in a HLL will beat gfxlib2's underlying mmx blitters though. Simply put, you need to know how to code asm.
For reference, gfxlib2's alpha blitter uses dst += src - alpha.
For reference, gfxlib2's alpha blitter uses dst += src - alpha.
I wondered if FB's blend used mmx..
I coded 2 alpha subs .. my d*a + s*(255-a) is faster than my s + a*(d-s).
FB's blit seems to be about 15% faster than my fastest ..
Anyway, thanks for the input.
My goal is to have most cpu-friendly version, and if I need an extra buffer to have my routine use Put, then that is what will happen
I coded 2 alpha subs .. my d*a + s*(255-a) is faster than my s + a*(d-s).
FB's blit seems to be about 15% faster than my fastest ..
Anyway, thanks for the input.
My goal is to have most cpu-friendly version, and if I need an extra buffer to have my routine use Put, then that is what will happen
The most CPU friendly blending will be hardware accellerated by the GPU. Have you considered using CAIRO?dafhi wrote:My goal is to have most cpu-friendly version, ...
dafhi wrote:@TJF
... However it seems to be utilizing only the cpu
You may have to finetune your installation to make hardware acceleration available.http://cairographics.org/ wrote:Cairo is designed to produce consistent output on all output media while taking advantage of display hardware acceleration when available (eg. through the X Render Extension).
@ TJF - could you check this out and give pointers? (no pun intended)
"palettizer.bi"
"cairo_test.bas"
"put_test.bas"
"palettizer.bi"
Code: Select all
'' pre-release
' +--------------------------------------------------+ '
' | palettizer.bi v0.98 by cRex (2011 Aug 03) | '
' +----------------------------------------------=---+ '
' | palettizer writes multiple gradients | '
' | to a 32bpp palette | '
' +--------------------------------------------------+ '
Type GradientRibbon
As UShort Sections
As UShort widBase,widVari
As Integer UBPix
As Single Shatter
End Type
Type zTileAnimProperties
As Single x1,x2,A0,A1,iA0,iA1,tile,xStep,rotRadi
End type
Type ComponentAnimProps
As Single clipLo,clipHi,alphaLo,alphaHi
As GradientRibbon infoRibbon
As zTileAnimProperties tile
End Type
Dim Shared As UShort mWidth()
Dim Shared As Byte mShatter()
Dim Shared As Single m_yStart()
Dim Shared As Single m_yEnd()
Dim Shared As Single mStart,mSng,mStep,mDelta
Dim Shared As Integer mX1,mX2
Dim Shared As UInteger mPalUB_, Tmp__, mAlpha
#Macro zPieceVal(Valu)
Valu = alphaLo + Rnd * (alphaHi-alphaLo)
#EndMacro
#Macro zComponent_Start_PrvEnd(I_,mX1_)
zPieceVal(mSng)
If mShatter(mX1_) Then
zPieceVal(m_yEnd(mX1_))
Else
m_yEnd(mX1_) = mSng
EndIf
m_yStart(I_) = mSng
mX1_ = I_
#EndMacro
Sub zDefComp_retval(ByRef retVal As Single)
retVal = mSng
mX1 += 1
mSng += mStep
End Sub
Sub Pal_AnimProperties(pComp As ComponentAnimProps,incAngle1 As Single = 0.003,incAngle2 As Single = 0.007, pTile As Single=2.1*(0.1 + Rnd),rotRadius As Single = 0.36,clipHi As Single = 0,clipLo As Single = 0)
pComp.tile.a0 = Rnd * 6.28
pComp.tile.a1 = Rnd * 6.28
pComp.tile.iA0 = incAngle1
pComp.tile.iA1 = incAngle2
pComp.tile.rotRadi = rotRadius * pTile
pComp.clipHi = clipHi
pComp.clipLo = clipLo
' pComp.alphaHi = alphaHi
' pComp.alphaLo = alphaLo
' pComp.infoRibbon = pRibbon
End Sub
Sub DefComponent(pComp As ComponentAnimProps,pRibbon As GradientRibbon, retGradient() As Single,alphaHi As Single = 1, alphaLo As Single = 0,incAngle1 As Single = 0.003,incAngle2 As Single = 0.007, pTile As Single=2.1*(0.1 + Rnd),rotRadius As Single = 0.36,ClipHi As Single = 0,ClipLo As Single = 0)
mX1 = 1
For I As Integer = 2 To pRibbon.Sections
zComponent_Start_PrvEnd(I,mX1)
Next
zComponent_Start_PrvEnd(1,mX1)
mX1 = 0
If UBound(retGradient) <> pRibbon.UBPix Then ReDim retGradient(pRibbon.UBPix)
For J As Integer = 1 To pRibbon.Sections
mSng = m_yStart(J)
mStep = ( m_yEnd(J) - m_yStart(J) ) / mWidth(J)
zDefComp_retval retGradient(mX1)',clipHi,clipLo
For K As Integer = mX1 To mX1 + ( mWidth(J) - 2 )
zDefComp_retval retGradient(K)',clipHi,clipLo
Next
Next
retGradient(pRibbon.UBPix)=retGradient(0)
Pal_AnimProperties pComp,incAngle1,incAngle2,pTile,rotRadius,clipHi,clipLo
pComp.alphaHi = alphaHi
pComp.alphaLo = alphaLo
pComp.infoRibbon = pRibbon
End Sub
Sub Gradient(retINFO As GradientRibbon,WidBase As UShort = 2,WidVari As UShort = 50,Sections As UByte=5+Rnd*15,Shatter As UByte=0)
retInfo.Sections = Sections
ReDim m_yStart(Sections)
ReDim m_yEnd(Sections)
ReDim mShatter(Sections)
ReDim mWidth(Sections)
retINFO.UBPix = 0
For I As Integer = 1 To Sections
mWidth(I) = widBase + Rnd * widVari
mShatter(I) = Rnd * 255 < Shatter
retINFO.UBPix += mWidth(I)
Next
retINFO.Shatter = Shatter
End Sub
#Macro zGradientModulus(pOutput,pInput,pInfo)
pOutput = pInput - pInfo.UBPix * Int(pInput / pInfo.UBPix)
#EndMacro
Function GradientVal(pInput As Single, pInfo As GradientRibbon,pGradient() As Single) As Single
zGradientModulus(pInput,pInput,pInfo)
Dim As Single SngLo = Int(pInput)
Dim As Single SngHi = SngLo + 1
Function = pGradient(SngLo) + (pInput - SngLo) * (pGradient(SngHi) - pGradient(SngLo))
End Function
' ------------------------------------------------------
' ================================
' hsv palettizer
' ================================
' ------------------------------------------------------
Type RGBQUAD2 ''RGBQUAD defined in windows.bi
Blue As UByte
Green As UByte
Red As UByte
Alpha As UByte
End Type
Type HSVTYPE
Val8 As UByte
Sat8 As UByte
Hue16 As Short ''0 to 1529 is a complete cycle. 1530 is a repeat of 0 [red]
End Type
Dim Shared As Integer mHueBase, mValue, mSatu, mValue_, mHue
#Macro zHSV2RGB(pCompA,pCompB,pMeas_)
If mHue < pMeas_ Then
pCompA = mValue
pCompB = mValue - mSatu * (pMeas_ - mHue) / 255&
Else
pCompA = mValue - mSatu * (mHue - pMeas_) / 255&
pCompB = mValue
End If
#EndMacro
Sub HSVTYPE_To_RGBQUAD(pDest As RGBQUAD2, pSrc As HSVTYPE)
mHue = pSrc.Hue16 - 1530& * Int(pSrc.Hue16 / 1530&)
mValue = pSrc.Val8
mSatu = mValue * CInt(pSrc.Sat8) / 255&
If mHue < 511& Then
pDest.Blue = mValue - mSatu
zHSV2RGB(pDest.Red, pDest.Green, 255&)
ElseIf mHue < 1021& Then
pDest.Red = mValue - mSatu
zHSV2RGB(pDest.Green, pDest.Blue, 765&)
Else
pDest.Green = mValue - mSatu
zHSV2RGB(pDest.Blue, pDest.Red, 1275&)
End If
End Sub
Private Sub zPaletteStream_animDefaults(pAnimHSV As ComponentAnimProps, pCase As Integer)
Select Case pCase
Case 1
Pal_AnimProperties pAnimHSV,0.0042,0.00377
Case 2
Pal_AnimProperties pAnimHSV,0.00431,0.00317
Case 3
Pal_AnimProperties pAnimHSV,0.0037,0.006
Case 4
Pal_AnimProperties pAnimHSV,0.0041,0.005
Case 5
Pal_AnimProperties pAnimHSV,0.0031,0.0029
End Select
End Sub
Private Sub zPaletteStream_gradientizerDefaults(pInfo As GradientRibbon, pGradient() As Single, pCase As Integer)
Select Case pCase
Case 1
' DefGradient pInfo,pGradient(),1530,,,,1,200,1 + Rnd*3,50
Case 2
' DefGradient pInfo,pGradient(),1530,,,,1,200,2 + Rnd*3,63
Case 3
' DefGradient pInfo,pGradient(),305,-50,255,,,,,31
Case 4
' DefGradient pInfo,pGradient(),305,-10,255,,,,,63
Case 5
' DefGradient pInfo,pGradient(),158,-150,128,,1,150,10+Rnd*25,31
End Select
End Sub
Private sub zPaletteStream_EndPoints(pAnimHSV As ComponentAnimProps,pGradient() As Single, pSng As Single,DoIncrement As Integer,DefaultCase As Integer)
''Initializations if user hasn't requested any
' If pAnimHSV.infoRibbon.UBPix = 0 Then
' zPaletteStream_gradientizerDefaults pAnimHSV.infoRibbon,pGradient(),DefaultCase
' EndIf
' If pAnimHSV.tile = 0 Then
' zPaletteStream_animDefaults pAnimHSV,DefaultCase
' EndIf
pAnimHSV.tile.x1 = (pAnimHSV.tile.rotRadi * Sin(pAnimHSV.tile.A0)) * pAnimHSV.infoRibbon.UBPix
pAnimHSV.tile.x2 = (pAnimHSV.tile.tile + pAnimHSV.tile.rotRadi * Cos(pAnimHSV.tile.A1)) * pAnimHSV.infoRibbon.UBPix
If DoIncrement Then
pAnimHSV.tile.A0 += pAnimHSV.tile.iA0
pAnimHSV.tile.A1 += pAnimHSV.tile.iA1
End If
pAnimHSV.tile.xStep = (pAnimHSV.tile.x2 - pAnimHSV.tile.x1) * pSng
If pAnimHSV.clipLo = pAnimHSV.clipHi Then
pAnimHSV.clipHi = pAnimHSV.alphaHi
pAnimHSV.clipLo = pAnimHSV.alphaLo
EndIf
End Sub
#Macro zPalStream_HHSV(pS1,pS2,pS3,pS4,sS1,sS2,sS3,sS4,Def1,Def2,Def3,Def4)
if UBound(pPalette) < 2 then redim pPalette(255)
mPalUB_ = UBound(pPalette)
Dim As Single sng1,sng2
Dim As HSVTYPE HSV_
sng1 = 1 / mPalUB_
zPaletteStream_EndPoints pS1,sS1(),sng1,DoIncrement,Def1
zPaletteStream_EndPoints pS2,sS2(),sng1,DoIncrement,Def2
zPaletteStream_EndPoints pS3,sS3(),sng1,DoIncrement,Def3
zPaletteStream_EndPoints pS4,sS4(),sng1,DoIncrement,Def4
#EndMacro
#Macro zClip(retVal,clipLo,clipHi)
If retVal > clipHi Then
retVal = clipHi
ElseIf RetVal < clipLo Then
retVal = clipLo
End if
#EndMacro
#Macro zPaletteStream_Component(pSng,pComp,pG)
pSng = GradientVal(pComp.tile.x1,pComp.InfoRibbon,pG())
zClip(pSng,pComp.clipLo,pComp.clipHi)
pComp.tile.x1 += pComp.tile.xStep
#EndMacro
Sub PalStream_RGBAA(pPalette() As UInteger, pR_ As ComponentAnimProps,pG_ As ComponentAnimProps,pB_ As ComponentAnimProps,pA1 As ComponentAnimProps,pA2 As ComponentAnimProps,sR_() As Single,sG_() As Single,sB_() As Single,sA1() As Single,sA2() As Single,DoIncrement As Integer = -1)
zPalStream_HHSV(pR_,pG_,pB_,pA1,sR_,sG_,sB_,sA1,1,2,3,5)
zPaletteStream_EndPoints pA2,sA2(),sng1,DoIncrement,5
Dim As Single sng3,sng4,sng5
For I As UInteger Ptr = @pPalette(0) To @pPalette(mPalUB_)
zPaletteStream_Component(sng1,pR_,sR_)
zPaletteStream_Component(sng2,pG_,sG_)
zPaletteStream_Component(sng3,pB_,sB_)
zPaletteStream_Component(sng4,pA1,sA1)
zPaletteStream_Component(sng5,pA2,sA2)
*I = RGBA(sng1,sng2,sng3,sng4+sng5)
Next
End Sub
Sub PalStream_HHSV(pPalette() As UInteger, pH1 As ComponentAnimProps,pH2 As ComponentAnimProps,pS_ As ComponentAnimProps,pV_ As ComponentAnimProps,sH1() As Single,sH2() As Single,sS_() As Single,sV_() As Single,DoIncrement As Integer = -1)
zPalStream_HHSV(pH1,pH2,pS_,pV_,sH1,sH2,sS_,sV_,1,2,3,4)
For I As RGBQUAD2 Ptr = @pPalette(0) To @pPalette(mPalUB_)
'' Two hue streams combined look way gooder
zPaletteStream_Component(sng1,pH1,sH1)
zPaletteStream_Component(sng2,pH2,sH2)
HSV_.Hue16 = sng1+sng2
zPaletteStream_Component(HSV_.Sat8,pS_,sS_)
zPaletteStream_Component(HSV_.Val8,pV_,sV_)
HSVTYPE_To_RGBQUAD *I, HSV_
Next
End Sub
Sub PalStream_HHSVA(pPalette() As UInteger, pH1 As ComponentAnimProps,pH2 As ComponentAnimProps,pS_ As ComponentAnimProps,pV_ As ComponentAnimProps,pA_ As ComponentAnimProps,sH1() As Single,sH2() As Single,sS_() As Single,sV_() As Single,sA_() As Single,DoIncrement As Integer = -1)
zPalStream_HHSV(pH1,pH2,pS_,pV_,sH1,sH2,sS_,sV_,1,2,3,4)
zPaletteStream_EndPoints pA_,sA_(),sng1,DoIncrement,5
For I As RGBQUAD2 Ptr = @pPalette(0) To @pPalette(mPalUB_)
zPaletteStream_Component(sng1,pH1,sH1)
zPaletteStream_Component(sng2,pH2,sH2)
HSV_.Hue16 = sng1+sng2
zPaletteStream_Component(HSV_.Sat8, pS_,sS_)
zPaletteStream_Component(HSV_.Val8, pV_,sV_)
HSVTYPE_To_RGBQUAD *I, HSV_
zPaletteStream_Component(I->Alpha, pA_,sA_)
Next
End Sub
' ================================
' gradient value after calculation
' ================================
#Macro Modulus(pValue,pModulus)
If pValue >= pModulus Then
pValue -= pmodulus * Int(pValue / pModulus)
End If
#EndMacro
Sub AlphaColor(ByRef RetPixel As UInteger, pPalette() As UInteger, pMu As Single)
Modulus(pMu,mPalUB_)
Dim As UInteger base = Int(pMu)
Tmp__ = RetPixel ''Tmp__ is a shared var
''This first section interpolates a color
''between 2 adjacent values in the palette
Dim As UInteger lAlph = 256 * (pMu - base)
RetPixel = pPalette(base + 1)
Dim As UInteger MaskRB = (RetPixel And &H00FF00FF) * lAlph
Dim As UInteger MaskAG = ((RetPixel And &HFF00FF00) Shr 8) * lAlph
lAlph = 256 - lAlph
RetPixel = pPalette(base)
MaskRB += (RetPixel And &H00FF00FF) * lAlph
MaskAG += ((RetPixel And &HFF00FF00) Shr 8) * lAlph
''This section calculates the transparency,
lAlph = MaskAG Shr 24
MaskRB And= &HFF00FF00
MaskAG And= &H0000FF00
MaskRB Shr= 8
MaskRB *= lAlph
MaskAG *= lAlph
lAlph = 255 - lAlph
RetPixel = Tmp__
MaskRB += (RetPixel And &H00FF00FF) * lAlph
MaskAG += (RetPixel And &H0000FF00) * lAlph
' MaskRB += &H800080
' MaskAG += &H8000
MaskRB Shr= 8
MaskAG Shr= 8
RetPixel = (MaskRB And &HFF00FF) Or (MaskAG And &HFF00)
End Sub
Sub SmoothColor(ByRef dest As UInteger, pPalette() As UInteger, pMu As Single)
Dim As UInteger MaskRB,MaskAG,Pixel_,Alpha_
Modulus(pMu,mPalUB_)
dest = Int(pMu)
Alpha_ = 256 * (pMu - dest)
Pixel_ = pPalette(dest + 1)
MaskRB = (Pixel_ And &H00FF00FF) * Alpha_
MaskAG = ((Pixel_ And &HFF00FF00) Shr 8) * Alpha_
Alpha_ = 256 - Alpha_
Pixel_ = pPalette(dest)
MaskRB += (Pixel_ And &H00FF00FF) * Alpha_
MaskAG += ((Pixel_ And &HFF00FF00) Shr 8) * Alpha_
MaskRB And= &HFF00FF00
MaskAG And= &HFF00FF00
dest = (MaskRB Shr 8) Or MaskAG
End Sub
Sub AlphaColor2(ByRef dest As UInteger, pPalette() As UInteger, pMu As Single)
'' Step 1 of 2
''interpolate ARGB betweeen 2 adjacent palette entries
Modulus(pMu,mPalUB_)
Dim As UInteger BG_ = Int(pMu)
Dim As UInteger lAlph = 256 * (pMu - BG_)
Tmp__ = pPalette(BG_ + 1) ''Tmp__ will be foreground
BG_ = pPalette(BG_)
Dim As UInteger OFOF = (BG_ And &HFF00FF00) Shr 8
Dim As UInteger FOFO = (Tmp__ And &HFF00FF00) Shr 8
FOFO -= OFOF
FOFO *= lAlph
' FOFO += &H800080 '' "0.5" -> Int(sng + 0.5)
FOFO += OFOF Shl 8
FOFO And= &HFF00FF00
BG_ And= &H00FF00FF
OFOF = Tmp__ And &H00FF00FF
OFOF -= BG_ '' RB channels
OFOF *= lAlph
' OFOF += &H800080 '' "0.5" -> Int(sng + 0.5)
OFOF Shr= 8
OFOF += BG_
OFOF And= &H00FF00FF
'' Step 2 of 2
''blend using alpha onto dest
'' G channel
lAlph = FOFO Shr 24
BG_ = dest And &H0000FF00
Dim As UInteger OOFO = ((FOFO And &H0000FF00) - BG_) * lAlph '+ &H8000
OOFO Shr= 8
OOFO += BG_
OOFO And= &H0000FF00
'' R B channels
dest And= &H00FF00FF
OFOF -= dest
OFOF *= lAlph
' OFOF += &H800080
OFOF Shr= 8
OFOF += dest
OFOF And= &H00FF00FF
dest = OOFO Or OFOF
End Sub
Sub SmoothColor2(ByRef dest As UInteger, pPalette() As UInteger, pMu As Single)
Dim As UInteger BG_,FOF
Modulus(pMu,mPalUB_)
BG_ = Int(pMu)
mAlpha = 256 * (pMu - BG_)
Tmp__ = pPalette(BG_ + 1) ''Tmp__ will be foreground
BG_ = pPalette(BG_)
FOF = (BG_ And &HFF00FF00) Shr 8
dest = (Tmp__ And &HFF00FF00) Shr 8
dest -= FOF '' AG channels
dest *= mAlpha
' dest += &H800080
dest += FOF Shl 8
dest And= &HFF00FF00
BG_ And= &H00FF00FF
FOF = Tmp__ And &H00FF00FF
FOF -= BG_ '' RB channels
FOF *= mAlpha
' FOF += &H800080 '' "0.5" -> Int(sng + 0.5)
FOF Shr= 8
FOF += BG_
dest Or= FOF And &H00FF00FF
End Sub
Sub Alpha257(ByRef dest As UInteger,foreground As UInteger,alph As UInteger)
Dim As UInteger temp_ = (dest And &HFF00FF00) Shr 8
Dim As UInteger rb_ = dest And &H00FF00FF
''This sub interprets alpha from 0 to 256
''http://stereopsis.com/doubleblend.html
''http://www.virtualdub.org/blog/pivot/entry.php?id=117
dest = (foreground And &HFF00FF00) Shr 8
dest -= temp_ '' AG channels
dest *= alph
dest += &H800080 '' "0.5" -> Int(sng + 0.5)
dest += temp_ Shl 8
dest And= &HFF00FF00
temp_ = foreground And &H00FF00FF
temp_ -= rb_ '' RB channels
temp_ *= alph
temp_ += &H800080 '' "0.5" -> Int(sng + 0.5)
temp_ Shr= 8
temp_ += rb_
dest Or= temp_ And &H00FF00FF
End Sub
'' =================================================
'' Run-time sample 2 (copy to a new file)
'' Tip: Replace all [apostrophe][space] with [empty]
'' =================================================
Code: Select all
#Include "cairo/cairo.bi"
#Include once "fbgfx.bi"
#If __FB_LANG__ = "fb"
Using fb
#EndIf
Dim e As EVENT
Type ImageInfo
As Any ptr img,pixels
As cairo_surface_t Ptr cs
As Integer pitch
End Type
Dim Shared As Integer SCR_W = 480
Dim Shared As Integer SCR_H = 360
Dim Shared As Integer WidM: WidM = SCR_W - 1
Dim Shared As integer HgtM: HgtM = SCR_H - 1
Dim Shared As Single sngMidx: sngMidx = WidM/2
Dim Shared As Single sngMidy: sngMidy = HgtM/2
Dim Shared As Single SCR_DIAGONAL: SCR_DIAGONAL = Sqr(sngMidx^2+sngMidy^2)
#Include "palettizer.bi"
Dim Shared As Single gradient_R()
Dim Shared As Single gradient_G()
Dim Shared As Single gradient_B()
Dim Shared As Single gradient_A1()
Dim Shared As Single gradient_A2()
Dim Shared As GradientRibbon mGInfo
Dim Shared As ComponentAnimProps PS_R,PS_G,PS_B,PS_A1,PS_A2
Dim Shared As Single SuperSample = 2
Dim Shared As Integer PaletteSize: PaletteSize = SuperSample*Sqr(sngMidx^2+sngMidy^2)
Dim Shared As UInteger mPalette(PaletteSize)
Dim Shared As Integer X,Y
Dim Shared As Single m_Sng
Dim Shared As Single sngMap(WidM,HgtM)
Dim Shared As cairo_t Ptr cr
Dim Shared As ImageInfo img_B,img_F,img_Wnd
Sub Cre8(pInfo As ImageInfo)
pInfo.img = ImageCreate( SCR_W, SCR_H, RGB(255,255,255))
ImageInfo pInfo.img, ,,, pInfo.pitch, pInfo.pixels
pInfo.cs = cairo_image_surface_create_for_data(pInfo.pixels, CAIRO_FORMAT_ARGB32, SCR_W, SCR_H, SCR_W*4)
End Sub
Sub Destr0y(pInfo As ImageInfo)
cairo_surface_destroy pInfo.cs
ImageDestroy pInfo.img
End Sub
Sub Checkerboard(pInfo As ImageInfo,CheckerSize As UInteger = 16,pWidM As Integer=WidM,pHgtM As Integer=HgtM,pCheckGray As Byte = 64)
Dim As UInteger SizeDouble=CheckerSize*2,SizeM=CheckerSize-1
For Y = 0 To pHgtM Step CheckerSize
For X = -CheckerSize * ((Y/SizeDouble)=Int(Y/SizeDouble)) To pWidM Step SizeDouble
Line pInfo.img,(X,Y)-(X+SizeM,Y+SizeM),RGB(pCheckGray,pCheckGray,pCheckGray),BF
Next
Next
End Sub
Sub Blit(cr As cairo_t Ptr, pInfo As ImageInfo)
cairo_set_source_surface (cr, pInfo.cs, 0, 0)
cairo_paint (cr)
End Sub
Private Sub NewPal(yBase As Integer=0,yHeight As Integer=19)
Gradient mGInfo,2,20,25+Rnd*25,0
DefComponent PS_R,mGInfo,gradient_R(),305,-50,0.003,0.0041,,,255
Gradient mGInfo,2,20,25+Rnd*25,0
DefComponent PS_G,mGInfo,gradient_G(),305,-50,0.004,0.0051,,,255
Gradient mGInfo,2,20,25+Rnd*25,0
DefComponent PS_B,mGInfo,gradient_B(),305,-50,0.005,0.0061,,,255
Gradient mGInfo,3,30,25+Rnd*55,31
DefComponent PS_A1,mGInfo,gradient_A1(),415,-300,0.0071,0.0079,,,127
Gradient mGInfo,3,30,25+Rnd*55,31
DefComponent PS_A2,mGInfo,gradient_A2(),415,-300,0.0057,0.0053,,,128
End Sub
Sub Render(dest As UInteger Ptr)
Dim As UInteger MaskRB,MaskAG,Pixel_,Alpha_
For y = 0 To HgtM
For x = 0 To WidM
''Interpolated color based on 2 adjacent palette entries
m_Sng = sngMap(x,y)
Modulus(m_Sng,mPalUB_)
*dest = Int(m_Sng)
Alpha_ = 256 * (m_Sng - *dest)
Pixel_ = mPalette(*dest + 1)
MaskRB = (Pixel_ And &H00FF00FF) * Alpha_
MaskAG = ((Pixel_ And &HFF00FF00) Shr 8) * Alpha_
Alpha_ = 256 - Alpha_
Pixel_ = mPalette(*dest)
MaskRB += (Pixel_ And &H00FF00FF) * Alpha_
MaskAG += ((Pixel_ And &HFF00FF00) Shr 8) * Alpha_
MaskRB And= &HFF00FF00
MaskAG And= &HFF00FF00
*dest = (MaskRB Shr 8) Or MaskAG
dest += 1
Next
Next
End Sub
' ------------------------------------------------------
' ================================
'
' ================================
' ------------------------------------------------------
Dim As Single xDelt,yDsq,xyDist
For Y = 0 To HgtM
yDsq = (y-sngMidy)^2
For x = 0 To WidM
xDelt = x - sngMidx
sngMap(x,y) = SuperSample*Sqr(yDsq + xDelt * xDelt)
Next
Next
ScreenRes SCR_W, SCR_H, 32
Cre8 img_F
Cre8 img_B
Checkerboard img_B,,,,0
img_Wnd.cs = cairo_image_surface_create_for_data(ScreenPtr, CAIRO_FORMAT_ARGB32, SCR_W, SCR_H, SCR_W*4)
Randomize
NewPal
Do
PalStream_RGBAA mPalette(),PS_R,PS_G,PS_B,PS_A1,PS_A2, _
gradient_R(),gradient_G(),gradient_B(),gradient_A1(),gradient_A2()
cr = cairo_create(img_Wnd.cs)
ScreenLock
Blit cr,img_B
Render img_F.pixels
Blit cr,img_F
ScreenUnLock
cairo_destroy cr
If (ScreenEvent(@e)) Then
if e.type = EVENT_KEY_PRESS Then
Select Case e.scancode
Case SC_ESCAPE
Exit Do
Case Else
NewPal
End Select
End If
End If
Sleep 30,1
Loop
cairo_surface_destroy img_Wnd.cs
Destr0y img_B
Destr0y img_F
Sleep
Code: Select all
'' include needed libs
#include once "fbgfx.bi"
'#include once "/gl/gl.bi"
'#include once "/gl/glu.bi"
'#include once "fbgl2d.bi"
'#include once "fbgl2d.bas"
#If __FB_LANG__ = "fb"
Using fb
#EndIf
Dim e As EVENT
Dim As Integer SCR_W = 480
Dim As Integer SCR_H = 360
Dim Shared As Integer WidM :WidM = SCR_W - 1
Dim Shared As integer HgtM :HgtM = SCR_H - 1
Dim As Single sngMidx = WidM/2
Dim As Single sngMidy = HgtM/2
#Include "palettizer.bi"
Dim Shared As Single gradient_R()
Dim Shared As Single gradient_G()
Dim Shared As Single gradient_B()
Dim Shared As Single gradient_A1()
Dim Shared As Single gradient_A2()
Dim Shared As GradientRibbon mGInfo
Dim Shared As ComponentAnimProps PS_R,PS_G,PS_B,PS_A1,PS_A2
Dim Shared As Single SuperSample = 2
Dim Shared As Integer PaletteSize: PaletteSize = SuperSample*Sqr(sngMidx^2+sngMidy^2)
Dim Shared As UInteger mPalette(PaletteSize)
Dim Shared As Integer X,Y
Dim Shared As Single m_Sng
ScreenRes SCR_W,SCR_H,32
'gl2d.screen_init( SCR_W, SCR_H )
'gl2d.vsync_on()
Dim As UInteger Ptr mPTR
Dim Shared As Integer pitch_FG
Dim Shared As Any Ptr pixels_FG
Dim Shared image_BG As Any Ptr: image_BG = ImageCreate( SCR_W, SCR_H, RGB(255, 255, 255) )
Dim Shared image_FG As Any Ptr: image_FG = ImageCreate( SCR_W, SCR_H )
ImageInfo image_FG, ,,, pitch_FG, pixels_FG
Sub Checkerboard(CheckerSize As UInteger = 16,pImg_ As Any Ptr, pWidM As Integer,pHgtM As Integer,pCheckGray As Byte = 64)
Dim As UInteger SizeDouble=CheckerSize*2,SizeM=CheckerSize-1
For Y = 0 To pHgtM Step CheckerSize
For X = -CheckerSize * ((Y/SizeDouble)=Int(Y/SizeDouble)) To pWidM Step SizeDouble
Line pImg_,(X,Y)-(X+SizeM,Y+SizeM),RGB(pCheckGray,pCheckGray,pCheckGray),BF
Next
Next
End Sub
Private Sub NewPal(yBase As Integer=0,yHeight As Integer=19)
Gradient mGInfo,2,20,25+Rnd*25,0
DefComponent PS_R,mGInfo,gradient_R(),305,-50,0.003,0.0041,,,255
Gradient mGInfo,2,20,25+Rnd*25,0
DefComponent PS_G,mGInfo,gradient_G(),305,-50,0.004,0.0051,,,255
Gradient mGInfo,2,20,25+Rnd*25,0
DefComponent PS_B,mGInfo,gradient_B(),305,-50,0.005,0.0061,,,255
Gradient mGInfo,3,30,25+Rnd*55,31
DefComponent PS_A1,mGInfo,gradient_A1(),415,-300,0.0071,0.0079,,,127
Gradient mGInfo,3,30,25+Rnd*55,31
DefComponent PS_A2,mGInfo,gradient_A2(),415,-300,0.0057,0.0053,,,128
End Sub
Checkerboard,image_BG,WidM,HgtM,0
'gl2d.load_image_to_HW(Cast(gl2d.image Ptr, image_BG))
Randomize
NewPal
Dim As Single xDelt,yDsq,xyDist
Dim Shared As Single sngMap(WidM,HgtM)
For Y = 0 To HgtM
yDsq = (y-sngMidy)^2
For x = 0 To WidM
xDelt = x - sngMidx
sngMap(x,y) = SuperSample*Sqr(yDsq + xDelt * xDelt)
Next
Next
Sub Render
Dim As UInteger MaskRB,MaskAG,Pixel_,Alpha_
Dim dest As UInteger Ptr = pixels_FG
For y = 0 To HgtM
For x = 0 To WidM
''Interpolated color based on 2 adjacent palette entries
m_Sng = sngMap(x,y)
Modulus(m_Sng,mPalUB_)
*dest = Int(m_Sng)
Alpha_ = 256 * (m_Sng - *dest)
Pixel_ = mPalette(*dest + 1)
MaskRB = (Pixel_ And &H00FF00FF) * Alpha_
MaskAG = ((Pixel_ And &HFF00FF00) Shr 8) * Alpha_
Alpha_ = 256 - Alpha_
Pixel_ = mPalette(*dest)
MaskRB += (Pixel_ And &H00FF00FF) * Alpha_
MaskAG += ((Pixel_ And &HFF00FF00) Shr 8) * Alpha_
MaskRB And= &HFF00FF00
MaskAG And= &HFF00FF00
*dest = (MaskRB Shr 8) Or MaskAG
dest += 1
Next
Next
End Sub
Do
PalStream_RGBAA mPalette(),PS_R,PS_G,PS_B,PS_A1,PS_A2, _
gradient_R(),gradient_G(),gradient_B(),gradient_A1(),gradient_A2()
ScreenLock
Put (0,0),image_BG,PSET
Render
Put (0,0),image_FG,Alpha
ScreenUnLock
If (ScreenEvent(@e)) Then
if e.type = EVENT_KEY_PRESS Then
Select Case e.scancode
Case SC_ESCAPE
' gl2d.destroy_image image_BG
ImageDestroy image_BG
ImageDestroy image_FG
' GL2D.destroy()
End
Case Else
NewPal
End Select
End If
End If
' Flip
Sleep 30,1
Loop
' mPTR = ScreenPtr
' ScreenLock
' Put (0,0),image_BG,PSET
' For y = 0 To HgtM
' For x = 0 To WidM
'' SmoothColor *mPTR,mPalette(),sngMap(x,y)
' AlphaColor *mPTR, mPalette(),sngMap(x,y)
' mPTR += 1
' Next
' Next
' screenunlock
'Back val 0
'Fore val 1
'Alpha [0 31 63 95 127 159 191 223 255] Result [0 0 0 0 0 0 0 0 1]
The most platform friendly version would be to not use proprietary libs. On win32 use either Direct3D or OpenGL. Both require a little bit of setup but unless you are making the next version in the Fallout series you don't need the extra bloated crap they add in all the wrapper libs (cairo, irrlitchit, etc). The only advantage to those libraries is that they generally they have written most of the container structures and graphics interface code for you. But, why waste (and require) space for 25MB of bloat for your tetris clone?TJF wrote:The most CPU friendly blending will be hardware accellerated by the GPU. Have you considered using CAIRO?
I'd say learn the basics of OpenGL as it will be infinitely more useful then learning a specific implementation of an OpenGL wrapper. I'm not saying don't use wrappers just don't get sucked into quick'n'easy unless you want to write $%#@ facebook games.
Cairo isn't a proprietary lib. It's open source like OpenGl, but much smaller (2D only -- well matching for a tetris clone with advanced grafic capabilities). And unlike Direct3D it's cross-platform. And there's no wrapper. Cairo is written in C and the library (DLL on win32) will be directly included from the FB source (like OpneGl).1000101 wrote:The most platform friendly version would be to not use proprietary libs. On win32 use either Direct3D or OpenGL. Both require a little bit of setup but unless you are making the next version in the Fallout series you don't need the extra bloated crap they add in all the wrapper libs (cairo, irrlitchit, etc). The only advantage to those libraries is that they generally they have written most of the container structures and graphics interface code for you. But, why waste (and require) space for 25MB of bloat for your tetris clone?
When compiling (on Ubuntu 11.04, fbc 0.22) I get lots of warnings:dafhi wrote:@ TJF - could you check this out ...
But it runs with a window like (the circles are changing color and moving)$ fbc -w pedantic -exx cairo_test.bas
palettizer.bi(53) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pComp) of Pal_AnimProperties()
palettizer.bi(53) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (incAngle1) of Pal_AnimProperties()
palettizer.bi(53) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (incAngle2) of Pal_AnimProperties()
palettizer.bi(53) warning 16(0): No explicit BYREF or BYVAL, at parameter 4 (pTile) of Pal_AnimProperties()
palettizer.bi(53) warning 16(0): No explicit BYREF or BYVAL, at parameter 5 (rotRadius) of Pal_AnimProperties()
palettizer.bi(53) warning 16(0): No explicit BYREF or BYVAL, at parameter 6 (clipHi) of Pal_AnimProperties()
palettizer.bi(53) warning 16(0): No explicit BYREF or BYVAL, at parameter 7 (clipLo) of Pal_AnimProperties()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pComp) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (pRibbon) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 4 (alphaHi) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 5 (alphaLo) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 6 (incAngle1) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 7 (incAngle2) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 8 (pTile) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 9 (rotRadius) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 10 (ClipHi) of DefComponent()
palettizer.bi(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 11 (ClipLo) of DefComponent()
palettizer.bi(87) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (retINFO) of Gradient()
palettizer.bi(87) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (WidBase) of Gradient()
palettizer.bi(87) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (WidVari) of Gradient()
palettizer.bi(87) warning 16(0): No explicit BYREF or BYVAL, at parameter 4 (Sections) of Gradient()
palettizer.bi(87) warning 16(0): No explicit BYREF or BYVAL, at parameter 5 (Shatter) of Gradient()
palettizer.bi(105) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pInput) of GradientVal()
palettizer.bi(105) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (pInfo) of GradientVal()
palettizer.bi(142) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pDest) of HSVTYPE_To_RGBQUAD()
palettizer.bi(142) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (pSrc) of HSVTYPE_To_RGBQUAD()
palettizer.bi(158) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pAnimHSV) of zPaletteStream_animDefaults()
palettizer.bi(158) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (pCase) of zPaletteStream_animDefaults()
palettizer.bi(172) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pInfo) of zPaletteStream_gradientizerDefaults()
palettizer.bi(172) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pCase) of zPaletteStream_gradientizerDefaults()
palettizer.bi(186) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pAnimHSV) of zPaletteStream_EndPoints()
palettizer.bi(186) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pSng) of zPaletteStream_EndPoints()
palettizer.bi(186) warning 16(0): No explicit BYREF or BYVAL, at parameter 4 (DoIncrement) of zPaletteStream_EndPoints()
palettizer.bi(186) warning 16(0): No explicit BYREF or BYVAL, at parameter 5 (DefaultCase) of zPaletteStream_EndPoints()
palettizer.bi(236) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (pR_) of PalStream_RGBAA()
palettizer.bi(236) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pG_) of PalStream_RGBAA()
palettizer.bi(236) warning 16(0): No explicit BYREF or BYVAL, at parameter 4 (pB_) of PalStream_RGBAA()
palettizer.bi(236) warning 16(0): No explicit BYREF or BYVAL, at parameter 5 (pA1) of PalStream_RGBAA()
palettizer.bi(236) warning 16(0): No explicit BYREF or BYVAL, at parameter 6 (pA2) of PalStream_RGBAA()
palettizer.bi(236) warning 16(0): No explicit BYREF or BYVAL, at parameter 12 (DoIncrement) of PalStream_RGBAA()
palettizer.bi(253) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (pH1) of PalStream_HHSV()
palettizer.bi(253) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pH2) of PalStream_HHSV()
palettizer.bi(253) warning 16(0): No explicit BYREF or BYVAL, at parameter 4 (pS_) of PalStream_HHSV()
palettizer.bi(253) warning 16(0): No explicit BYREF or BYVAL, at parameter 5 (pV_) of PalStream_HHSV()
palettizer.bi(253) warning 16(0): No explicit BYREF or BYVAL, at parameter 10 (DoIncrement) of PalStream_HHSV()
palettizer.bi(257) warning 4(1): Suspicious pointer assignment
palettizer.bi(257) warning 4(1): Suspicious pointer assignment
palettizer.bi(271) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (pH1) of PalStream_HHSVA()
palettizer.bi(271) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pH2) of PalStream_HHSVA()
palettizer.bi(271) warning 16(0): No explicit BYREF or BYVAL, at parameter 4 (pS_) of PalStream_HHSVA()
palettizer.bi(271) warning 16(0): No explicit BYREF or BYVAL, at parameter 5 (pV_) of PalStream_HHSVA()
palettizer.bi(271) warning 16(0): No explicit BYREF or BYVAL, at parameter 6 (pA_) of PalStream_HHSVA()
palettizer.bi(271) warning 16(0): No explicit BYREF or BYVAL, at parameter 12 (DoIncrement) of PalStream_HHSVA()
palettizer.bi(276) warning 4(1): Suspicious pointer assignment
palettizer.bi(276) warning 4(1): Suspicious pointer assignment
palettizer.bi(301) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pMu) of AlphaColor()
palettizer.bi(343) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pMu) of SmoothColor()
palettizer.bi(365) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pMu) of AlphaColor2()
palettizer.bi(415) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pMu) of SmoothColor2()
palettizer.bi(442) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (foreground) of Alpha257()
palettizer.bi(442) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (alph) of Alpha257()
cairo_test.bas(45) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pInfo) of Cre8()
cairo_test.bas(51) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pInfo) of Destr0y()
cairo_test.bas(56) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (pInfo) of Checkerboard()
cairo_test.bas(56) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (CheckerSize) of Checkerboard()
cairo_test.bas(56) warning 16(0): No explicit BYREF or BYVAL, at parameter 3 (pWidM) of Checkerboard()
cairo_test.bas(56) warning 16(0): No explicit BYREF or BYVAL, at parameter 4 (pHgtM) of Checkerboard()
cairo_test.bas(56) warning 16(0): No explicit BYREF or BYVAL, at parameter 5 (pCheckGray) of Checkerboard()
cairo_test.bas(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (cr) of Blit()
cairo_test.bas(65) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (pInfo) of Blit()
cairo_test.bas(70) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (yBase) of NewPal()
cairo_test.bas(70) warning 16(0): No explicit BYREF or BYVAL, at parameter 2 (yHeight) of NewPal()
cairo_test.bas(87) warning 16(0): No explicit BYREF or BYVAL, at parameter 1 (dest) of Render()
Sorry, I don't understand this.dafhi wrote:... and give pointers? (no pun intended)
the cairo version runs slower. I was wondering if you are familiar enough with Cairo to suggest speed improvements. Thanks for the compiler warnings list. They don't appear here, but I get 'suspicious pointer assignment' messages.
Yeah I'd like to learn opengl. Relsoft's EasyGL thread suggests "render to texture" is what I need
Yeah I'd like to learn opengl. Relsoft's EasyGL thread suggests "render to texture" is what I need