Archived

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

Whackjack

DirectX and color key transparency

Recommended Posts

Whackjack    100
In my Direct3D 8 book, the author says that DirectX doesn''t have a built-in way to color key transparency for loaded bitmaps. He instead uses a cumbersome function that he wrote that loads in each pixel of the bitmap one at a time and tests to see if it matches the predetermined color key. Is there truly not an API function that does this, or did the author just go out of his way to make things more complicated?

Share this post


Link to post
Share on other sites
RhoneRanger    100
The author went out of his way to make a function of his own. If he SAYS there is no function, he either does not know, or wants you to think his way is better.

The D3DX Library has a function called D3DXCreateTextureFromFileEx, and there is a parameter there that askes for the color key.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Check out this function, it simplifies stuff even more however before it ignores the pixels set the alpha test in your initializer like this
d3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true);
d3dDevice->SetRenderState(D3DRS_ALPHAREF, 0x01);
d3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

so here is the code. use it if it helps any.


LoadSurface(LPDIRECT3DSURFACE8* ppSurface, char *filename)
{
D3DCOLOR colourKey = D3DCOLOR_ARGB(0,255,0,255);
LPDIRECT3DSURFACE8 pSurface;
D3DXIMAGE_INFO srcInfo;
PALETTEENTRY palette[256];

d3dDevice->CreateImageSurface(1, 1, d3dCFormat, &pSurface);
D3DXLoadSurfaceFromFile(pSurface, NULL, NULL, filename, NULL, D3DX_FILTER_NONE, 0, &srcInfo );
pSurface->Release();

d3dDevice->CreateImageSurface(srcInfo.Width, srcInfo.Height,
d3dCFormat, ppSurface);
pSurface = *ppSurface;

D3DXLoadSurfaceFromFile(pSurface, palette, NULL, filename,
NULL, D3DX_FILTER_NONE, colourKey, &srcInfo);
}


I think that code was from this site, but i forget

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
One quick note, in the code i posted d3dCFormat is actually just a variable for D3DFMT_A8R8G8B8 which is what should go there.

Share this post


Link to post
Share on other sites
TalonSnow    128
most likely the book your using is old

And was using DirectX around or up to 4
They didnt have color key transparency at that time, and was introduced later. Especially when using DirectDraw

If he is using DirectX 7+ , then yes he was misleading you.

Share this post


Link to post
Share on other sites
Whackjack    100
Thanks for your input everyone. The author is using DX8, but I just think that he didn''t know about the color key transparency functions. It''s unfortunate too, because the function he built is disgustingly more complicated than it needs to be.

Share this post


Link to post
Share on other sites