Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Trouble with tiles and transparency

This thread is locked; no one can reply to it. rss feed Print
Trouble with tiles and transparency
Matthew Dalrymple
Member #7,922
October 2006
avatar

I posted a topic about my code, but I found that I believe the solution can be found by asking this question instead of the other. Something is wrong with my transparency and I'm having a lot of problems with it.
I made the background of the screen white to show my problem. I tiled using this tileset:
http://img139.imageshack.us/img139/767/tiledu3.png
My grid is 3x3 using all of tile number 2 (The green one)
I then draw another tile to the screen coord (20, 20). This 2nd tile that I draw is tile 3 (the transparent one). Now this should just display the 3x3 grid of the green tiles.. because the tile drawn over them is transparent right? Well this is what I actually see:
{"name":"error2yd8.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/8\/c81a84b4d26c617e32aeccc19bf161fe.png","w":646,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/c\/8\/c81a84b4d26c617e32aeccc19bf161fe"}error2yd8.png
Here is the relevant code:
Main:

1BITMAP *tile;
2BITMAP* draw;
3PALETTE palette;
4set_palette(palette);
5 
6cTile Tile;
7Tile.LoadTileSet("tile.bmp", 64, 64, palette)
8tile = create_bitmap(64, 64);
9draw = create_bitmap(64,64);
10draw = Tile.GetTile(3);
11tile = Tile.GetTile(2);
12while(!key[KEY_ESC])
13{
14 for(int y = 0; y < 3; y++)
15 {
16 for(int x = 0; x < 3; x++)
17 {
18 thescreen.DrawToBuffer(tile, 0, 0, tile->w * x, tile->h * y);
19 }
20 }
21 thescreen.DrawToBuffer(draw, 0, 0, 20, 20);
22 vsync();
23 thescreen.Swap();
24}
25destroy_bitmap(tile);
26destroy_bitmap(draw);

Tile:

1BOOL cTile::LoadTileSet(char* Filename, int TileWidth, int TileHeight, PALETTE Palette)
2{
3 m_Filename = Filename;
4 m_TileSet = load_bitmap(Filename, Palette);
5 if(!m_TileSet) { return FALSE;}
6 if(TileWidth == 0 || TileHeight == 0) { return FALSE;}
7 m_TileWidth = TileWidth;
8 m_TileHeight = TileHeight;
9 if(m_TileSet->h % TileHeight != 0) { return FALSE;}
10 m_Rows = m_TileSet->h / TileHeight;
11 if(m_TileSet->w % TileWidth != 0) { return FALSE;}
12 m_Cols = m_TileSet->w / TileWidth;
13 return TRUE;
14}
15BITMAP* cTile::GetTile(int TileNum)
16{
17 // Is the Tile Number in range?
18 if(TileNum > ((m_Cols * m_Rows) - 1))
19 {
20 allegro_message("TileSet: %s\nTile: %d\nDoes not exhist!", m_Filename, TileNum);
21 TileNum = ((m_Cols * m_Rows) - 1);
22 allegro_message("Using Tile: %d", TileNum);
23 }
24 BITMAP* tile;
25 tile = create_bitmap(m_TileWidth, m_TileHeight);
26 int tilexpos = 0;
27 int tileypos = 0;
28 
29 tileypos = ((TileNum / m_Cols) * m_TileHeight);
30 tilexpos = ((TileNum % m_Cols) * m_TileWidth);
31 masked_blit(m_TileSet, tile, tilexpos, tileypos, 0, 0, m_TileWidth, m_TileHeight);
32 return tile;
33}

Screen:

1BOOL cScreen::Swap()
2{
3 blit(BackBuffer, screen, 0, 0, 0, 0, BackBuffer->w, BackBuffer->h);
4 ClearBuffer();
5 return TRUE;
6}
7 
8BOOL cScreen::ClearBuffer()
9{
10 //clear_to_color(BackBuffer, bitmap_mask_color(BackBuffer));
11 clear_to_color(BackBuffer, makecol(250, 250, 250));
12 return TRUE;
13}
14 
15BOOL cScreen::DrawToBuffer(BITMAP* backbuf, int xpos, int ypos, int xdes = 0, int ydes = 0)
16{
17 masked_blit(backbuf, BackBuffer, xpos, ypos, xdes, ydes, backbuf->w, backbuf->h);
18 return TRUE;
19}

:-/:P
EDIT: Well after thinking more about the knowledge miran gave me from my old post

miran said:

You must not use masked_blit() when you copy the back buffer to the screen. ...

I went through and looked and I used masked_blit() in more than one place. I see that using this more than once on a given bitmap is not a good idea. I now have changed my GetTile() method to return a bit() of the tileset instead of a masked_blit. I am now only using masked_blit in the DrawToBuffer() method. I guess solving your own problems is always best sometimes.
EDIT: FIXED: FYI That means IT WORKS now.. and when I use
clear_to_color(BackBuffer, bitmap_mask_color(BackBuffer));
It actually draws the pink color instead of the black I was seeing.

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

Go to: