ID3DXSprite question

This topic is 4952 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

hi, i'm trying to get a simple tile game going using directx. i've been reading "programming role-playing games with directx" by jim adams. its been pretty good, i'm using it along side online tutorials and msdn. the idea of using tiles seems pretty straightforward, so the first thing i'm trying to do is draw a sprite to the screen. in the book it uses a pointer to a ID3DXSprite, and draws it with this method: HRESULT Draw( LPDIRECT3DTEXTURE9 pSrcTexture, CONST RECT *pSrcRect, CONST D3DXVECTOR2 *pScaling, CONST D3DXVECTOR2 *pRotationCenter, FLOAT Rotation, CONST D3DVECTOR2 *pTranslation, D3DCOLOR Color ); pSrcRect is the rectangle of the area on the .bmp you want to draw, pScaling is how much you want to scale the x and y axis, rotation is if you want to rotate it, pTranslation is the x and y values of the screen position, and color is the modulation color. i tried incorporating it into my little program, and it said i was using the wrong parameters. i looked on msdn and found the draw function is like this: HRESULT Draw( LPDIRECT3DTEXTURE9 pTexture, CONST RECT *pSrcRect, CONST D3DXVECTOR3 *pCenter, CONST D3DXVECTOR3 *pPosition, D3DCOLOR Color ); pTexture represents the sprite texture. pSrcRect portion of the source texture to use for the sprite pCenter identifies the center of the sprite. If this argument is NULL, the point (0,0,0) is used, which is the upper-left corner. pPosition identifies the position of the sprite. If this argument is NULL, the point (0,0,0) is used, which is the upper-left corner. Color modulation color is this new with the most recent update to directx? the book uses directx 9.0 and was published just a few months ago. and if anybody is familliar with the new draw function, what exactly is pCenter? is it the center of where i want to draw it to the screen or is it the center of the section of the .bmp file i want to draw? and if i'm drawing in 2d do i want to set the third number in the vector to 0?

Share on other sites
Quote:
 Original post by ctaclasHRESULT Draw( LPDIRECT3DTEXTURE9 pSrcTexture, CONST RECT *pSrcRect, CONST D3DXVECTOR2 *pScaling, CONST D3DXVECTOR2 *pRotationCenter, FLOAT Rotation, CONST D3DVECTOR2 *pTranslation, D3DCOLOR Color);

This is the DirectX 8, or rather D3DX8 implementation.

Quote:
 Original post by ctaclasHRESULT Draw( LPDIRECT3DTEXTURE9 pTexture, CONST RECT *pSrcRect, CONST D3DXVECTOR3 *pCenter, CONST D3DXVECTOR3 *pPosition, D3DCOLOR Color);

This is the D3DX9 implementation.

If the book is supposed to be using DirectX 9, I assume it's just something they forgot to update if this book was originally for released for DirectX 8.

Quote:
 Original post by ctaclaswhat exactly is pCenter?

It is the center of the sprite... Used with D3DXSPRITE_BILLBOARD it orientates the sprite to face the camera using this center of rotation.

Quote:
 Original post by ctaclasif i'm drawing in 2d do i want to set the third number in the vector to 0?

Yep :)

Share on other sites
thanks, i'm able to draw my tiles to the screen now :)

now i'm trying to make transparant sections of my tile. for example, i have a tile that is white background with a solid black circle. i have another tile that is colored. i'm trying to put the black circle onto the other tile without the white background. i'm trying to play around with the alpha color but i don't seem to be doing it right.

the way to do it is alpha blending right? i've been playing with the color value that gets passed to draw. if i'm not mistaken, the fourth number has a range from 0-255 with 0 being transparant and 255 being opaque, and this fourth value is called the alpha value? it seems if i set it high then my circle and background is all opaque but if i set it too low both circle and background are not drawn.

i added the following to my function where i initialize all my graphics device related stuff:
pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, true);		pd3dDevice->SetRenderState(D3DRS_ALPHAREF, 0x08);		pd3dDevice->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

is that all i need to do?

