Sign in to follow this  
xegoth

ID3DXSPRITE needs to work...

Recommended Posts

Okay this is the most stumped I've been in a LONG time. I want to draw sprites in screen space. I am currently drawing them in world space. Whenever I try to draw in screen space my sprite doesn't appear and draw returns the error: D3DERR_INVALIDCALL
HRESULT DXEngine::BltSprite( D3DXMATRIX matWorld, LPDIRECT3DTEXTURE9 pSrcTexture, 
          RECT *pSrcRect, DWORD dwFlags )
{
	
	D3DXVECTOR3 trans(0,0,0), rcenter(0,0,0);

	 // Define the center of the texture. For rotation, and drawing at the center.
	 if(pSrcRect) {
		trans.x -= rcenter.x = ((float)(pSrcRect->right - pSrcRect->left) / 2);
		trans.y -= rcenter.y =((float)(pSrcRect->bottom - pSrcRect->top) / 2);
	 }

	 m_pd3dxSprite->SetTransform( &matWorld );     
	 m_pd3dxSprite->Begin( D3DXSPRITE_OBJECTSPACE );        
	 
	 m_pd3dxSprite->Draw(m_pTexShip, pSrcRect, &rcenter, NULL, 0xFFFFFFFF );        
	 m_pd3dxSprite->End();

	 return S_OK;

}

Basically if I dont call begin with D3DXSPRITE_OBJECTSPACE I get the error. Please someone help me. I'm totally stuck. Can anyone give me a function that draws a sprite in screen space using ID3DXSPRITE and the latest version of DX? Or know why mine doesn't work whenever I don't use object space? I'm killing myself over this. Thanks in advance. If you post an example of drawing a sprite in screen space please keep it as simple as possible, thank you.

Share this post


Link to post
Share on other sites
You dont need 3D coords if you are just going to use screen space. The position is passed in a D3DXVECTOR2, not 3.

ID3DXSprite *pSprite;

pSprite->Draw(pTexInterface, //IDirect3DTexture8
&src, //source RECT
&rescale, //scaling (D3DVECTOR2)
NULL, //center (D3DVECTOR2)
0, //rotation, in radians
&trans, //translation, (D3DVECTOR)
D3DCOLOR_XRGB(255, 255, 255));//transparent color

hope it helps

-Jason

Share this post


Link to post
Share on other sites
Quote:
Original post by nosajghoul
You dont need 3D coords if you are just going to use screen space. The position is passed in a D3DXVECTOR2, not 3.

ID3DXSprite *pSprite;

pSprite->Draw(pTexInterface, //IDirect3DTexture8
&src, //source RECT
&rescale, //scaling (D3DVECTOR2)
NULL, //center (D3DVECTOR2)
0, //rotation, in radians
&trans, //translation, (D3DVECTOR)
D3DCOLOR_XRGB(255, 255, 255));//transparent color

hope it helps

-Jason


I get : error C2660: 'Draw' : function does not take 7 parameters

Are you using the most recent version of DirectX? (9.0c or whatever). If not, does anyone have a working example of ID3DXSPRITE in screenspace that uses the latest version?

Heres the definition of Draw:

Quote:

ID3DXSprite::Draw Method

--------------------------------------------------------------------------------

Adds a sprite to the list of batched sprites.

Syntax

HRESULT Draw( LPDIRECT3DTEXTURE9 pTexture,
CONST RECT *pSrcRect,
CONST D3DXVECTOR3 *pCenter,
CONST D3DXVECTOR3 *pPosition,
D3DCOLOR Color
);
Parameters

pTexture
[in] Pointer to an IDirect3DTexture9 interface that represents the sprite texture.
pSrcRect
[in] Pointer to a RECT structure that indicates the portion of the source texture to use for the sprite. If this parameter is NULL, then the entire source image is used for the sprite.
pCenter
[in] Pointer to a D3DXVECTOR3 vector that 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
[in] Pointer to a D3DXVECTOR3 vector that 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
[in] D3DCOLOR type. The color and alpha channels are modulated by this value. A value of 0xFFFFFFFF maintains the original source color and alpha data. Use the D3DCOLOR_RGBA macro to help generate this color.
Return Value


If the method succeeds, the return value is S_OK.

If the method fails, the return value can be one of the following values.


D3DERR_INVALIDCALL The method call is invalid. For example, a method's parameter may have an invalid value.
D3DXERR_INVALIDDATA The data is invalid.


How do I use it in screen space?

Share this post


Link to post
Share on other sites
Solved my own problem here's working D3DXSPRITE in screen space:


//----------------------------------------------------------------------------
// Draw2d() : A replacement of the DirectDraw Blt() function for Direct3D9
// extensively rewritten to work in 3d space
//----------------------------------------------------------------------------
HRESULT DXEngine::Draw2d(LPDIRECT3DTEXTURE9 pSrcTexture,
RECT *pSrcRect, D3DXVECTOR3 trans, float scale, DWORD dwFlags )
{

D3DXVECTOR3 rcenter(0,0,0);

// Define the center of the texture. For rotation, and drawing at the center.
if(pSrcRect) {
trans.x -= rcenter.x = ((float)(pSrcRect->right - pSrcRect->left) / 2);
trans.y -= rcenter.y =((float)(pSrcRect->bottom - pSrcRect->top) / 2);
}

D3DXMATRIX mat;

D3DXMatrixAffineTransformation(&mat, scale, NULL, NULL, &trans);

m_pd3dxSprite->SetTransform(&mat);

m_pd3dxSprite->Begin(NULL);

HRESULT hr;

hr = m_pd3dxSprite->Draw(pSrcTexture, pSrcRect, &rcenter, NULL, 0xFFFFFFFF );
m_pd3dxSprite->End();

if(hr == D3DXERR_INVALIDDATA || hr == D3DERR_INVALIDCALL)
PostQuitMessage(0);

return S_OK;


}

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