Direct3D: Using colorkeying with StretchRect?

Started by
3 comments, last by Drexl 19 years, 7 months ago
I´m using StretchRect to draw the background in my game. I want to use colorkeying so I can make certain areas of the bitmap transparent. But now Im getting FRUSTRATED, I cant get the damn colorkeying to work. The thing that happens is that the color that I want transparent gets rendered BLACK. Ok, this is the parts of my code dealing with my background, can anyone spot what Im doing wrong? Please help me! //Create a surface for the background bitmap. if(FAILED(g_pDirect3DDevice->CreateOffscreenPlainSurface( 640, 480, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pBackground, NULL ))) { return(E_FAIL); } //Load the background bitmap into the surface. if(FAILED(D3DXLoadSurfaceFromFile( g_pBackground, NULL, NULL, "background.bmp", NULL, D3DX_DEFAULT, D3DCOLOR_RGBA(255,0,255,255), NULL ))) { return(E_FAIL); } //Set rendering states g_pDirect3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); g_pDirect3DDevice->SetRenderState(D3DRS_ALPHAREF, 0x01); g_pDirect3DDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); g_pDirect3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); //Render the background g_pDirect3DDevice->StretchRect(g_pBackground, &SrcRect, g_pBackBuffer, &DestRect, D3DTEXF_NONE);
Advertisement
I'm not sure if colorkeying is possible with StretchRect (I don't think it is), but SetRenderState only effects render calls not surface blitting.

I would recommend either using D3DXSprite or making your own textured quad encapsulation. You will be able to do much more just as easilly.
I am using D3DXSprite for rendering my small moving objects. I tried to use that interface to render the background, but I noticed that it couldnt handle large bitmaps (about 200*200 and larger), which resulted in distorted, stretched bitmaps on the screen. I would be forced to partition my background into 16 or more smaller bitmaps. And that is what I am forced to do if I cant get StretchRect to work, or find some other interface to help me draw my background.

I could use the DirectDraw function Blt, but isnt that an obsolete technique when you want to program 2D games nowadays?
I'm pretty sure you should be able to get the D3DXSprite stuff to draw large images correctly as well. You probably just need to examine the documentation more closely. I haven't used D3DXSprite myself, but I do know that with more manual methods of drawing sprites (which are most certainly similar to what D3DXSprite does under the hood), getting a flag or some other minor thing wrong will indeed cause problems like you described. But it's definitely very doable once you get everything right.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Believe me, I´ve looked through the documentation very closely for the past weeks, without result. It seems I´ve tried everything to get the darn thing to work properly. I too think its just a simple little flag or something that should be set correctly to get it to work. So, is there anyone how can help me with this?

This topic is closed to new replies.

Advertisement