Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

sprite sheet

I try to blend the character into game but I still cannot remove the blue color in the sprite sheet and discover that the white area of sprite is semi-transparent.

Before that, the color D3DCOLOR_XRGB(255, 255, 255) is set in D3DXCreateTextureFromFileEx. You will see the fireball through the sprite.

transparent

After I change the color to D3DCOLOR_XRGB(0, 255, 255), the result will be

no transparent

Now, I am trying to remove the blue color of the sprite sheet and my expected result is something like that

expected result

Until now, I still cannot figure out how to do that. Any ideas?

void initPlayer()
{
    //  Create texture.
    hr = D3DXCreateTextureFromFileEx(d3dDevice, "player.png", 169, 44, 
         D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, 
         D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(0, 255, 255), 
         NULL, NULL, &player);
}

void renderPlayer()
{
    sprite->Draw(player, &playerRect, NULL, &D3DXVECTOR3(playerDest.X, playerDest.Y, 0),D3DCOLOR_XRGB(255, 255, 255));
}

void initFireball()
{
    hr = D3DXCreateTextureFromFileEx(d3dDevice, "fireball.png", 512, 512,
         D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED,
         D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255),
         NULL, NULL, &fireball);
}

void renderFireball()
{
    sprite->Draw(fireball, &fireballRect, NULL, &D3DXVECTOR3(fireballDest.X, fireballDest.Y, 0), D3DCOLOR_XRGB(255,255, 255));
}
share|improve this question

2 Answers 2

up vote 1 down vote accepted

You have four options. First, edit the image and make the background transparent. This the "most correct" solution long-term.

Second, transform the background color on load before creating the texture. Loop over all pixels and set them to 0 alpha for the specified color. You may to alter the image format depending on how it's loaded.

Third, do this at runtime on the GPU. Load the image, bind a render target of the same size, then use a shader which alters the source pixels matching the background color. May or may not be faster than using the CPU.

Finally, worst option, just do this "on demand" with a shader and don't bother storing the results.

share|improve this answer
    
I already successfully implement a solution. Is this the second solution u mention? // Create texture. hr = D3DXCreateTextureFromFileEx(d3dDevice, "player.png", 169, 44, D3DX_DEFAULT, NULL, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_XRGB(153, 255, 255), NULL, NULL, &player); // Specify alpha blend will ensure that the sprite will render the background with alpha. sprite->Begin(D3DXSPRITE_ALPHABLEND); –  optimisez Jun 30 '13 at 5:47
    
Yes, that's my second solution listed. I didn't know D3DXCreateTextureFromFileEx could do that, but that's easier than doing it manually for sure. –  Sean Middleditch Jun 30 '13 at 6:52
    
Btw I have interest to know how to implement the third and last solution. –  optimisez Jun 30 '13 at 8:02
    
You should start by learning shaders and the programmable pipeline, which is too big of a topic to tack on here. After you've got that down, if the answer doesn't become obvious, ask a new question. –  Sean Middleditch Jun 30 '13 at 20:43

It has been a long time since I have used DirectX 9, but here is how I see it. You're going to want to, first:

  1. Use an image editor of your choice to remove the blue section of the sprite directly so that it is transparent. This should be achievable with just about any program. Make sure you save as .png or another image format supporting transparency.

  2. Use an image viewer to check that the sprite is now really transparent (windows default program will do).

  3. Use a blend state of some sort in your rendering code to ensure that your alpha is not ignored.

For blend states, you might want to look into:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb172252(v=vs.85).aspx

On a cursory look I think D3DBLEND_SRCCOLOR might be what you're looking for.

share|improve this answer
    
D3DBLEND_SRCCOLOR is definitely not what he wants. –  Sean Middleditch Jun 30 '13 at 3:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.