DirectX: Need help with D3DXCreateTextureFromFileEx / D3DX_DEFAULT_NONPOW2

This is a discussion on DirectX: Need help with D3DXCreateTextureFromFileEx / D3DX_DEFAULT_NONPOW2 within the Game Programming forums, part of the General Programming Boards category; I have a player class here that inits the player and its sprite texture for drawing. The sprite map is ...

  1. #1
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283

    DirectX: Need help with D3DXCreateTextureFromFileEx / D3DX_DEFAULT_NONPOW2

    I have a player class here that inits the player and its sprite texture for drawing. The sprite map is 399x800, and each tile is 50x50. When I execute the program, the sprite is 64x64 instead of 50x50, so the sprite is cut off to the right and bottom. What can I do to draw this texture as 50x50?


    Code:
    class Common
    { 
    public:
    	LPDIRECT3DTEXTURE9 spriteTexture;
    	LPD3DXSPRITE sprite;
    	D3DXVECTOR3 pos; 
    	RECT srcRect;
    	D3DXVECTOR3 center;
    	
    	// Common Constructor
    	Common(void) 
    	{ 
    	}
    
    	void Init(int x, int y, char *filepath)
    	{
    		// Set the sprite position in screen space. 
    		pos = D3DXVECTOR3(x, y, 1);
     
    		hr = D3DXCreateTextureFromFileEx(pd3dDevice, filepath, D3DX_DEFAULT_NONPOW2, 
    			D3DX_DEFAULT_NONPOW2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 
    			D3DCOLOR_XRGB(255, 0, 255), NULL, NULL, &spriteTexture);
    
    		if (FAILED(hr))
    			return; 
    		
    		hr = D3DXCreateSprite(pd3dDevice, &sprite);
    		if (FAILED(hr))
    			return;
    
    		center = D3DXVECTOR3(0, 0, 0);
    
    		srcRect.left = 0;
    		srcRect.top = 0;
    		srcRect.right = 50;
    		srcRect.bottom = 50;
    	} 
    
    	// Common Deconstructor
    	~Common()
    	{
    		D3D_SAFE_RELEASE(spriteTexture);
    		D3D_SAFE_RELEASE(sprite);
    	}
    
    	void Draw()
    	{
    		sprite->Begin(0);
    		sprite->Draw(spriteTexture, &srcRect, &center, &pos, D3DCOLOR_XRGB(255, 255, 255));
    		sprite->End();
    	}
    };

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Video drivers will clamp your texture size to the nearest power of 2. So 65x65 becomes 64x64 and 50x50 becomes 64x64.

    Always use a power of 2 instead of those weird dimensions you posted.

    I have no idea why you would want to specify to D3D to not use a power of 2.

    There is a very good reason hardware uses a power of 2.

    It's called multiplying and dividing through bit shifting left and right and it speeds up the operation enormously.

    Forcing your hardware to do otherwise is not a good idea, if it even will do that.

  3. #3
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    I see your points and think it's a good idea, but with my pre-made sprite maps wouldn't it blur the detail?

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    Yes it would. You would have to re-create the sprite images. You may be able to get away with nearest power of 2 resize in photoshop, gimp, or paint shop pro with little loss in your image.

  5. #5
    Software engineer
    Join Date
    Aug 2005
    Location
    Oregon
    Posts
    283
    Alright, thanks for the help Bubba.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    So 65x65 becomes 64x64
    Are you sure about this? I was under the impression it would be 128x128...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Isometric Tile Engine using DirectX
    By Wraithan in forum Game Programming
    Replies: 3
    Last Post: 07-17-2006, 12:16 PM
  2. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 06:16 PM
  3. DirectX - Starting Guide?
    By Zeusbwr in forum Game Programming
    Replies: 13
    Last Post: 11-24-2004, 11:49 PM
  4. DirectX 8 with Borland
    By Tommaso in forum Game Programming
    Replies: 11
    Last Post: 08-12-2003, 09:30 AM
  5. Directx SDK Documentation
    By Zoalord in forum Game Programming
    Replies: 4
    Last Post: 05-08-2003, 06:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21