and also, for the other parameters for the color that i pass to the draw function, do the r,g,b values define which color is transparant?

Share on other sites
in DX9, you can pass flags for Sprite before rendering.
Sprite->Begin( DWORD FLAGS );
pass D3DXSPRITE_ALPHABLEND as the flag.

Share on other sites
For drawing transparent sections of your tile, I think you mean setting a colour key.

When you load your texture (e.g. with D3DXCreateTextureFromFileEx() ) set an colorkey value.
This colour value will get transparent, when your tile is drawn.

Share on other sites
thanks guys i got the transparant color to work :)

i just got one more question on this topic though

because the color key is set in the CreateTextureFromFileEx function, all of the tiles that have been packed into the same .bmp file have to have the same color key, right?

if i had some tiles that i wanted to be completely opaque, and others transparant, do i have to make sure the opaque tile does not contain anything that has the same color of the color key? or is there another way?

i could keep them in separate .bmp files and exclude the color key from the textures i want to be opaque

i know in the ID3DXSprite::Draw function it takes as the last parameter a D3DCOLOR as the modulation color. i'm not 100% sure what exactly it does. just by playing with it it appears you can use the rgb properties to change the colors, for example i made a multicolored texture and set the color to 255,0,0,255 and it turned the colors into red and black, i assume it just turned down the greens and blues leaving only reds and blacks where there were no red to begin with. what does the alpha value of the color do? it looks to me like if i set it low enough the entire texture disappears, but if i set it high it doesn't do anything at all. i was wondering if i could make a sprite opaque even if it has pixels set to the color i use as the color key by setting the alpha value to not allow that particular sprite to have transparancy

thanks :)

Share on other sites
Quote:
 Original post by ctaclasbecause the color key is set in the CreateTextureFromFileEx function, all of the tiles that have been packed into the same .bmp file have to have the same color key, right?

Yes.

Quote:
 if i had some tiles that i wanted to be completely opaque, and others transparant, do i have to make sure the opaque tile does not contain anything that has the same color of the color key?

Pretty much. The color key applies to the whole texture.

Quote:
 or is there another way?

You can use alpha transparency. If you do this, then don't worry about the color key. It's probably a bad idea to do both.

Quote:
 i could keep them in separate .bmp files and exclude the color key from the textures i want to be opaque

Yeah, that's totally possible, but switching between texture takes longer than pulling from different regions on one texture, so it's generally better to stick to using as few textures as you can.

Quote:
 i know in the ID3DXSprite::Draw function it takes as the last parameter a D3DCOLOR as the modulation color. i'm not 100% sure what exactly it does.

It's a multiplier. The color channels in each pixel of the texture are multiplied by the corresponding color channels in the modulator. This is an easy way to do fading in and out. If you're using ARBG, the modulating by 0xFF000000 will turn that sprite invisible, and 0xFFFFFFFF will display it as it normally looks, but no brighter.

kingnosis

Share on other sites
cool, thanks, i think understand what that multiplier in the draw function is, i played with it and saw that it can filter out colors by adjusting the rgb values, and make the texture see-through, like stained glass windows by adjusting the alpha value.

theres only one part that i'm kind of fuzzy on.

the color key method that i got working, in my book its described as "transparent blitting with alpha testing", this presents a problem if i want to draw a sprite that has the color used as the color key, it has to be on another .bmp file, which creates extra overhead of loading another .bmp file.

the part where you suggested "You can use alpha transparency. If you do this, then don't worry about the color key. It's probably a bad idea to do both."

i'm not too clear what alpha transparancy is. is that related to the stained glass effect i got by playing with the alpha value passed to the draw function, or is that something else?

Share on other sites
Make the colour on the second sprite slightly different. Even just change the green value by one. This will make the colours different values, but the difference will be indistinguishable by the human eye.

Share on other sites
That being said, people usually use pure magenta as their colour key. Full red, zero green, full blue. This colour is very rarely used in actual graphics, so it is a very handy colour to use for a colour key.

• 18
• 11
• 18
• 9
• 52
• Forum Statistics

• Total Topics
631397
• Total Posts
2999800
×