LB Booster
Programming >> Compatibility with LB4 >> semi-transparent sprite
http://lbb.conforums.com/index.cgi?board=compatibility&action=display&num=1328735655

semi-transparent sprite
Post by tsh73 on Feb 8th, 2012, 8:14pm

This *could* affect compatibility, though probably this is not much used and pretty sure undocumented.
With sprites, it is possible to create not black-and-white mask, but grayscale. And on some backgrounds it gives semi-transparent sprite.
This code creates red sprite with right part going transparent (to white).
Now, LBB works differently. It goes opaque for some threshold (more then half a sprite), then colors get inverted (XOR?) with background showing though.
(just run it to see)
Code:
'Q: is semitransparent sprite, red strip on white
'LB vs LBB difference
nomainwin

WindowWidth = 600
WindowHeight =  300

open "test" for graphics_nsb as #gr
#gr "trapclose [quit]"
#gr "down"

    for i = 1 to 200
        x=rnd(1)*600
        y=rnd(1)*300
        r=rnd(1)*100+10
        #gr "place ";x;" ";y
        #gr "circle ";r
    next
    #gr "getbmp bg 0 0 600 280"
    #gr "background bg"

d=30
for i = 0 to 255
    #gr "color black"
    #gr "line ";i;" ";0;" ";i;" ";d
    #gr "color red"
    #gr "line ";i;" ";d;" ";i;" ";2*d

    c$=i;" ";i;" ";i
    #gr "color ";c$
    #gr "line ";i+256;" ";0;" ";i+256;" ";d
    #gr "color red"
    #gr "line ";i+256;" ";d;" ";i+256;" ";2*d
next
#gr "Getbmp sprite 0 0 512 ";2*d
#gr "Addsprite sprite sprite"
#gr "Spritexy sprite 40 100"

#gr, "Drawsprites"
wait
'-----------------------------------
[quit]
    close #gr
    call tryUnloadBMP "sprite"
    call tryUnloadBMP "bg"
end

sub tryUnloadBMP picName$
on error goto [dontCare]
    unloadbmp picName$
[dontCare]
end sub
 

Re: semi-transparent sprite
Post by Richard Russell on Feb 8th, 2012, 9:39pm

on Feb 8th, 2012, 8:14pm, Guest-tsh73 wrote:
With sprites, it is possible to create not black-and-white mask, but grayscale.

Yes, I was aware of this issue (although I don't mention it in the docs). It shows up in the sample program hockey.bas supplied with LB.

It would be difficult for me to modify LBB to be compatible with this feature because the sprite is immediately converted to a regular Windows icon using GetDIBits and CreateIconFromResourceEx. Thereafter all sprite operations are carried out using the icon API, which is very convenient.

As you say, it's an undocumented feature so I'm not too concerned.
Re: semi-transparent sprite
Post by Richard Russell on Feb 9th, 2012, 3:55pm

on Feb 8th, 2012, 8:14pm, Guest-tsh73 wrote:
With sprites, it is possible to create not black-and-white mask, but grayscale.

Actually, if you look carefully, that's not what's happening. Towards the right-hand end of the sprite, where you have made it partially 'transparent', the background circles are red when they should be black:

User Image

This is what it should look like:

User Image

If LB had been performing a true transparency I might have wanted to try to emulate it, but evidently what it actually does is something strange which probably has little practical value.

Richard.

Re: semi-transparent sprite
Post by tsh73 on Feb 12th, 2012, 8:06pm

Well, it "kind of" works, so one might find it useful.
I used this to make semi-transparent hairline on a slide rule.
JB forum::JB Programs Shared ::Slide rule
And it indeed works in LBB, and semitransparent too - just cyan instead of red. wink
(yes I know I could check if program works under LBB and change color).
Re: semi-transparent sprite
Post by Richard Russell on Feb 12th, 2012, 9:01pm

on Feb 12th, 2012, 8:06pm, Guest-tsh73 wrote:
And it indeed works in LBB, and semitransparent too.

It's impossible for a sprite mask in LBB to be semi-transparent - it's a 1-bit-per-pixel black-and-white bitmap!

In the slide-rule program what you've made (in LBB) is an XOR sprite, in other words the cursor exclusive-ORs whatever lies beneath it with red. So when it covers the white background it appears cyan, and when it covers the black scale or text it appears red.

I mentioned before that LBB's sprites are in fact icons, and that's how they work. There's an AND bitmap (the mask) and an XOR bitmap (the foreground). In a conventional LB sprite you always arrange that where the AND bitmap is white (allowing the background through) the foreground bitmap is black. But if you have some non-black foreground in that region it will plot in an XOR fashion.

In principle I could modify LBB's sprites to have a genuine transparent mask, but it wouldn't be compatible with what LB does so I don't see any point.

Richard.

P.S. The slide rule program is very nice indeed, and (apart from the graticule colour) I'm pleased that it seems to work so well in LBB too!
Re: semi-transparent sprite
Post by Richard Russell on Feb 18th, 2012, 5:06pm

on Feb 9th, 2012, 3:55pm, Richard Russell wrote:
If LB had been performing a true transparency I might have wanted to try to emulate it, but evidently what it actually does is something strange

I take it back: it's not LB misbehaving, it's your program! The norm in Windows is for sprites/icons to have a pre-multiplied foreground; that is, the sprite's foreground should be multiplied by the alpha value (transparency). You are not doing that in your program, which is why the background circles appear red instead of black. If you modify the code like this then it works as one would expect:

Code:
for i = 0 to 255
    #gr "color black"
    #gr "line ";i;" ";0;" ";i;" ";d
    #gr "color red"
    #gr "line ";i;" ";d;" ";i;" ";2*d

    c$=i;" ";i;" ";i
    #gr "color ";c$
    #gr "line ";i+256;" ";0;" ";i+256;" ";d
    c$=255-i;" ";0;" ";0
    #gr "color ";c$
    #gr "line ";i+256;" ";d;" ";i+256;" ";2*d
next 

As LB appears to implement the transparency correctly (assuming a pre-multiplied foreground), I will attempt to do so in LBB too.

Richard.
Re: semi-transparent sprite
Post by tsh73 on Feb 20th, 2012, 07:25am

Quote:
The norm in Windows is for sprites/icons to have a pre-multiplied foreground; that is, the sprite's foreground should be multiplied by the alpha value (transparency). You are not doing that in your program,

I was never aware of it. Looks really interesting.
Thanks for the info.
I will do some tinkering with that, when time permits.
Re: semi-transparent sprite
Post by Richard Russell on Feb 20th, 2012, 09:17am

on Feb 20th, 2012, 07:25am, Guest-tsh73 wrote:
I will do some tinkering with that, when time permits.

In your slide rule program you could usefully change this line:

Code:
#gr2 "color red" 
to
Code:
#gr2 "color 63 0 0" 

Since the 'transparency' value for the line is set to 192 (lightgray) the amplitude of the 'foreground' shouldn't exceed 63, so that when it is superimposed on a white background it doesn't clip.

You probably won't see any difference, but this change would give a slightly more realistic 'semi-transparent' line if it was superimposed on a more complex background.

Richard.
Re: semi-transparent sprite
Post by tsh73 on Feb 20th, 2012, 10:11am

Quote:
You probably won't see any difference

Exactly.
But it always nice to know "why".
Re: semi-transparent sprite
Post by Richard Russell on Feb 25th, 2012, 5:40pm

on Feb 18th, 2012, 5:06pm, Richard Russell wrote:
I take it back: it's not LB misbehaving

I know I keep changing my mind, but LB definitely isn't exhibiting true transparency. Try running this program under both LB (4.04) and LBB (1.80):

Code:
'LB vs LBB difference
nomainwin

WindowWidth = 600
WindowHeight =  300

open "test" for graphics_nsb as #gr
#gr "trapclose [quit]"
#gr "down"

loadbmp "bg", "C:\progra~1\libert~1.04\sprites\bg1.bmp"
#gr "background bg"

d=130
for i = 0 to 255
    c$=i;" ";i;" ";i
    #gr "color ";c$
    #gr "line ";i;" ";0;" ";i;" ";d
    #gr "color black "
    #gr "line ";i;" ";d;" ";i;" ";2*d
next
#gr "Getbmp sprite 0 0 256 ";2*d
#gr "Addsprite sprite sprite"
#gr "Spritexy sprite 0 0"
#gr "SpriteScale sprite 234"

#gr, "Drawsprites"
wait

[quit]
    close #gr
    unloadbmp "sprite"
    unloadbmp "bg"
end 

Under LBB the transparency works as you would expect, with the sprite being fully opaque on the left and fully transparent on the right, with a gradual transition.

But under LB the sky and the clouds behave as they do in LBB, but the mountains and the rest of the background behave as though the sprite is fully opaque in the left half and fully transparent in the right half:

User Image

In other words, depending on the background colour the sprite exhibits both linear transparency (like LBB does now) and binary transparency (like LBB did previously). My mind is boggling at this result - it appears to make no sense at all!

Richard.