Sign in to follow this  
blueshogun96

2D sprite problems

Recommended Posts

At the moment, I'm trying to write a replacement for the ID3DXSprite interface. The reason is because Xbox360 doesn't support this interface and I'm attempting to port existing code that currently uses it.

I've made some progress getting D3DXMatrixOrthoLH to work (finally) and I've managed to successfully blit a full screen 2D image. However, when blitting smaller images and sprite sheets, things don't work properly anymore. I've put quite a bit of work into getting this working so far, here's my code:

[CODE]
void bltSprite( LPDIRECT3DTEXTURE9 tex, RECT* src, D3DXVECTOR3* dst )
{
D3DXMATRIX mtxProj, mtxWorld, mtxView;
struct BLTVERTEX v[4];
float w = src->top - src->right;
float h = src->left - src->bottom;
float fx, fy;
ZeroMemory( v, sizeof( struct BLTVERTEX ) * 4 );
fx = src->right - (ScreenX/2.0f);
fy = src->bottom - ((ScreenY/2.0f)-h);
v[0].v.x = dst->x - 0.5f;
v[0].v.y = -dst->y - 0.5f;
v[1].v.x = dst->x+w - 0.5f;
v[1].v.y = -dst->y - 0.5f;
v[2].v.x = dst->x+w - 0.5f;
v[2].v.y = -dst->y+h - 0.5f;
v[3].v.x = dst->x - 0.5f;
v[3].v.y = -dst->y+h - 0.5f;
v[0].t.x = src->top / w;
v[0].t.y = 1.0f - src->left / h;
v[1].t.x = src->right / w;
v[1].t.y = 1.0f - src->left / h;
v[2].t.x = src->right / w;
v[2].t.y = 1.0f - src->bottom / h;
v[3].t.x = src->top / w;
v[3].t.y = 1.0f - src->bottom / h;
D3DXMatrixTranslation( &mtxWorld, fx, -fy, 0.0f );
D3DXMatrixIdentity( &mtxView );
IDirect3DDevice9_SetTransform( lpD3DDevice, D3DTS_WORLD, &mtxWorld );
IDirect3DDevice9_SetTransform( lpD3DDevice, D3DTS_VIEW, &mtxView );
IDirect3DDevice9_SetFVF( lpD3DDevice, D3DFVF_XYZ | D3DFVF_TEX1 );
IDirect3DDevice9_SetTexture( lpD3DDevice, 0, (IDirect3DBaseTexture9*) tex );
IDirect3DDevice9_DrawPrimitiveUP( lpD3DDevice, D3DPT_TRIANGLEFAN, 2, v, sizeof( struct BLTVERTEX ) );
}
[/CODE]


I apologize for the usage of C (for the C haters), but the project was originally written in C. I can't quite figure out what's going on here.

Examples of what works and what doesn't:

This works:
[CODE]
RECT src = { 0, 0, 640, 480 };
D3DXVECTOR3 dst = { 0, 0, 0 };
bltSprite( lpSplash, &src, &dst );
[/CODE]
[img]http://i48.tinypic.com/2jg4gp2.png[/img]

This doesn't:
[CODE]
RECT src = { 50, 50, 600, 440 };
D3DXVECTOR3 dst = { 0, 0, 0 };
bltSprite( lpSplash, &src, &dst );
[/CODE]
[img]http://i49.tinypic.com/2en8085.png[/img]

Moving the sprites about doesn't seem to work either in some instances. Not sure what's going on here. This sucks, so any help is appreciated. Thanks.

Shogun Edited by blueshogun96

Share this post


Link to post
Share on other sites
[source lang="cpp"]float w = src->top - src->right;
float h = src->left - src->bottom;
[/source]
Shouldn't this be w=right-left and h=top-bottom ?

Share this post


Link to post
Share on other sites
Just in case anyone cares, I fixed it the other day. This works perfectly.

[CODE]
void bltSprite( LPDIRECT3DTEXTURE9 tex, RECT* src, D3DXVECTOR3* dst )
{
D3DXMATRIX mtxWorld, mtxView, mtxWVP;
D3DSURFACE_DESC sd;
struct BLTVERTEX v[4];
float w = -(src->left - src->right);
float h = -(src->top - src->bottom);
float tw, th;
float fx, fy;
float u1, u2, v1, v2;
ZeroMemory( v, sizeof( struct BLTVERTEX ) * 4 );
fx = dst->x - (ScreenX/2.0f);
fy = dst->y - ((ScreenY/2.0f)-h);
IDirect3DTexture9_GetLevelDesc( tex, 0, &sd );
tw = sd.Width;
th = sd.Height;
u1 = src->left / tw;
u2 = src->right / tw;
v1 = src->top / th;
v2 = src->bottom / th;
v[0].v.x = 0.5f;
v[0].v.y = 0.5f;
v[1].v.x = w + 0.5f;
v[1].v.y = 0.5f;
v[2].v.x = w + 0.5f;
v[2].v.y = h + 0.5f;
v[3].v.x = 0.5f;
v[3].v.y = h + 0.5f;
v[0].t.x = u1;
v[0].t.y = v2;
v[1].t.x = u2;
v[1].t.y = v2;
v[2].t.x = u2;
v[2].t.y = v1;
v[3].t.x = u1;
v[3].t.y = v1;
D3DXMatrixTranslation( &mtxWorld, fx, -fy, 0.0f );
D3DXMatrixIdentity( &mtxView );
#if 0
IDirect3DDevice9_SetTransform( lpD3DDevice, D3DTS_WORLD, &mtxWorld );
IDirect3DDevice9_SetTransform( lpD3DDevice, D3DTS_VIEW, &mtxView );
IDirect3DDevice9_SetFVF( lpD3DDevice, D3DFVF_XYZ | D3DFVF_TEX1 );
#else
D3DXMatrixMultiply( &mtxWVP, &mtxWorld, &mtxView );
D3DXMatrixMultiply( &mtxWVP, &mtxWVP, &mtxProj );
IDirect3DDevice9_SetVertexShaderConstantF( lpD3DDevice, 0, (float*)&mtxWVP, 4 );
IDirect3DDevice9_SetVertexDeclaration( lpD3DDevice, lpVDecl );
IDirect3DDevice9_SetVertexShader( lpD3DDevice, lpVS );
IDirect3DDevice9_SetPixelShader( lpD3DDevice, lpPS );
#endif
IDirect3DDevice9_SetTexture( lpD3DDevice, 0, (IDirect3DBaseTexture9*) tex );
IDirect3DDevice9_DrawPrimitiveUP( lpD3DDevice, D3DPT_TRIANGLEFAN, 2, v, sizeof( struct BLTVERTEX ) );
}
[/CODE]

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