Reputation: 23485
How can I get Direct-X to draw every colour except for black? Currently I use the following code for loading and drawing a texture:
void LoadTexture(IDirect3DDevice9* Device, unsigned char* buffer, int width, int height, IDirect3DTexture9* &Texture, ID3DXSprite* &Sprite)
{
Device->CreateTexture(width, height, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &Texture, 0);
D3DXCreateSprite(Device, &Sprite);
D3DLOCKED_RECT rect;
Texture->LockRect(0, &rect, nullptr, D3DLOCK_DISCARD);
TexturePixels = static_cast<unsigned char*>(rect.pBits);
Texture->UnlockRect(0);
memcpy(TexturePixels, &buffer[0], width * height * 4);
}
void DrawTextureSprite(IDirect3DDevice9* Device, IDirect3DTexture9* Texture, ID3DXSprite* Sprite)
{
if (Sprite && Texture)
{
D3DXVECTOR3 Position = D3DXVECTOR3(0, 0, 0);
Sprite->Begin(D3DXSPRITE_ALPHABLEND);
Sprite->Draw(Texture, nullptr, nullptr, &Position, D3DCOLOR_RGBA(0xFF, 0xFF, 0xFF, 0xFF)); //0xFFFFFFFF - white..
Sprite->End();
}
}
void BltBuffer(IDirect3DDevice9* Device)
{
if (Bitmap != nullptr)
{
std::uint8_t* Ptr = Bitmap->pixels;
for (int I = 0; I < Bitmap->height; ++I)
{
for (int J = 0; J < Bitmap->width; ++J)
{
std::uint8_t B = *(Ptr++);
std::uint8_t G = *(Ptr++);
std::uint8_t R = *(Ptr++);
*(Ptr++) = (B == 0 && G == 0 && R == 0) ? 0 : 0xFF; //if the colour is black, set the alpha channel to 0x0.
}
}
//if (!Texture && !TexturePixels)
{
LoadTexture(Device, Bitmap->pixels, Bitmap->width, Bitmap->height, Texture, Sprite);
}
DrawTextureSprite(Device, Texture, Sprite);
SafeRelease(Texture);
SafeRelease(Sprite);
}
}
Every frame I call BlitBuffer
and I pass it my device. However, when it draws, it draws my black pixels even though I set their alpha channel to 0. All other pixels have their alpha channels set to 255.
Any ideas how I can make it NOT draw black pixels?
Upvotes: 1
Views: 205
Reputation: 20764
you have to enable and setup the blending mode as well
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3ddev->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
Upvotes: 1