0

I'm trying to write some code to read a list of file names from a config file named "TextureList.txt" and then use those files to create a shader resource view for each texture. The listing below shows what I currently have:

LPCSTR textures[MAX_TEXTURES];

std::ifstream texturesToLoad;

texturesToLoad.open("TextureList.txt");

int numberOfTextures = 0;

while(!texturesToLoad.eof())
{
    std::string textureName;
    std::getline(texturesToLoad, textureName);

    textures[numberOfTextures] = textureName.c_str();

    numberOfTextures++;

    char debugMsg[100];
    sprintf(debugMsg, "numberOfTextures = %d\n", numberOfTextures);
    OutputDebugString(debugMsg);
    OutputDebugString(textureName.c_str());
    OutputDebugString("\n");
}

for(int i = 0; i < numberOfTextures; i++)
{       
    d3dResult = D3DX11CreateShaderResourceViewFromFile( d3dDevice_,
    textures[i], 0, 0, &colorMap_[i], 0 );

    if( FAILED( d3dResult ) )
    {
        DXTRACE_MSG( "Failed to load the texture image!" );
        return false;
    }
}

The debugs at the bottom of the while loop show that the file is being read correctly, it obtains all of the file names and calculates the correct number of textures; however it fails when it tries to create the first resource view.

Can anyone tell me where I'm going wrong?

I don't know if it helps but it works if I replace everything above the for loop with hard coded file names using the following line:

const LPCSTR textures[3] = {"tex1.dds", "tex2.dds", "tex3.dds"};

Nick
  • 21
  • 4
  • BTW, rather than use the [deprecated D3DX11](https://msdn.microsoft.com/en-us/library/windows/desktop/ee663275.aspx) texure loader, use [DDSTextureLoader](http://blogs.msdn.com/b/chuckw/archive/2012/05/04/direct3d-11-textures-and-block-compression.aspx) from the [DirectX Tool Kit](http://go.microsoft.com/fwlink/?LinkId=248929) instead. – Chuck Walbourn Feb 11 '15 at 07:21
  • What's the ``d3dResult``? The HRESULT gives you a strong hint as to why it failed. – Chuck Walbourn Feb 11 '15 at 07:22
  • Hi, I had another look at the error it was giving me and I seem to have found the problem. I was pointing the LPCSTRs at textureName, which goes out of scope on each iteration of the while loop. So when I tried to load the texture, the LPCSTR was no longer pointing at valid memory. Thanks for your help – Nick Feb 11 '15 at 18:36

0 Answers0