Original Post
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;
}