Great To see you at an IDE Dude!
Oops, confession time, I started on it and then it grew into the start of a new production.
Also have a butchers at the Freebasic tutorials by RDC.
'
' draw image alpha
' for drewpee.
'
option explicit
option static
const as integer SCREEN_WWIDTH=640
const as integer SCREEN_HEIGHT=480
const as integer IMAGE_WWIDTH=96
const as integer IMAGE_HEIGHT=96
#include once "tinyptc_ext++.bi"
dim shared as uinteger screen_buffer( SCREEN_WWIDTH * SCREEN_HEIGHT )
dim shared as uinteger image_buffer ( IMAGE_WWIDTH * IMAGE_HEIGHT )
declare sub draw_image_alpha( byval pos_x as integer, byval pos_y as integer, byval alpha_val as integer )
declare sub graphics ( byval title as string, byval wwidth as integer, byval height as integer, byval options as integer=1 )
declare sub init_demo ()
declare sub run_demo ()
declare function alpha_blend( byval col1 as uinteger, byval col2 as uinteger, byval alpha as integer ) as uinteger
init_demo()
run_demo()
sub clear_screen()
for a as integer=0 to (SCREEN_WWIDTH*SCREEN_HEIGHT)-1
screen_buffer(a)=0
next
end sub
sub draw_image_alpha( byval pos_x as integer, byval pos_y as integer, byval alpha_val as integer )
dim as integer alpha_col=256-alpha_val
dim as integer x,y
dim as uinteger col1, col2
for y=0 to IMAGE_HEIGHT-1
for x=0 to IMAGE_WWIDTH-1
if ((x+pos_x)>0) and ((x+pos_x)<SCREEN_WWIDTH-1) and ((y+pos_y)>0) and ((y+pos_y)<SCREEN_HEIGHT-1) then
col1=screen_buffer( (x+pos_x)+(y+pos_y)*SCREEN_WWIDTH )
col2= image_buffer( (x) +(y) * IMAGE_WWIDTH )
' method 1.
if col2<>0 then screen_buffer((x+pos_x)+(y+pos_y) * SCREEN_WWIDTH )= alpha_blend( col1, col2, alpha_val )
' method 2.
if col2<>0 then
screen_buffer( ( x+pos_x )+( y+pos_y ) * SCREEN_WWIDTH )= _
((((col1 and &hff00ff ) *alpha_val+( col2 and &hff00ff )*alpha_col ) and &hff00ff00 ) shr 8 ) or _
((((col1 and &hff00ff00) shr 8)*alpha_val+((col2 and &hff00ff00) shr 8 )*alpha_col ) and &hff00ff00 )
end if
end if
next
next
end sub
sub graphics( byval title as string, byval wwidth as integer, byval height as integer, byval options as integer=1 )
if options=0 then
ptc_setdialog( 1, "full screen mode?", 0, 0 )
else
ptc_setdialog( 0, "", 0, 1 )
end if
ptc_allowclose( 1 )
if ( ptc_open( title, wwidth, height )=0 ) then end -1
end sub
sub init_demo()
graphics( "dRaw IMaGe aLPhA", 640, 480 )
dim as integer x,y
for y=0 to image_height-1
for x=0 to image_wwidth-1
image_buffer( (x)+(y)*image_wwidth )=&hfff0f000' mellow yellow with alpha channel set to 255
next
next
end sub
sub run_demo()
while inkey<>chr(27)
clear_screen()
draw_image_alpha( 230, 145, 64 )
draw_image_alpha( 210, 120, 96 )
ptc_update ( @screen_buffer(0) )
wend
end sub
function alpha_blend( byval col1 as uinteger, byval col2 as uinteger, byval alpha as integer ) as uinteger
dim as integer inv_alpha
dim as integer r,g,b
dim as integer r1,r2
dim as integer g1,g2
dim as integer b1,b2
if alpha > 255 then alpha = 255
if alpha < 0 then alpha = 0
inv_alpha = 255 - alpha
r1=( col1 shr 16) and &hff
g1=( col1 shr 8) and &hff
b1=( col1 ) and &hff
r2=(col2 shr 16) and &hff
g2=(col2 shr 8 ) and &hff
b2=(col2 ) and &hff
r=(( r1 * alpha ) or ( r2 * inv_alpha)) shr 8
g=(( g1 * alpha ) or ( g2 * inv_alpha)) shr 8
b=(( b1 * alpha ) or ( b2 * inv_alpha)) shr 8
return (r shl 16)or(g shl 8)or(b)
end function