(Fixed)(Beginner) How to use D3DXCreateTextureFromFile to load a texture

Started by
0 comments, last by precious roy 11 years, 2 months ago

Hello,

i have been looking for an answer to this question for the past 2 days and have only been able to find one that sometimes works. i will start by showing my code. the code in this case loads a model and continues to load the materials and textures.


void LoadModel(LPWSTR FileName, LPWSTR FileLocation)
{
    LPD3DXBUFFER bufShipMaterial;
	

    D3DXLoadMeshFromX((std::wstring(FileLocation) + FileName).c_str(),    // load this file
                      D3DXMESH_SYSTEMMEM,    // load the mesh into system memory
                      d3ddev,    // the Direct3D Device
                      NULL,    // we aren't using adjacency
                      &bufShipMaterial,    // put the materials here
                      NULL,    // we aren't using effect instances
                      &numMaterials,    // the number of materials in this model
                      &ModelMesh);    // put the mesh here

    // retrieve the pointer to the buffer containing the material information
    D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)bufShipMaterial->GetBufferPointer();

    // create a new material buffer for each material in the mesh
    material = new D3DMATERIAL9[numMaterials];
	texture = new LPDIRECT3DTEXTURE9[numMaterials];

    for(DWORD i = 0; i < numMaterials; i++)    // for each material...
    {
        material[i] = tempMaterials[i].MatD3D;    // get the material info
        material[i].Ambient = material[i].Diffuse;    // make ambient the same as diffuse


	//if there is a texture to load, load it
	if(FAILED(D3DXCreateTextureFromFileA(d3ddev, 
                  tempMaterials[i].pTextureFilename,&texture[i])))
		texture[i] = NULL;    // if there is no texture, set the texture to NULL
    }
}

The code above works fine to a point. when the texture is not in the same folder as the executable it will not lode it. you can see that when i load the texture i add the FileLocation to the Filename and this works like a charm. but now i want to do the same when loading my textures and this is where the problem start. i wiped up this, it probably well i know its not the best way of doing this because this gives me an error 2 out of the 3 times running the program


		if(tempMaterials[i].pTextureFilename != NULL)
		{
			//converting the lpstw from pTextureFilename to lpwstr
			char* convertMe = new char[sizeof(tempMaterials[i].pTextureFilename)];
			strcpy(convertMe,tempMaterials[i].pTextureFilename);
			int buffSize = (int)strlen(convertMe) + 1;
			LPWSTR TestureName = new wchar_t[buffSize];
			MultiByteToWideChar(CP_ACP, 0, convertMe, buffSize, TestureName, buffSize);
			std::wstring test = (std::wstring(FileLocation) + TestureName);

			//if there is a texture to load, load it
			if(FAILED(D3DXCreateTextureFromFile(d3ddev, test.c_str(), &texture[i])))
				texture[i] = NULL;    // if there is no texture, set the texture to NULL
		}
		else
			texture[i] = NULL;

the error that it gives is "Attempted to read or write protected memory. This is often an indication that other memory is corrupt.". anyways what i'm doing here is converting a LPSTR string to LPWSTR and then combine them, just like i did withe the model file.

now i know that the problem is somewhere in the converting of the string, i guess my question is how am i supposed to do this ?

Advertisement

Ok after a long search i managed to find out how to fix the problem.

i ended up making it like this.


		if(tempMaterials[i].pTextureFilename != NULL)
		{
			DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, tempMaterials[i].pTextureFilename, -1, NULL, 0);
			wchar_t *pwText;
			pwText = new wchar_t[dwNum];

			if(!pwText)
			{
				delete []pwText;
			}

			MultiByteToWideChar (CP_ACP, 0, tempMaterials[i].pTextureFilename, -1, pwText, dwNum );

			std::wstring TextureName = FileLocation;
			TextureName.append(pwText);

			//if there is a texture to load, load it
			if(FAILED(D3DXCreateTextureFromFile(d3ddev, TextureName.c_str(), &texture[i])))
				texture[i] = NULL;    // if there is no texture, set the texture to NULL

			delete []pwText;
		}
		else
			texture[i] = NULL;

Thanks Chris!

This topic is closed to new replies.

Advertisement