Author Topic: TinyPTC alpha blending?  (Read 5021 times)

0 Members and 1 Guest are viewing this topic.

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
TinyPTC alpha blending?
« on: June 16, 2010 »
Apologies first for not being around that much - real life always takes over!
I have a quick one and I don't think anybody has asked about this before?

Can you do Alpha blends using TinyPTC?

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: TinyPTC alpha blending?
« Reply #1 on: June 16, 2010 »
Is the Pope Catholic?
Let me get the brain in gear.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: TinyPTC alpha blending?
« Reply #2 on: June 16, 2010 »
Thanks Clyde - started doing some fantastic stuff using it but would love to do something more.

A
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: TinyPTC alpha blending?
« Reply #3 on: June 17, 2010 »
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.
 
Code: [Select]
'
' 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
« Last Edit: June 17, 2010 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: TinyPTC alpha blending?
« Reply #4 on: June 17, 2010 »
Thanks for this mate - very much appreciated!
I will take a look at this asap - I am away for the weekend at Bletchley Park with the Retro Computer Museum so probably won't get chance until early next week! :)

Cheers
DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: TinyPTC alpha blending?
« Reply #5 on: June 18, 2010 »
Have a blast there!
looking out for your adventures :D
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: TinyPTC alpha blending?
« Reply #6 on: June 26, 2010 »
Clyde - Love the way you have done this . . . here is something I have done already - this will be very useful indeed! ;)

Code: [Select]
'
' draw image alpha
' for drewpee.
'
option explicit
option static

Dim Shared as Integer bc,a,qq
bc=256
Dim Shared as Integer x(bc),y(bc),x1(bc),y1(bc),alp(bc)

const as integer SCREEN_WWIDTH=640
const as integer SCREEN_HEIGHT=480
const as integer IMAGE_WWIDTH=24
const as integer IMAGE_HEIGHT=24
#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

for a=1 to bc
x(a)=int(rnd(1)*(SCREEN_WWIDTH-image_wwidth))
y(a)=int(rnd(1)*(SCREEN_HEIGHT-image_height))
qq=int(rnd(1)*16)
if qq<=8 then x1(a)=1+int(rnd(1)*2):y1(a)=1+int(rnd(1)*2)
if qq>=9 then x1(a)=-1+int(rnd(1)*-2):y1(a)=-1+int(rnd(1)*-2)
alp(a)=32+int(rnd(1)*128)
next a

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 )=&hffffd000' mellow yellow with alpha channel set to 255
        next
    next
end sub

sub run_demo()
    while inkey<>chr(27)
        clear_screen()
        for a=1 to bc
        draw_image_alpha(x(a),y(a),alp(a))
        x(a)=x(a)+x1(a)
        y(a)=y(a)+y1(a)
        if x(a)>=(SCREEN_WWIDTH-image_wwidth) then x1(a)=-x1(a)
        if x(a)<=0 then x1(a)=-x1(a)
        if y(a)>=(SCREEN_HEIGHT-image_height) then y1(a)=-y1(a)
        if y(a)<=0 then y1(a)=-y1(a)
        next a
        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

Thanks a million for this mate. Very much appreciated :)

Drew
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: TinyPTC alpha blending?
« Reply #7 on: June 27, 2010 »
Looking Great!
btw comment out method1. a/ it's not quite right - in my quick example it worked with the 2 images overlapping.

And b/ you'll get a speed boost as I meant to say comment out which one you prefer, as currently it's performing both.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: