Transparent color

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
beeb103
Newbie
Posts: 3
Joined: Thu Aug 11, 2005 12:02 pm
Location: US
Contact:

Transparent color

Post by beeb103 »

Hello everyone,

I am using Screen 13 and DATA for my graphics. I am trying to make the black transparent so you can't see it. Here's is a example,

data: 000, 150, 150, 150, 000
data: 000, 150, 150, 150, 000
data: 150, 150, 150, 150, 150
data: 000, 150, 150, 150, 000

Is there a way I can get the 000 to not be seen?

Thanks
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Yes, several ways actually:

1) Dont draw it (aka: Use your own routines instead of the built in ones)
2) Use a mask bitmap
3) Use FreeBASIC (www.FreeBASIC.net)

I dont have time to make up examples..
Maybe someone else will clarify it..

Sorry for the useless post :P
I have left this dump.
User avatar
The Awakened
Veteran
Posts: 144
Joined: Sun Aug 07, 2005 1:51 am

Post by The Awakened »

Well, I assume you're using the following routine:

Code: Select all


for y = 0 to yourwidth
        for x = 0 to yourheight
                   read pixel%
                   pset x, y, pixel%
        next
next

Put this instead of the pset

Code: Select all


if pixel% > 0 then
             pset x, y, pixel
end if

That way, if the pixel is 0, the routine will just skip over it. As for PUTing, you'll have to set up a mask... check out Apester's tutorial, that's where I first got my tips. It's right here: http://petesqbsite.com/sections/tutoria ... s.shtml#11

Good luck to you! :D
"Sorry for beating you up with a baseball bat Julian, but I DID think that you were a samsquanch."
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post by Zamaster »

A quick explanation o' masks... You can think of masks as holes you put down on an image that allow your sprite to fill them without messing up the background pixels or your sprites pixels. To make a mask you need to make a black cutout of your sprite and slap it on a box of white pixels that are the same size as your sprite, e.g.

The sprite 6x6 for simplicity in:

0,0,0,4,0,0
0,1,1,6,1,1
0,1,0,4,0,0
2,3,2,5,2,2
0,1,0,4,0,0
0,1,0,4,0,0

(note: the transparent pixels MUST be black!)

The Mask - F's for white(QB's color 15):

F,F,F,0,F,F
F,0,0,0,0,0
F,0,F,0,F,F
0,0,0,0,0,0
F,0,F,0,F,F
F,0,F,0,F,F

A quick way to make a mask is to scan over your sprite
and set the masks pixels to White if the color is 0 or
black if it equals anything else

Code: Select all

FOR x% = 0 TO 15
   FOR y% = 0 TO 15
      col% = sprite(x% ,y%)
      IF col% = 0 THEN 
         col% = 15
      ELSE
         col% = 0
      END IF
      mask(x%, y%) = col%
   NEXT y%
NEXT x%
The next step is to PUT(preferrable) the mask down using the AND
operrator, then PUT down the sprite with the OR operrator:

PUT (10, 10), yourmask%(0), AND

PUT (10, 10), yoursprite%(0), OR

and TADA!!!!! You have a sprite with a transparent background!
One problem with masks though, as opposed to a custom
"Dont draw the black pixels" routine. Masks take up twice as much
space cause you need to store the sprite and the mask.

Hope this helps!
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

This thread is definately NOT in the right forum.
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

Yeah.. seems the new thing to post questions in the News forum.. oh well..
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Or use rellib, dqb or some other library. The sooner you learn them, the better off you are.
Image
beeb103
Newbie
Posts: 3
Joined: Thu Aug 11, 2005 12:02 pm
Location: US
Contact:

OOPS

Post by beeb103 »

WHAT? I thought I posted in the Help forum...sorry, lol.

Thanks everyone, I will try some of your ideas.
User avatar
The Walrus
Veteran
Posts: 87
Joined: Fri Apr 01, 2005 7:19 am
Location: Denmark
Contact:

Post by The Walrus »

There's also a tutorial on putting transperant sprites here:

