Help! something about transparency
I am using directx9 to do a little 2d program.
Much like the way in direct draw, i use "CreateOffscreenPlainSurface()" to create an offsceen surface, then call "D3DXLoadSurfaceFromFile()" to load a map into this offscreen surface. after copy some curtain part from this surface to backbuffer by calling "StretchRect()", the "Present()" is used to draw the backbuffer.
so far, these worked well. but these is color in the map i want it to be transparent, like pink : RGB(255,0,255). so i set the "Colorkey" param in function "D3DXLoadSurfaceFromFile()" to value 0x00FF00FF, then call some "SetRenderState" function to set alpha blending before the "StretchRect()" copy contents from offsurface to backbuffer(detail code is attached below). And it dosen't work, the pink is still there.:(
maybe i didn't use these function right, can someone check my code, and tell me what should i do to make it work?
any help would be greatly appreciated.....:)
init and load the map file
Code:
myPainter.InitDirect3D(hwnd, clientRect.right, clientRect.bottom);
myPainter.CreateOffScrSurfaceFromFile("res\\chess.bmp", 0x00ff00ff);
code in msg loop
Code:
if(myPainter.BeginRender())
{
myPainter.m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
myPainter.m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
myPainter.m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
// also tried these way
//myPainter.m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
//myPainter.m_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_INVSRCCOLOR);
//myPainter.m_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_SRCCOLOR);
//myPainter.m_pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);
//myPainter.m_pD3DDevice->SetRenderState(D3DRS_ALPHAREF, 0x01);
//myPainter.m_pD3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);
myPainter.Render(chessman1);//chessman has the source rect and dest rect in it.
myPainter.Render(chessman2);
//myPainter.m_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
//myPainter.m_pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
myPainter.EndRender();
}
code of myPainter definition
Code:
bool KD3D9Painter::InitDirect3D(HWND hWnd, UINT nWidth, UINT nHeight)
{
Clean();//for safety
//create d3d9 object
if((m_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return false;
//set d3d presentation parameters struct
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = BACKBUF_COUNT;
d3dpp.BackBufferHeight = nHeight;
d3dpp.BackBufferWidth = nWidth;
d3dpp.hDeviceWindow = hWnd;
//create d3d device here
if(FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_pD3DDevice)))
return false;
return true;
}
bool KD3D9Painter::CreateOffScrSurfaceFromFile(LPCTSTR pFile, D3DCOLOR ColorKey)
{
if(m_pD3DDevice == NULL)
return false;
D3DXIMAGE_INFO imageInfo;
if(FAILED(D3DXGetImageInfoFromFile(pFile, &imageInfo)))
return false;
if(FAILED(m_pD3DDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pOffScrSurface, NULL)))
return false;
if(FAILED(D3DXLoadSurfaceFromFile(m_pOffScrSurface, NULL, NULL, pFile, NULL, D3DX_DEFAULT, ColorKey, NULL)))
return false;
return true;
}
bool KD3D9Painter::BeginRender()
{
if(m_pD3DDevice == NULL)
return true;
if(FAILED(m_pD3DDevice->BeginScene()))
return false;
if(FAILED(m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(255, 0, 0, 255), 1.0f, 0)))
return false;
if(FAILED(m_pD3DDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &m_pBackbuf)))
return false;
return true;
}
void KD3D9Painter::Render(const KSprite& kSpr)
{
if(m_pD3DDevice == NULL)
return;
m_pD3DDevice->StretchRect(m_pOffScrSurface, &(kSpr.srcRect), m_pBackbuf, &(kSpr.pos), D3DTEXF_NONE);
}
bool KD3D9Painter::EndRender()
{
if(m_pD3DDevice == NULL)
return false;
if(FAILED(m_pD3DDevice->EndScene()))
return false;
if(FAILED(m_pD3DDevice->Present(NULL, NULL, NULL, NULL)))
return false;
return true;
}