Skip to main content
gamedev.net
🔒 Locked

sprite texture sizes

Started by d3dm33t 3 replies 950+ views
Original Post
d3dm33t
d3dm33t
I am using a sprite to show a texture which is the title screen for my game. My window is 1024x768 and the jpg is 1024x768 however when I show the sprite with the texture loaded from my jpg, it does not fit in the window. It appears as if the jpg is too big. Has anyone seen this before? Here is my sprite code: I call this function first to load the jpg as a texture:

void CWindow::showPicture(LPCTSTR filename)
{
	if( !FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, filename, &g_pTexture)))
	{
		g_pd3dDevice->SetTexture(0, g_pTexture);
		D3DXCreateSprite(g_pd3dDevice, &sprite);
		sprite->Draw(g_pTexture, NULL, (512, 384, 0), (0, 0, 0), 0xFFFFFFFF);
	}
}

This is the code I use to show the sprite:

	if(myWindow.getTexture() == NULL)
		myWindow.showPicture("title.jpg");
	myWindow.BeginRender();
	myWindow.getSprite()->Begin(NULL);
	myWindow.getSprite()->Draw(myWindow.getTexture(), NULL, NULL, NULL, 0xffffffff);
	myWindow.getSprite()->End();
	myWindow.EndRender();
	SendMessage(myWindow.getHwnd(), WM_PAINT,0,0);

And here are some extra class members I use in the code above:

LPDIRECT3DTEXTURE9 g_pTexture;		// Texture
ID3DXSprite* sprite;

ID3DXSprite* CWindow::getSprite()
{
	return sprite;
}

LPDIRECT3DTEXTURE9 CWindow::getTexture()
{
	return g_pTexture;
}

I checked and double checked that my jpg is indeed 1024x768. Any help would be appreciated.
Armadon
Armadon
Hi there d3dm33t,
How are you doing?

The Problem
Textured sprite rendering. Texture does not fit window.

The Solution
The texture you are trying to draw is exactly the size of the window. Although when rendering textures it's always a good idea that the textures be power of 2 textures. In your case something like 1024x1024 or whatever your choose.

Another point is that a texture of 1024x768 will be bigger than the window since the window has borders etc. So the window view size might be closer to 1022x766 or something to that effect.

With this knowledge you can either play around with it to fit the window or you could create a new texture.

Also if you really want the correct size of the window without the borders. There is a cool function called GetClientRect

PS: When rendering your sprite you can specify (0, 0, 0) for the second parameter. Otherwise your sprite might be off. If you specify the center as the middle of the texture and then place the sprite at (0, 0, 0) which is your next coordinate you are cutting off some of the texture. Here is an example of how you can render your sprite.
sprite->Draw(g_pTexture, NULL, (0, 0, 0), (0, 0, 0), 0xFFFFFFFF);

I hope this helps, I am sure there are other forum members that may have more insight. Good luck buddy, don't give up.
d3dm33t
d3dm33t
Yeah, I thought it might be the border, title bar, etc that might be causing it so I switched to fullscreen and get the same problem. I changed it to:

sprite->Draw(g_pTexture, NULL, (0, 0, 0), (0, 0, 0), 0xFFFFFFFF);

and got no change.
Sages
Sages
The window size isn't really an issue, because the backbuffer is what is being rendered to and it will simply get stretched to the size of the window.

D3DXCreateTextureFromFile() is going to resize that texture of yours since it isn't a power of two. I have heard of people getting bitten by this in the past.

I personally don't use the ID3DXSprite interface but it shouldn't be hard to get it working with the square power of two texture.

Feel free to ask more questions if necessary, also let me know if the solution doesn't work.
d3dm33t
d3dm33t
That did it! I had to change my texture to a 1024x1024 and it fit perfectly! Thanks for he help guys!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.