Transparent Images

BlitzPlus Forums/BlitzPlus Programming/Transparent Images

InvincibleWall(Posted 2011) [#1]
I decided now that I have learned about circles and angles and Sin(), Cos(), Tan() and the rest (thanks to all that helped with that) I'm going to test my knowledge of them by making a space shooter similar in nature to Star Sonata (kinda) but i looked at my old flame and energy effects and they do not belong in space, actually they do not belong in any non-Sribblenauts type game. They have no transparency so it is only circles and rectangles changing sizes and colors before disappearing.

well you get my point I'm trying to learn the art of alpha channels and transparency. I am currently studying code archives examples and they seem far to slow to use in real time... is it possible to do real time transparency in B+ ? I am for the most part just trying to make rectangles, circles and lines appear to glow, fade out or fade in to (ontop of many different backgrounds and space ships) simulate lasers, fire, plasma and any other type of glowing energy your can think of...

any references/articles/books/code etc... that you guys have found that would help to learn how to make a real time transparency function would be great

btw I'm trying to learn how to do this not just have code that does it, I like understanding exactly how everything works in my code

Thanks Ahead of time


xlsior(Posted 2011) [#2]
It's always going to be slow in blitzplus, since it runs in 2d mode... you'll have to do the math, mix and draw pixel by pixel.

Years ago I came across a dll that could draw transparent sprite in blitzplus pretty fast, but I don't remember its name.

It's always going to be much slower in blitzplus than it would be in a language like blitzmax though, since that one uses 2d-in-3d, and can use the hardware acceleration of the videocard to do blending, scaling, rotating etc.


InvincibleWall(Posted 2011) [#3]
hhhmmm okay, I was playing around with readpixel/writepixel (fast) and I noticed that I could make something transparent if I added a little to the color integer before drawing it (aiming for the alpha value in it)

is there a way to convert that integer into 4 different variables r,g,b,a so that I can edit them?

and one random off topic-ish question
is BlitzMax that good? I was thinking of getting Blitz3D because, well.....
3D > 2D in most situations (and it can still do 2D anyway)
is BlitzMax > Blitz3D ?


Yasha(Posted 2011) [#4]
is there a way to convert that integer into 4 different variables r,g,b,a so that I can edit them?


Yes:
a = (col And $FF000000) Shr 24
r = (col And $FF0000) Shr 16
g = (col And $FF00) Shr 8
b = col And $FF

Use Or and Shl to combine them back into one integer.

is BlitzMax that good? I was thinking of getting Blitz3D because, well.....
3D > 2D in most situations (and it can still do 2D anyway)
is BlitzMax > Blitz3D ?


Way to kick a hornet's nest...

The only real pro point for Blitz3D is that it's more or less the same language as BlitzPlus, so you wouldn't have to learn very much new. All of its other pros are evenly matched by BlitzMax.

BlitzMax on the other hand has a bunch more good points. The ones that really stand out from B3D are that it has native OO (I'm sure you knew that) which gives you some more options for structuring your programs, and a much, much better compiler so it produces optimised code (at least, pretty good... not sure how it compares to top-end optimisers but whatever). The breaking-changes are not that bad, and most of them are for the better anyway (e.g. it has a garbage collector; linked lists are no longer tied to types). Also, BlitzMax is $20 cheaper.

In truth in terms of 2D and 3D support they're actually pretty evenly matched - all the BlitzMax 3D engines except miniB3D can be used with Blitz3D as userlibs, and libs like Draw3D give Blitz3D the same 2D power as the Max2D engine. Although why you would get Blitz3D only to use a third-party 3D engine is beyond me (in fact, couldn't you do this in BlitzPlus too?).

Last edited 2011


InvincibleWall(Posted 2011) [#5]
Cool I just found a similar (not as direct) example on the BlitzMax Forums... but since I have very little knowledge on how this works I can't figure out how to combine them after I edit them

how is (rgba Shr 16) And $FF) = 0
while rgba Shr 16 = 65280
and the original color integer was -16765441

what is $FF anyway?

btw my programming knowledge is quite newbish since I'm still in highschool so don't squish my brain >_<


Yasha(Posted 2011) [#6]
Ah, OK...

First thing: in BlitzPlus and Blitz3D, "And", "Or" and "Xor" are bitwise operations, not just logical (in BlitzMax there's a distinction between And and BitAnd, etc.). This means that it compares all 32 bits of an integer to produce a result value, not just "is that integer False or not?" which is what a logical operator would do.

So 1 And 2 = 0 because the 1 bit and the 2 bit don't overlap, meaning no bits are set in both numbers. 1 Or 2 = 3 because it combines the set bits from both.

Second thing: $012A4D is how you enter number in hexadecimal format. The compiler perceives this as an integer same as if it were written in decimal. You can also write in binary: %0100100110011. The reason we would want to do this is because the format may better represent the way we want to use the data: if a number is representing bitflags, for instance, binary is good because it shows exactly which bits are set. It's common to use hex for colours, because the four-byte integer divides perfectly into eight characters, two for each component, and they line up exactly with the bytes, which decimals don't.

So a colour is represented as four bytes, representing the four component values. They are arranged in a row. So this hopefully explains how the And extracts a value: col And $00FF0000 uses And to ignore the a, g and b bytes (where the mask value has no bits set) and grab all the data from the r byte ($FF is the highest value a single byte can hold, so all bits are set and therefore whatever was in the colour in that space will be reproduced in the result).

Because the red byte is two slots along, its actual numeric value is much higher than 255. Therefore we use the right shift operator to reduce it: move it 16 bits (or four letters) to the right quickly divides it by 65536. In this case it's easier to think of it as literally shoving the value over to the right hand side of the integer.

To invert this operation you need to not just swap the operators around, but use the opposite operation. Once you've got the value of a pixel, Shl shoves it back toward the left, to wherever it should be within the colour value, and then these "shoved" pixels are combined together with Or (since they only have data in non-overlapping bytes, Or will simply combine them all, as explained above).

Example:

col = (a Shl 24) Or (r Shl 16) Or (g Shl 8) or b


(b is already at the right position and doesn't need to be shifted.)


InvincibleWall(Posted 2011) [#7]
WOW, thats cool info thanks a ton


InvincibleWall(Posted 2011) [#8]
um.... what is the alpha value in this for?????

it is always read as 255 no matter what you write it as


Andy_A(Posted 2011) [#9]
Here's a transparent sprite routine from the archives that updates a 191x36 sprite in 2-3ms.

http://www.blitzmax.com/codearcs/codearcs.php?code=913




InvincibleWall(Posted 2011) [#10]
hhmm I read that earlier... it was very confusing as to how he did it though

Last edited 2011


InvincibleWall(Posted 2011) [#11]
thx for all the help guys

Last edited 2011


mv333(Posted 2011) [#12]
Years ago I came across a dll that could draw transparent sprite in blitzplus pretty fast, but I don't remember its name.


I think you're talking about the Extended B2D dll. I tried it over a year ago, and it worked great. The version i have is 1.60, i think that's the last one that came out.