• Advertisement
Sign in to follow this  

D3DX Sprites appear bigger

This topic is 4949 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry if this question is dumb, but whenever I try to render a sprite using D3DX, it appears bigger onscreen (roughly one and a half the size it should be). Is there a reason for this? Or am I perhaps initializing something incorrectly in my code?
	if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "images/snakeHeaddown.png", &g_pTexture)))
		return false;
	if(FAILED(D3DXCreateSprite(g_pd3dDevice, &g_pSprite)))
		return false;
/**/
	g_pSprite->Begin(NULL);
	D3DXVECTOR3 foo(16, 16, 1);

	if(D3DXERR_INVALIDDATA == (g_pSprite->Draw(g_pTexture, NULL, NULL, &foo, 0xFFFFFFFF )))
		msgboxnumber(100);

Share this post


Link to post
Share on other sites
Advertisement
I don't believe I've set up any cameras yet. I'm just drawing to the screen and leaving it at that.

Share this post


Link to post
Share on other sites
Stupid question, but is it due to the fact you have your application in a lower resolution than your OS?

Share this post


Link to post
Share on other sites
Stupid question, as in, obvious? Either way, I think its a good question and probably your answer.

Share this post


Link to post
Share on other sites
you need to disable texture filtering using D3DXCreateTextureFromFileEx

D3DXCreateTextureFromFileEx(device, texture.c_str(), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &m_texture);

D3DX_FILTER_NONE
No scaling or filtering will take place. Pixels outside the bounds of the source image are assumed to be transparent black.

Share this post


Link to post
Share on other sites
Quote:
Original post by eFoDay
you need to disable texture filtering using D3DXCreateTextureFromFileEx

D3DXCreateTextureFromFileEx(device, texture.c_str(), D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &m_texture);

D3DX_FILTER_NONE
No scaling or filtering will take place. Pixels outside the bounds of the source image are assumed to be transparent black.


That worked... the image is not being resized, but now there is a black border (transparent, eh?) on the left and bottom sides of the picture, so it is still pretty much the same size as it was before.

I'm one step closer, but not quite there yet. Why is D3D trying to render pixels outside the bounds of the source image? I'm not specifying any sizes.

Quote:
Stupid question, but is it due to the fact you have your application in a lower resolution than your OS?
No, I'm running in windowed mode, not fullscreen.

Share this post


Link to post
Share on other sites
Is your images size not powers of 2? A lot of cards still need textures with sizes being powers of 2.

The D3DXCreateTextureFromFile will acquire a bigger texture and put your image inside with borders.

You can use D3DXCreateTextureFromFileEx and specify D3DX_DEFAULT_NONPOW2 for width and height. This will only work on a card that supports textures of non power of 2 sizes.

Share this post


Link to post
Share on other sites
I was making a HUD system using the sprite tutorial from nexe and was just about to post the same problem when I saw this post. I am also getting a border around the sprite - though my sprite dimensions are powers of 2.

Share this post


Link to post
Share on other sites
you dont have to create in non powers of two. You can scale it down to original size:


scale.x = origWidth / width;
scale.y = origHeight / height;

D3DXMatrixTransformation2D (... & scale );

sprite->SetTransform(& matrix);
sprite->Draw(..)


you can calculate width by getting a surface description from the texture's highest miplevel, or you can just use the rule of thumb: round up to power of two. Eg:

124 -> 128
343 -> 512
10 -> 16

etc..

Share this post


Link to post
Share on other sites
Q: How do I get rid of black borders around my non-powers-of-2-sprites?
A: If you're using ID3DXSprite, use the D3DXSPRITE_ALPHABLEND-flag when calling ID3DXSprite::Begin().

Share this post


Link to post
Share on other sites
Quote:
Original post by dhanji
you dont have to create in non powers of two. You can scale it down to original size:

If you do this your texture gets filtered multiple times (once on load and once on draw). Depending on your art this can cause nasty artifacts.

Share this post


Link to post
Share on other sites
Wow, I feel dumb. My image was 25x25 px when I thought it was 16x16. Anyway, the problem's solved, thanks for everything guys.

I learned a lot from this thread.

Share this post


Link to post
Share on other sites
By the way, if you should need to use sprites whose dimensions aren't powers of two, you can still render them without the border by explicitly stating the coordinates in the source-rect.
Since textures can only be stored with these dimension limits, passing an empty rect (or equivalent) will cause the whole texture to be rendered, including the black border.

Share this post


Link to post
Share on other sites
Ah, yes. That makes sense, don't know why I didn't think of it before.

Thanks for the tip.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement