ID3DXSprite is good... and look at this nice goody by AGPX
...
Initialize with:
ID3DXSprite *spriteDrawer = NULL;
D3DXCreateSprite(pd3dDevice, &spriteDrawer);
Load your sprite with:
IDirect3DTexture9 *sprite = NULL;
D3DXCreateTextureFromFileEx(pd3dDevice,
L"c:\\MyWellSprite.png",
D3DX_DEFAULT_NONPOW2,
D3DX_DEFAULT_NONPOW2,
1,
0,
D3DFMT_UNKNOWN,
D3DPOOL_MANAGED,
D3DX_DEFAULT,
D3DX_DEFAULT,
0x00FF00FF,
NULL,
NULL,
&sprite);
All the pixels of color (255, 0, 255) are trasparent (not drawed).
And finally use the following to draw:
void DrawSprite(float x, float y, float sx, float sy, float angle, unsigned char alpha, LPDIRECT3DTEXTURE9 sprite, RECT *rc, IDirect3DDevice9* pd3dDevice)
{
D3DSURFACE_DESC desc;
sprite->GetLevelDesc(0, &desc);
const int halfWidth = desc.Width >> 1;
const int halfHeight = desc.Height >> 1;
D3DXMATRIX rotate, trasl, scale, result;
D3DXMatrixIdentity(&result);
D3DXMatrixTranslation(&trasl, -halfWidth, -halfHeight, 0);
D3DXMatrixMultiply(&result, &result, &trasl);
D3DXMatrixScaling(&scale, sx, sy, 1.0f);
D3DXMatrixMultiply(&result, &result, &scale);
D3DXMatrixRotationZ(&rotate, D3DXToRadian(angle));
D3DXMatrixMultiply(&result, &result, &rotate);
D3DXMatrixTranslation(&trasl, +halfWidth, +halfHeight, 0);
D3DXMatrixMultiply(&result, &result, &trasl);
D3DXMatrixTranslation(&trasl, x, y, 0);
D3DXMatrixMultiply(&result, &result, &trasl);
spriteDrawer->SetTransform(&result);
spriteDrawer->Draw(sprite, rc, NULL, NULL, (0xFFFFFF) | (alpha << 24));
}
Not the best code, but works. Optimization is an exercise for you ![]()
Basically DrawSprite draw sprite in the position (x,y) with scaling
factor (sx,sy), with rotation of angle-degrees and with
trasparency of "alpha" (0xFF = fully opaque). You can also specify a subrect to draw (rc).
Usage example:
// Clear the render target and the zbuffer
pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);
// Render the scene
pd3dDevice->BeginScene();
spriteDrawer->Begin(D3DXSPRITE_ALPHABLEND);
DrawSprite(x, y, 1.00f, 1.00f, 0, 255, sprite, NULL, pd3dDevice);
spriteDrawer->End();
pd3dDevice->EndScene();
To draw a mirrored sprite use negative scaling factor (es.: sx = -1.0).
Happy coding,
AGPX