Sign in to follow this  


Recommended Posts

I am trying to use Sprite.Draw to draw a user interface for a program I am working on. I am running into an issue with transparency in the sprite. I am using a texture that has an alpha channel and it works correctly except that I get a white line around the entire sprite. I have tried passing Color.Transparent as the last parameter but this causes the entire sprite to disappear. According to the documentation, this should cause the original color and alpha values to come through, but instead I have been using Color.White. Does anyone know how I can remove this border from around the sprite or a better way to render an interface for a 3d game? Thanks

Share this post

Link to post
Share on other sites
Does the image have a white background? If so, you might want to check the alpha channel to see that it is lined up with the image properly.

To ensure the alpha is working you might want to just make it a small circle and then check that only a small circle of the image is rendered.

Share this post

Link to post
Share on other sites
I think I can help with this one. I ran into the same problem with my sprites (only im rendering them using objectspace instead of screenspace.
First make sure you have your transparent color rgb values.
Then when you load the texture use something like this (assuming d3d9):

LPDIRECT3DTEXTURE9 txtr; // d3d loaded texture
std::string filename = "texture.bmp"; // or whatever your texture filename is

if (FAILED(D3DXCreateTextureFromFileEx(d3dDevice, filename.c_str(),
D3DCOLOR_ARGB(255,255,0,255), // transparency color
NULL, NULL, &txtr)))
// failed to load the texture. return from load function, report error, etc

This will create the texture file in memory and replace your transparency color (pink in this case) with a black color with 0 alpha value. When you setup your device you need to enable alpha blending like so :

// any pixel on a texture that has an alpha value >= 1 will be rendered
d3dDevice->SetRenderState(D3DRS_ALPHAREF, (DWORD)0x00000001);
d3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);

this enables alpha testing during blitting/texturing. I had to do this manually even tho in the documentation the D3DXSprite::Begin() function is suppose to do it for you (but for whatever reason doesnt) when you pass D3DXSPRITE_ALPHABLEND as a parameter. Doing this you still use


to start your drawing. Im assuming youve already created the sprite interface using your d3dDevice object. when you render your image use the following

txtr, // the texture image your blitting
pSrcRect, // source rectangle pointer of the section of image to draw (null uses entire image)
pCenter, // pointer to D3DXVECTOR3 struct that is the center of image (null is upper left)
pPosition, pointer to D3DXVECTOR3 struct that is the position of sprite on screen (null is upper left or 0,0)
Color // color manipulation value NOT alpha color use D3DCOLOR_RGBA(255, 255, 255, 255) to keep the image the same color (or adjust this color to get some cool color effects)

thats it, your sprite will be drawn at pPosition with transparency color you specified when you loaded the texture being clear. make sure you D3DXSprite->End() when done drawing.

hope that helps.

Share this post

Link to post
Share on other sites
Another thing, if your having problems with the pCenter and pPosition values you can always pass NULL for both during the Draw() call and instead use

D3DXVECTOR2 vPosition = D3DXVECTOR2(x, y); // x and y screen coords
D3DXMATRIX mMat; // transformation matrix
&mMat, // address of the output matrix (ie return value)
Scale, // float value of scale to use (1.0f keeps same image size)
pRotationCenter, // center of rotation. D3DXVECTOR2 varibable, null uses identity matrix
Rotation, // float value of rotation in radians (degrees*(D3DX_PI/180)) 0 for no rotation
&vPosition // pointer to D3DXVECTOR2 struct for translation of image

D3DXSprite->SetTransform(&mMat); // set the transform for the image(s) about to be drawn
D3DXSprite->Draw(....); //draw the sprite

if you use this method make sure you use NULL for the center and position params in the Draw() function call.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this