• Advertisement
Sign in to follow this  

LPD3DXSPRITE

This topic is 3763 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

Hello! Right then it's been a long hard day and I need a break, but before I do I need some help as I can't seem to spot what I'm doing wrong here, but hopefully you fine chaps might be able to! Jolly good! :) Ok, I'm attempting to use LPD3DXSPRITE for the basis of my game, but before that i'm learn the basics on DX as i've never done it before...basically I want to be able to load in any size of image and use that as my texture, but scale it to any size (1-N), position it but the sprites local 0,0 must be in it's center (not in the example below tho, this is for a background image). Here the code below so you can get an idea of WHAT i'm trying to do:

		// Draw with alpha blending - needed for our transparent sprites
		gSprite->Begin(D3DXSPRITE_ALPHABLEND);


		// Sprite is 64x64 in size
		RECT drawRect;

		drawRect.left   = 0;
		drawRect.right  = 64; // Width
		drawRect.top    = 0;
		drawRect.bottom = 64; // Height

		// Screen dimensions = 800 x 600

		D3DXVECTOR2 scale( 1.0f/64.0f, 1.0f/64.0f ); // Scale the sprite down to size of 1 local to the screen
		D3DXVECTOR2 scale2( 800.0f, 600.0f );		 // I want the sprite to cover the whole screen for my background image
		D3DXVECTOR2 scale3;

		// Multiply the two scale vector objects to scale it to fit the screen
		scale3.x = scale.x * scale2.x;
		scale3.y = scale.y * scale2.y;
				
		// Position the image from 0,0
		D3DXVECTOR2 pos = D3DXVECTOR2( 0.0f, 0.0f );

		// Matrix to pass to the sprites transform function
		D3DXMATRIX mat;

		D3DXMatrixTransformation2D( &mat, // out
					    &D3DXVECTOR2( 0.0f, 0.0f ),
                                             0.0f,     // scaling rotation
					    &scale3,  // scaling
					    NULL,  // rotation center
					    0.0f,  // rotation
					    &pos ); // translation

		

		gSprite->SetTransform( &mat );

		gSprite->Draw( gTexture, &drawRect, NULL, NULL, D3DCOLOR_COLORVALUE( 1.0f, 1.0f, 1.0f, 1.0f ) );

		// Finished drawing. By reusing the same sprite object D3D can maximise batching of the draws
		gSprite->End();

		// Finished rendering
		gD3dDevice->EndScene();
		gD3dDevice->Present( NULL, NULL, NULL, NULL );

The sprite covers the entire screen fine in terms of X, but in terms of Y it goes slightly over so the bottom of the image is missing! Like it's scaling over slightly...I've tried this with several screen resolutions and different images...i still get the same problem...any ideas please? Cheers!

Share this post


Link to post
Share on other sites
Advertisement
You can only load images that are of a Power of 2. If your image is not a power of 2, directX will scale it so that it is. That may be your problem

Share this post


Link to post
Share on other sites
I tried this out real quick using the same basic premise and it seemed to work just fine. This is the code I used:



CComPtr<IDirect3DTexture9> tex = textureManager->GetTexture(TEXT("brick01.bmp"));
D3DSURFACE_DESC desc;
tex->GetLevelDesc(0, &desc);
FLOAT texW = desc.Width, texH = desc.Height;

D3DXMATRIXA16 transform;
D3DXMatrixTransformation2D(&transform, &D3DXVECTOR2(0,0), 0, &D3DXVECTOR2(backBufferW/texW, backBufferH/texH), NULL, 0, NULL);
spriteInterface->SetTransform(&transform);

spriteInterface->Draw(tex, NULL, NULL, NULL, 0xffffffff);



The only part I could see something going wrong in your code is if the dimensions of your textures aren't actually 64. Its probably do a good idea to a quick check and verify that your texture is actually being loaded as 64x64, or just base your scaling on the dimensions in the surface description like I did in my code.

Share this post


Link to post
Share on other sites
Quote:
Original post by stringa
You can only load images that are of a Power of 2. If your image is not a power of 2, directX will scale it so that it is. That may be your problem


This only happens on GPU's that don't support non-power-of-2 textures, which is mainly old DX8 stuff. You can check for it by querying for the D3DPTEXTURECAPS_POW2 bit in the texture caps.

Share this post


Link to post
Share on other sites
My gfx card supports non - power of 2 textures...i even tried changing the image to the same size of the screen (800x600) and only having it to a scale of 1 and it still ended up clipping over the bottom of the screen...wtf lol? This has got me baffled...can more ideas please? :)

Share this post


Link to post
Share on other sites
Quote:

This only happens on GPU's that don't support non-power-of-2 textures, which is mainly old DX8 stuff. You can check for it by querying for the D3DPTEXTURECAPS_POW2 bit in the texture caps.

If the texture is loaded with D3DXCreateTextureFromFile, however, the image is resized regardless of the device capabilities. D3DXCreateTextureFromFile is D3DXCreateTextureFromFileEx with a bunch of default (D3DX_DEFAULT) values -- including for width and height. This causes the API to round the file's width and height up to the next power of two and scale. To disable this, D3DXCreateTextureFromFileEx must be called and D3DX_DEFAULT_NONPOW2 must be specified.

The OP doesn't specify how the texture is loaded, but D3DX is likely. Here is the link to the documentation for the D3DXCreateTextureFromFileEx function.

Share this post


Link to post
Share on other sites
Thanks to everyone for your replies and jpetrie for your explanations of D3DXCreateTextureFromFileEx :) I've actually solved it, the window had a menu bar at the top and i didn't see it (sorry lol)...I should have a court order against me for me to be banned from PC's for life...

Share this post


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

  • Advertisement