http://www.qbasicnews.com/tutorials.php ... =view&id=7
If swimming is so good for your figure, how do you explain walruses?
beeb103
Newbie
Posts: 3
Joined: Thu Aug 11, 2005 12:02 pm
Location: US
Contact:

Thanks

Post by beeb103 »

I used the mask and it worked.

Thanks everyone.
Rattrapmax6
Veteran
Posts: 1055
Joined: Sun Jan 02, 2005 2:11 pm
Location: At my computer
Contact:

Post by Rattrapmax6 »

It's all this work that makes FreeBasic Trans option look so nice:

Code: Select all

PUT (x, y), sprite, trans
:roll:
-Kevin (aka:Rattra)
(x.t.r.GRAPHICS)
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post by Zamaster »

Wait my bad, I forgot to say why the whole AND and OR thing work...
Okay so you know that a mask's background pixels are white and there is a big black spot in the middle which is your sprite. When you AND on your mask here is what happens:

the masks pixels which are white are in binary as:

11111111

the pixels under the white(the background colored pixels) are in
binary as... we'll say the qb color light red(12):

00001100

now ANDing them together ANDs each bit with the other corresponding bit in the other binary number:


11111111

AND

00001100
======
00001100


Exactly the second number(or the pixels your are ANDing the white on to)! So thats why the white pixels dont get drawn.
Now as for the black (qb color 0):


00000000

AND

00001100
======
00000000

You get 0. Thats why the black pixels show up. But the actual sprite gets ORd on so you're ORing the black and ORing the actual color:

00000000 <-- sprites transparent black

OR

00001010 <-- backgrounds green
======
00001010

You get the background color. And finally for the sprites solid pixels:

00000111 <-- sprites color grey

OR

00000000 <-- cutouts black (made when ANDing the mask)
======
00000111

So all in all, this fancy mix of opperrators creates a transparent sprite!
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
User avatar
romassoldier
Newbie
Posts: 4
Joined: Tue Aug 30, 2005 9:03 am

Post by romassoldier »

HI! My problem isn't in the DATA transparent colors. I use 8 bits .BMPs, and I wanna put the image onto the screen, but the background won't be visible.
I make a BMP file, which is the mask(I saw it in the Sonix game), and the background is 50% grey.
Image
(rehosted, this isn't the original!)
And I open this bmps, and GET dr1, and dr1mask.
BUT if I use this code

Code: Select all

PUT dr1 (1, 1), AND
PUT dr1mask (1, 1), OR
Then i don't give what i want. (Transparent backgound...)
Last edited by romassoldier on Sat Mar 10, 2007 3:17 pm, edited 3 times in total.
User avatar
Zamaster
Veteran
Posts: 174
Joined: Wed Jun 15, 2005 1:51 pm

Post by Zamaster »

Okey pokey, now take a look at the pic on the left. That background cant be pink if your doing a mask, the background needs to be black(QB color 0). Now for the pic on the right, that grey background needs to be white(QB color 15) and all the main pixels, the colored ones that is, need to be black. So you have a sprite with a black background one the left and a white background with a black cutout of your sprite on the right.

An example sprite:

0,1,4,0
1,0,0,4
4,0,0,1
0,4,1,0

That sprites mask:

15,0,0,15
0,15,15,0
0,15,15,0
15,0,0,15


If I could post pics I would. Read my posts again till you get the
hang of it and good luck!(Nice job on those sprites!)
C:\DOS
C:\DOS\RUN
RUN\DOS\RUN
User avatar
romassoldier
Newbie
Posts: 4
Joined: Tue Aug 30, 2005 9:03 am

Post by romassoldier »

ok I will try it, and thanks.
Last edited by romassoldier on Sat Mar 10, 2007 3:18 pm, edited 1 time in total.
I came, I saw, and I didn't believe my eyes...
Dave Unit

Post by Dave Unit »

Ummm... I'm a beginning program, but the problem I see is that the mask is coming after the original sprite. Try putting the mask first, that should fix your problem. I think. =P
Post Reply