Sign in to follow this  
nullsmind

Rotating a sprite?

Recommended Posts

You can actually use Direct3D or OpenGL for 2d. Not only is this a lot faster, but you also get rotation, scaling, blending, alpha, etc for free. I don't see any strong reasons to continue using DirectDraw.

Share this post


Link to post
Share on other sites
ID3DXSprite is not DirectDraw.

I believe it also supports attaching a transformation matrix

http://www.google.com/search?q=rotate+ID3DXSprite

Share this post


Link to post
Share on other sites
Ah, ID3DXSprite::SetTransform() is what I'm looking for.

HRESULT SetTransform(
CONST D3DXMATRIX * pTransform
);

Describing the matrix is confusing, though. What do these variables mean?

typedef struct D3DXMATRIX {
FLOAT _11, FLOAT _12, FLOAT _13, FLOAT _14,
FLOAT _21, FLOAT _22, FLOAT _23, FLOAT _24,
FLOAT _31, FLOAT _32, FLOAT _33, FLOAT _34,
FLOAT _41, FLOAT _42, FLOAT _43, FLOAT _44 );
} D3DXMATRIX;

Share this post


Link to post
Share on other sites
That's just a 4x4 matrix of floats. The names of the variables ( _11, _23, etc ) are just indications of the positions - _11 means location 1,1 _23 means location 2,3 - or, row 2, column 3.

I'm pretty sure there's a function in D3DX that creates a rotation matrix for you, given the angle.

Share this post


Link to post
Share on other sites
Wikipedia has a couple articles about transformation matrices. I found them quite helpful, but they might be too generic for what you're doing.

Share this post


Link to post
Share on other sites
Here is a nice function for you


bool Blit(LPD3DXSPRITE pSprite, LPDIRECT3DTEXTURE9 pTexture, float DestX, float DestY, int SrcX, int SrcY, int Width, int Height, float XScale, float YScale, float rotation, D3DCOLOR Color)
{

// make sure a valid sprite was passed
if( pSprite == NULL )
return false;

// make sure a valid texture was passed
if( m_Texture == NULL )
return false;

// create the rect that will copy from the texture
RECT Rect;
Rect.left = SrcX;
Rect.top = SrcY;
Rect.right = Rect.left + Width;
Rect.bottom = Rect.top + Height;


// create the transformation
D3DXMATRIX mat;
D3DXMatrixTransformation2D( &mat, NULL, 0.0f, &D3DXVECTOR2( XScale, YScale ), NULL, rotation, NULL );
pSprite->SetTransform(&mat);

// draw it
if(FAILED(pSprite->Draw(m_Texture, &Rect ,NULL ,&D3DXVECTOR3( DestX, DestY, 0.0f ),Color)))
return false;

return true;
}

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