Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Terrain with masked bitmaps

This thread is locked; no one can reply to it. rss feed Print
Terrain with masked bitmaps
ingo
Member #6,576
November 2005

I would like to code a game that has Liero-style terrain destruction. (Think worms, scorched earth, etc). I get the theory behind what I am trying to do, I am just having trouble thinking about how to translate it into code.

Maps have two bitmaps: the actual fresh, clean map that isn't touched, and the mask, that has the information about which pixels can be destroyed, solid, already empty, etc.

To accomplish this, I need to be able to do this:
http://img258.imageshack.us/img258/7730/masksdn9.png

I do not believe the magic pink color would not work for this method, because there will be multiple colors that I will be checking.

Thank you for your help :)

James Stanley
Member #7,275
May 2006
avatar

What do you mean by 'checking multiple colours'?
You can only (AFAIK) use magic pink or palette[0] as invisible...

EDIT:
I am doing destructable environments in my current game. I haven't started coding it yet, but when I was thinking I ran into a problem: Magic pink is invisible, so drawing that on to something will have no difference. You blit the mask on and then use a couple of nested for loops to convert your mask colour to magic pink. Unless anybody knows of a more elegant solution...

Kauhiz
Member #4,798
July 2004

Quote:

Magic pink is invisible, so drawing that on to something will have no difference.

Unless you use regular blit ::)

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

CGamesPlay
Member #2,559
July 2002
avatar

I used this post to help me, but it required a lot of changing.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ingo
Member #6,576
November 2005

I know, you can only use the magic pink by default. This is why I am not asking anything about the built in functions.

Like i said before, there will be the map that looks all pretty, and then a colored mask for that map. To explain in more detail, the colored mask has different colors to specify what's going on. For example, anything that is black is solid, anything that is grey is destroyable, anything that is red is insta-death, anything that is white is free space. I am horrible about explaining things, so here is a diagram i found:

http://jnrdev.72dpiarmy.com/en/jnrdev5/files/1_bmp2map.gif

that should pretty much explain it. See how the mask is multicolored? Basically i want to use the mask as a cookie cutter. Any pixel on the mask that is white, don't draw it when displaying the map. there are two bitmaps- a mask and a map. The map isn't manipulated at all at runtime, only the mask. Does this make more sense?

Thank you for all your responses. ;D

CGamesPlay
Member #2,559
July 2002
avatar

Er, just use three bitmaps. Draw the entire terrain mask, and when you destroy terrain form that, blit the transparent mask color to it. Then blit the non-destroyable mask over the destroyable mask, that way one cannot destroy the non-destroyable mask.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

ingo
Member #6,576
November 2005

Thanks for the replies, but my main question still hasn't been answered (although I am getting some good extra information). So people understand, I'll try once more to see if this makes more sense on what I am asking.

I have mask.bmp and map.bmp. They are the same size. How do I get map.bmp to only display what the mask wants?

Here is that picture again:
http://img258.imageshack.us/img258/7730/masksdn9.png
Showing mask.bmp and map.bmp, and the final screen.

Matthew Dalrymple
Member #7,922
October 2006
avatar

The only way I can think of doing this would be to use putpixel and getpixel

1BITMAP* terrain;
2BITMAP* mask;
3BITMAP* map;
4PALETTE palette;
5 
6terrain = load_bitmap("terrain.bmp", palette);
7mask = load_bitmap("mask.bmp", palette);
8map = create_bitmap(terrain->w, terrain->h);
9 
10for(int y = 0; y < terrain->h; y++)
11{
12 for(int x = 0; x < terrain->w; x++)
13 {
14 if(getpixel(mask, x, y) == makecol(/*YOUR MASK COLOR HERE*/))
15 {
16 putpixel(map, x, y, getpixel(terrain, x, y));
17 }
18 }
19}
20 
21destroy_bitmap(terrain);
22destroy_bitmap(mask);
23destroy_bitmap(map);

I didn't compile that of course. I hope that's what you were looking for.

=-----===-----===-----=
I like signatures that only the signer would understand. Inside jokes are always the best, because they exclude everyone else.

ingo
Member #6,576
November 2005

Exactly what I was looking for, Matthew! Thanks a bunch. Also, thanks to all who inputted, you've given me solutions to other questions too. Cheers.

gnolam
Member #2,030
March 2002
avatar

Quote:

makecol(/*YOUR MASK COLOR HERE*/))

bitmap_mask_color()
:P
And move it outside the loop. Also, use the "unsafe" versions of getpixel and putpixel to make it a bit less horribly slow. Which leads me to the last point, which is that you don't want to do that every frame - you'll want something along the lines of what CGames suggested.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

ingo
Member #6,576
November 2005

Yeah, I have a function called update_map() that does what Matt suggested. It is only called at the beginning, and then whenever I need the map to change.

Thanks for the bitmap_mask_color() function.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

I have mask.bmp and map.bmp. They are the same size. How do I get map.bmp to only display what the mask wants?

Okay, that was answered in my first post. But apparently you're happy with 3 FPS ;)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

James Stanley
Member #7,275
May 2006
avatar

Quote:

Unless you use regular blit

If you use a regular blit you have to apply colour to the whole image. :)

Go to: