Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Alpha blending? RGBA()?
#1
I'd never even heard of it before I found FB. The manual told me that it's used to simulate transparency...but after that I didn't really understand the implemenatation.

Code:
SCREENRES 800,600,32
LINE (0,0)-(50,50),RGB(255,0,0),BF
LINE (25,25)-(75,75),RGBA(0,0,255,100),BF
Shouldn't that make a red box, and a translucent blue box on top of it? It doesn't for me.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#2
It doesn't work with the drawing primitives, only with PUT.

Here's the code you posted modified to do translucency:
Code:
option byval
declare sub drawTranslucentFilledBox (x1, y1, x2, y2, col as uinteger)

SCREENRES 800,600,32
LINE (0,0)-(50,50),RGB(255,0,0),BF

'LINE (25,25)-(75,75),RGBA(0,0,255,100),BF
drawTranslucentFilledBox 25, 25, 75, 75, RGBA(0,0,255,100)

sleep

end

' Really really slow lazy way to draw an alpha blended box:
sub drawTranslucentFilledBox (x1, y1, x2, y2, col as uinteger)
    
    w = x2 - x1 + 1
    h = y2 - y1 + 1
    
    ' ImageCreate will figure out how much memory to allocate:
    DIM sprite AS ANY PTR = ImageCreate(w, h)
    
    ' Fill sprite with col:
    LINE sprite, (0, 0)-(w - 1, h - 1), col, BF
    
    ' ALPHA only works with 32 bit sprite on 32 bit screen modes
    ' It takes the A in RGBA as how opaque that pixel should be (0=invisible, 255=solid)
    PUT (x1, y1), sprite, ALPHA
    
    ' Free the memory allocated for the sprite:
    ImageDestroy sprite
    
end sub
Reply
#3
Wicked, thanks Sterling.
You mentioned that it's slow and lazy...how could it be made faster?
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#4
Fill the color of the buffer when you create it and remove the line statement.

ImageCreate(...)
stylin:
Reply
#5
Quote:You mentioned that it's slow and lazy...how could it be made faster?
Well ideally you wouldn't allocate any memory at all just to draw a translucent box.

Here's the better way. This is FB code and PUT is MMX optimized asm, but this is still faster because this doesn't do any memory allocation:
Code:
option byval
declare sub drawTranslucentFilledBox (x1, y1, x2, y2, col as uinteger)

SCREENRES 800,600,32
LINE (0,0)-(50,50),RGB(255,0,0),BF

'LINE (25,25)-(75,75),RGBA(0,0,255,100),BF
drawTranslucentFilledBox 25, 25, 75, 75, RGBA(0,0,255,100)

sleep

end

' Somewhat less slow and lazy way to draw an alpha blended box:
sub drawTranslucentFilledBox (x1, y1, x2, y2, col as uinteger)
    
    const redblue_mask = &h00FF00FF
    const green_mask   = &h0000FF00
    
    ' Would be better if these values could be known (like with DIM SHARED
    ' variables or CONSTs) without needing to call these every time:
    screeninfo sw, sh ', , pitch
    dim framebuf as uinteger = screenptr
    
    pitch = sw * 4
    
    ' Clipping:
    if x1 < 0 then x1 = 0
    if y1 < 0 then y1 = 0
    if x2 >= sw then x2 = sw - 1
    if y2 >= sh then y2 = sh - 1
    
    w = x2 - x1 + 1
    h = y2 - y1 + 1
    
    alpha = (col shr 24) + 1
    src_redblue = col and redblue_mask
    src_green = col and green_mask
    
    ' Address in memory of the pixel at (x1, y):
    dim scanline as uinteger = framebuf + y1 * pitch + (x1 * 4)
    
    ' Address in memory of the pixel at (x, y):
    dim dest as uinteger
    
    dim as uinteger dest_redblue, dest_green, dest_col
    
    screenlock
    
    for y = 0 to h - 1
        dest = scanline
        for x = 0 to w - 1
            
            dest_col = peek(integer, dest)
            
            dest_redblue = dest_col and redblue_mask
            dest_green = dest_col and green_mask
            dest_col = (dest_redblue + (((src_redblue - dest_redblue) * alpha) shr 8)) and redblue_mask
            dest_col += (dest_green + (((src_green - dest_green) * alpha) shr 8)) and green_mask
            
            poke integer, dest, dest_col
            
            dest += 4
            
        next x
        scanline += pitch
    next y
    
    screenunlock y1, y2
    
end sub
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)