Jump to content
  • Advertisement

Archived

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

Zxylin

Transparency on 2D Tiles with VB/DX8

This topic is 5934 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

Okay, let me start by saying that after years of lurking, "thinking about it" and lofty starts I''m finally working on a project I can actually finish; so please bear with me. I''m working on a simple 2D dungeon game using tiles. I''m using VB6 and DirectX8/Direct3D. I currently have a basic tileset and some test levels. My question is, how do I make the background of my tiles transparent so that the tile behind it shows through? For example, an item is sitting on the floor; currently the bitmap for the item tile has a copy of the floor texture for a background. Rather than drawing the floor for every item/monster tile, I want it to be transparent. The tiles are drawn as textures on a 3D square. Am I making any sense? Any suggestions? Open your eyes and free your mind That you may see the subtle wonder Of the worlds we live in...

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Basically what you want to do is go through the pixels of the tile your blitting, and if that pixel is the transparent color you pick (usually black or RGB(255, 0, 255)(magenta)) don''t blit it, otherwise you do blit it. In C/C++:

for(int i = 0; i < TILE_HEIGHT; i++)
{
for(int j = 0; j < TILE_WIDTH; j++)
{
// tile is a multidimensional array of pixel data
if(tile[j] != TRANSPARENT_COLOR)
{
PutPixel(x, y, tile[j][i]);
}
}
}

... but this should be easy enough to convert to VB.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I messed that last one up.
Here...

Basically what you want to do is go through the pixels of the tile your blitting, and if that pixel is the transparent color you pick (usually black or RGB(255, 0, 255)(magenta)) don''t blit it, otherwise you do blit it. In C/C++:

for(int py = 0; py < TILE_HEIGHT; py++)
{
for(int px = 0; px < TILE_WIDTH; px++)
{
// tile is a multidimensional array of pixel data
if(tile[px][py] != TRANSPARENT_COLOR)
{
PutPixel(x + px, y + py, tile[px][py]);
// or whatever your pixel function is
}
}
}

... but this should be easy enough to convert to VB.

Share this post


Link to post
Share on other sites
Umm... Okay.

I don''t have a pixel function. I''m drawing the tiles by loading the tilemap as a texture and using a portion of the texture corresponding to the tile I want as the texture for a 4-Vertex Triangle strip (square). I have seen some reference to doing exactly what you said though using alpha testing. Might this be a possible way to go?

Open your eyes and free your mind
That you may see the subtle wonder
Of the worlds we live in...

Share this post


Link to post
Share on other sites
Per pixel is not the way to go. You want to use alpha testing and/or alpha blending.

When you load the texture of tiles, with the color black being the areas you want transparent (please excuse any errors, I don''t really use VB):


  
set pTexture = D3DX.CreateTextureFromFileEx(pD3DDevice, "texture.bmp", D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, &HFF000000, 0, 0)


After setting the texture to use, and are ready to render your quads, enable the alpha testing:


  
pD3DDevice.SetRenderState(D3DRS_ALPHATESTENABLE, TRUE)
pD3DDevice.SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL)
pD3DDevice.SetRenderState(D3DRS_ALPHAREF, 8)


From here on, all black pixels in the loaded texture should show through.



Jim Adams
home.att.net/~rpgbook
Author, Programming Role-Playing Games with DirectX
and Focus On: Advanced Animation with DirectX

Share this post


Link to post
Share on other sites
Alright! I got it working now.

Thanx for the suggestion AP, it turns out that I can use the AlphaTest renderstate to do what you said for me. There''s an article in the resources that explains how to set it.

Now I just need to edit the rest of my tiles...

------------------------------------------------------------
Open your eyes and free your mind
That you may see the subtle wonder
Of the worlds we live in...

Share this post


Link to post
Share on other sites
Thanx Jim, that''s pretty much what I did. Question: why 8 for the AlphaRef? I used 0 and D3DCMP_GREATER for the AlphaFunc.

------------------------------------------------------------
Open your eyes and free your mind
That you may see the subtle wonder
Of the worlds we live in...

Share this post


Link to post
Share on other sites
I asked a related question a few days ago that might be helpful to you if you''re planning to have to hit test or collosion detect against these sprites/tiles.

http://www.gamedev.net/community/forums/topic.asp?topic_id=113593

Hope this helps.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!