Skip to main content
gamedev.net
🔒 Locked

Problem with CreateTexture()!

Started by raccoonone 5 replies 650+ views
Original Post
raccoonone
raccoonone
I've been working on a Singleton class to manage textures for the game I'm working on. It stores the textures in a hash table. But I'm having a problem when I try to add the textures, for some reason the CreateTexture() function isn't creating a LPDIRECT3DTEXTURE8 for me. It just sets my pointer to NULL. I checked that m_pD3DDevice is pointing to a valid device, and it is. I don't know very much about DirectX, but I've used CreateTexture before and never had problems with it. Can anyone tell me why it won't create a new texture?

struct tmTexture
{
	LPDIRECT3DTEXTURE8 ptexture;
	int TextureID;
	string TextureName;
	tmTexture *next;
};

class CTextureManager
{
private:
	int hash(int key);
private:
	tmTexture **m_pptexturetable;
	int m_iNumofLists;
	LPDIRECT3DDEVICE8 m_pD3DDevice;
	static CTextureManager *m_singletonptr;
public:
	CTextureManager();
	ErrorMessage Setup(LPDIRECT3DDEVICE8 Device, char filename[] = "textures.txt");
	~CTextureManager();
	ErrorMessage GetTexture(int id, LPDIRECT3DTEXTURE8 &ptexture);
	ErrorMessage AddTexture(string filename, int id);
	static CTextureManager *GetSingleton();
};

ErrorMessage CTextureManager::AddTexture(string filename, int id)
{
	ifstream fin;
	CBitMap bitmaploader;
	CPixel *pBitmap;
	int Width, Height;
	tmTexture *tempnode;
	D3DLOCKED_RECT kLockedRect;
	filename += ".bmp";

	//Load bit map data
	bitmaploader.Load_BitMap(filename.data(), &pBitmap, &Width, &Height);

	//allocate memory for node
	tempnode = new tmTexture;
	//allocate memory for texture
	tempnode->TextureID = id;
	filename.erase(filename.end() - 4, filename.end());
	tempnode->TextureName = filename;
	tempnode->next = NULL;
	//Create Direct 3D Texture
	if(FAILED(m_pD3DDevice->CreateTexture(Width, Height, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &tempnode->ptexture)))
	{
		return emD3DTextureFailedToCreate; //Problem is here! The texture is never created.
	}
	tempnode->ptexture->LockRect(0, &kLockedRect, NULL, 0);
	DWORD *piTextureBuffer = (DWORD *) kLockedRect.pBits;

	for(int x = 0; x < (Width * Height); x++)
	{
		piTextureBuffer[x] = D3DCOLOR_XRGB(pBitmap[x].GetPixel().r, pBitmap[x].GetPixel().g, pBitmap[x].GetPixel().b);
	}
	
	tempnode->ptexture->UnlockRect(0);
	delete[] pBitmap;
	//Find which list to insert texture in and move to end of list
	tmTexture *cur = m_pptexturetable[hash(id)];
	for(;cur->next != NULL; cur = cur->next);
	cur->next = tempnode;

	return emNoError;
}


jollyjeffers
jollyjeffers
what sort of values are Width and Height - used in your CreateTexture() call?

They're the only two parameters I can see as obvious candidates for failing - they can be quite dependent on the underlying hardware/driver. I don't see any enumeration code to verify that the dimensions loaded from the file are valid [smile]

Also, have you tried enabling the D3D debug runtimes? they often spit out useful descriptions as to quite what it doesn't like.

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
jollyjeffers
jollyjeffers
Another couple of things I just thought of:

1. Any particular reason why you're not using D3DXCreateTextureFromFile() or D3DXCreateTextureFromFileEx() ??

2. Your looping code, where it fills in the data, could be a source of problems due to the pitch of each row in the surface/texture, there might be more than (Width * Height) DWORD's. Most importantly, they may well cause your data to be offset incorrectly given the way you're writing it [smile]

You should really be incrementing by the 'pitch' for each row.

hth
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
raccoonone
raccoonone
I checked the Height and Width, they're both 10 (which is what they should be).

I didn't know about the CreateTextureFromFile(), I'm pretty new to DirectX. Thanks for telling me about it! I'll learn how to use that. It'll save me time instead of writting my own functions to load bitmaps, and then converting them to textures.
raccoonone
raccoonone
I tired to use D3DXCreateTextureFromFile(), but the following errors:

Alpha 0.2 fatal error LNK1104: cannot open file 'libci.lib'

I added "D3dx8.lib", to the Additional Dependancies for the project.
and I included #include

I assume that's a library file that I need to install or something? I know almost nothing about how .lib files work, except that when I don't need them to the dependancies I get linker errors.
raccoonone
raccoonone
Thanks!!!

That fixed my problem, and it's a lot easier to use that function:)

Topic Locked

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

Sign in to reply to this topic.