ID3DXSprite is not working?

Started by
13 comments, last by Cambo_frog 19 years, 5 months ago
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....
Things has been changed but the just the way it is....
Advertisement
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
For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.
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)
As far as I know, ID3DXSprite itself doesn't provide anything for scaling - I'm wondering if there's nothing you can do using the texture transform matrix, though.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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
For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.
Well, what if you do want to draw the sprites in screen space?
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
For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.
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.
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


For the love of god, please tell me that you've just omitted your error checking code for brevity, and you don't really assume that all those functions succeed.
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?

This topic is closed to new replies.

Advertisement