Sign in to follow this  
farhanx

ID3DXSprite is not working?

Recommended Posts

I am new doing 2d work on directX8 so need some guide from you ppls... here is my code i dont know what is missing , just its not showing anything on the screen accept my purple screen D3DXCreateTextureFromFile(dDevice,"pics.bmp",&texture); D3DXCreateSprite(dDevice,&pSprite); RECT SrcRect; SrcRect.bottom=200; SrcRect.left=0; SrcRect.right=300; SrcRect.top=400; dDevice->SetRenderState(D3DRS_LIGHTING,FALSE); while(TRUE) { dDevice->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,55),0.0f,0); dDevice->BeginScene(); dDevice->SetTexture(0,texture); pSprite->Draw(texture,&SrcRect, &D3DXVECTOR2(11.0f,33.0f),NULL,0.0f, &D3DXVECTOR2(1.0f,2.0f),D3DCOLOR_RGBA(255,255,255,127)); dDevice->EndScene(); dDevice->Present(0,0,0,0); ................ I have changes source rect values , scale values, Axis values butt nothing works?? help....

Share this post


Link to post
Share on other sites
Try calling pSprite->Begin() just before you call pSprite->Draw().
Call pSprite->End() just after you draw the sprite.

Use appropriate flags when calling pSprite->Begin().For instance if you want alpha blending specify D3DXSPRITE_ALPHABLEND.

Also, it's best to get into the habit of checking your HRESULTS.
In this particular case make sure you check the HRESULT from your D3DXCreateTextureFromFile call.

HTH,
Cambo_frog

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeyO
Is there a way to scale sprites? I Haven't been able to.. they just seem to be as large as the texture you use (32x32 or whatever)


Have you tried using the D3DXSPRITE_OBJECTSPACE flag when you call ID3DXSprite::Begin()

Here is a quote from the SDK Docs regarding this flag
Quote:

The world, view, and projection transforms are not modified. The transforms currently set to the device are used to transform the sprites when the batched sprites are drawn (when ID3DXSprite::Flush or ID3DXSprite::End is called). If this flag is not specified, then world, view, and projection transforms are modified so that sprites are drawn in screen-space coordinates.


HTH,
Cambo_frog

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeyO
Well, what if you do want to draw the sprites in screen space?


If you use the D3DXSPRITE_OBJECTSPACE flag, you would probably have to set your view and projection matrices to map your coordinates to screen space prior to calling ID3DXSprite::Begin().

Maybe you could avoid this by using the ID3DXSprite::SetTransform() method, but I am not sure how this method works, as The SDK documentation is very limited regarding this.For instance I am not sure if you have to call this method between ID3DXSprite::Begin() and ID3DXSprite::End().

Which method(s) have you tried to scale the sprite so far?

TIA,
Dave

Share this post


Link to post
Share on other sites
Only transform.

If you change the scale of the rectangle during the draw method, it will change the size of the rectangle, but not the size of the texture drawn within the rectangle.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeyO
Only transform.

If you change the scale of the rectangle during the draw method, it will change the size of the rectangle, but not the size of the texture drawn within the rectangle.


I am sure we can get to the bottom of this eventually :)
Could you please give me some more info?

1) When you say "Only transform", I assume you mean ID3DXSprite::SetTransform(), is this correct?

2) Assuming you are using ID3DXSprite::SetTransform(), do you call this method between ID3DXSprite::Begin() and ID3DXSprite::End() or before ID3DXSprite::Begin(), or have you tried both?

3) Could you post your code?

Maybe the only way is to use the D3DXSPRITE_OBJECTSPACE flag in the call to ID3DXSprite::Begin() and set the view and projection matrices to map your coordinates to screen space prior to calling ID3DXSprite::Begin()?

I can post code on how to set up your view and projection matrices to map to screen space, but it is probably best if you could give me the answers to questions 1 to 3 first.

TIA,
Cambo_frog


Share this post


Link to post
Share on other sites
1) Yes.. but not SetTransform(), there is a read/set Transform property

2) Im doing transform after Begin() and before Draw()

3) (I'm using C#/MDX)
flareTest.Begin(SpriteFlags.SortTexture | SpriteFlags.ObjectSpace);

Sprite.Transform = Matrix.Scaling(0.02f, 0.02f, 0.02f);

Sprite.Draw(Texture, new Rectangle(0, 0, 32, 32), new Vector3(16, 16, 0), new Vector3(0, 0, 0), Color.Gray);

flareTest.End();

So, to 'Add a sprite to the list of batched sprites.', I need to call Draw(), but for each sprite do I need to do a Begin() and End()? If each End() call 'Restores the device to the state it was in before', wouldnt doing Begin() and End() for each sprite be inefficient?

Share this post


Link to post
Share on other sites
OK Mikey,

I have never used Managed DirectX or .Net languages in any way ( I use C++ for DirectX apps ) so that makes it a bit more difficult.

Quote:

1) Yes.. but not SetTransform(), there is a read/set Transform property


This definately sounds like the managed equivalent of the C++ function ID3DXSprite::SetTransform(). Try setting this property before you call the C# equivalent of ID3DXSprite::Begin(). I am not saying it will work, but I am thinking it is best to explore all possibilities.

Quote:

3) (I'm using C#/MDX)
flareTest.Begin(SpriteFlags.SortTexture | SpriteFlags.ObjectSpace);

Sprite.Transform = Matrix.Scaling(0.02f, 0.02f, 0.02f);

Sprite.Draw(Texture, new Rectangle(0, 0, 32, 32), new Vector3(16, 16, 0), new Vector3(0, 0, 0), Color.Gray);

flareTest.End();


These lines look OK, but I can't be sure because I don't use C#

Quote:

So, to 'Add a sprite to the list of batched sprites.', I need to call Draw(), but for each sprite do I need to do a Begin() and End()? If each End() call 'Restores the device to the state it was in before', wouldnt doing Begin() and End() for each sprite be inefficient?


As I read it, you can draw the same sprite object several times between Begin and End with different state/properties/textures.

If you use several different sprite objects, you will need to call Begin and End for each sprite object.

I will try and write a test app tomorrow to solve the problem.
If I find solution I will post the code in C++ and hopefully someone can convert to C# for you.

If I don't find a solution, I will let you know anyway.

HTH,
Cambo_frog

Share this post


Link to post
Share on other sites
Yeah, nevermind about the begin() end() stuff, I'm an idiot:)

C# and C++ aren't so different, I can interpret the C++ and figure out the C# pretty easily. Anyway, dont worry about the C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeyO
Yeah, nevermind about the begin() end() stuff, I'm an idiot:)

C# and C++ aren't so different, I can interpret the C++ and figure out the C# pretty easily. Anyway, dont worry about the C++.


Ok I just tried creating a scale transform:

D3DXMATRIX spriteScaleMatrix;
D3DXMatrixScaling( &spriteScaleMatrix, 0.02f, 0.02f, 0.02f );

Then setting the sprite transform:
pSprite->SetTransform( &spriteScaleMatrix );

works for me setting the transform between Begin and End or before Begin, both work.

note If you use the scaling you specified (0.02f) which would make your texture 1/50th of its original size, and if the size of the texture was say 32 X 32 as in your original example, your sprite would be displayed as a single pixel (or less!)

HTH,
Cambo_frog

[Edited by - Cambo_frog on November 8, 2004 6:59:50 PM]

Share this post


Link to post
Share on other sites
Actually, when I was rendering it like that without the scaling it was taking up more than the whole screen (probably too close to the camera), so 0.02 made it so you could actually see the whole thing..

Do I need to set the worldspace/billboard for scaling?

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeyO
Actually, when I was rendering it like that without the scaling it was taking up more than the whole screen (probably too close to the camera), so 0.02 made it so you could actually see the whole thing..


I don't think the distance from the camera would make any difference so the scaling of the sprite when rendering in screen space.

When you specified your rectangle:
new Rectangle(0, 0, 32, 32),

This would use the 32 x 32 pixel block from the top left corner of the texture, which should easily fit the screen without any scaling.

Quote:

Do I need to set the worldspace/billboard for scaling?


No, I just used the C++ equivalant of the flags you used:
D3DXSPRITE_ALPHABLEND|D3DXSPRITE_SORT_TEXTURE

It also works for me with flags set to 0.

**** Edit ******

Also try using the position parameter of the Draw function instead of the center parameter to position the sprite.

When you use:

Sprite.Draw(Texture, new Rectangle(0, 0, 32, 32), new Vector3(16, 16, 0), new Vector3(0, 0, 0), Color.Gray);

Are you trying to make the sprite entirely visible in the top left corner of the screen?

If so, then it would not work. Try:

Sprite.Draw(Texture, new Rectangle(0, 0, 32, 32), new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.Gray);

or pass NULL (or C# equivalant) in the position and center parameters.

If you want to position the sprite using its center at say x,y
and use the entire texture

You could use:
Sprite.Draw(Texture, new Rectangle(0, 0, texture_width, texture_height), new Vector3(texture_width / 2, texture_height / 2, 0), new Vector3(x, y, 0), Color.Gray);

But this will not work when correctly when using scaling (other than 1.0)


****************

HTH,
Cambo_frog

[Edited by - Cambo_frog on November 9, 2004 3:17:33 AM]

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