Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


2D sprite problems


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 blueshogun96   Crossbones+   -  Reputation: 1052

Like
0Likes
Like

Posted 01 August 2012 - 01:06 AM

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:

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 ) );
}


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:
RECT src = { 0, 0, 640, 480 };
D3DXVECTOR3 dst = { 0, 0, 0 };
bltSprite( lpSplash, &src, &dst );
Posted Image

This doesn't:
RECT src = { 50, 50, 600, 440 };
D3DXVECTOR3 dst = { 0, 0, 0 };
bltSprite( lpSplash, &src, &dst );
Posted Image

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, 01 August 2012 - 01:19 AM.

Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7859

Like
0Likes
Like

Posted 01 August 2012 - 01:24 AM

[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 ?

#3 blueshogun96   Crossbones+   -  Reputation: 1052

Like
0Likes
Like

Posted 01 August 2012 - 01:48 AM

Tried it, makes things worse because then nothing shows at all...

Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


#4 blueshogun96   Crossbones+   -  Reputation: 1052

Like
0Likes
Like

Posted 03 August 2012 - 03:50 PM

Just in case anyone cares, I fixed it the other day. This works perfectly.

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 ) );
}

Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS