Archived

This topic is now archived and is closed to further replies.

Masking in VB

This topic is 5155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a 2D puzzle game, and I need to be able to draw sprites on top of an image background using masking. I have created masks for the sprites (black where there will be color, and white everywhere else), and for the sprite itself, I have white everywhere I don''t want drawn. When drawing the mask, I used PaintPicture with the operand vbSrcAnd. When drawing the image, I used the same method and vbDstInvert. Both images are 24-bit bitmaps. The problem is, when the mask is applied, it looks just like I used vbSrcCopy; there isn''t any actual masking. Any ideas? I''ve thought about going with DirectX, but the GDI is simple and fast enough for me right now.

Share this post


Link to post
Share on other sites
I don''t remember the constants offhand, though I believe it''s SRC_AND and SRC_INVERT. In any event, the hardcoded value is 0xEE0000 and 0x880000 (not sure which is which, but I believe that''s correct).

You should have BLACK where you don''t want to draw, though. See, if you AND anything with white (0xFFFFFF; for your mask) you end up with, well, anything that was there to begin with since all the 0 bits are set to 0 in your white because of the AND. Now that your mask is applied, you only have a black outline of your sprite where you want it to show up and masked regions remain untouched. So you OR with your sprite. Anything OR 0x000000 gives Anything (as all 1 bits in 0x000000 will be set to 1 by the OR).

Now assuming the masked region of your sprite is white, you''ll be ORing with 0xFFFFFF, which, well, sets all bits of your destination surface to 1 (ie, 0xFFFFFF), giving you a white outline. If you OR with 0x000000 (black), you go about this the opposite way and no bits on your destination surface are changed, effectively masking the sprite.

In short...

Mask: White outline, black sprite. AND with destination.
Sprite: Black outline, colored sprite. OR with destination.

Share this post


Link to post
Share on other sites
Are there any special settings for the drawing target (the picture box, in this case), or for the image sources (again picture boxes)? What about the image format? Does 24-bit .bmp work just fine? I''ve compared my code with a previous program I wrote that masks just fine, and everything looks the same, but it''s not working here. In fact, I''ve applied JUST the mask, and no matter what operand I try it looks like it''s just doing a direct copy. *TEARS OUT HAIR*

Share this post


Link to post
Share on other sites