FileInput >> MaxTextures;
aMapTileTexture = new IDirect3DTexture9*[MaxTextures];
for (int t=1;t>=MaxTextures;t++) {
char tile[30]="tile";
char num[4];
itoa(t,num,10);
strcat(tile, num);
strcat(tile,bmpS);
D3DXCreateTextureFromFile(g_d3d_device,tile,&aMapTileTexture[t]);
}
In theory this should work, but in practice it does not. Help please?
Strings problem
What I am trying to do is this; create a function that loads textures from a file, in a for loop that uses an integer value to figure out the name of the texture, and goes until it reaches the number of max textures defined in the file. Here is a sample:
If you don't mind using the STL, then std::stringstream can help you. You'll need to include <string> and <sstream>.
Or you could even just have a NumberToString() function, like so:
FileInput >> MaxTextures;aMapTileTexture = new IDirect3DTexture9*[MaxTextures];std::stringstream FileName;for (int t=1;t>=MaxTextures;t++){ FileName.str("tile"); FileName << t; D3DXCreateTextureFromFile(g_d3d_device,FileName.str().c_str(),&aMapTileTexture[t]); }
Or you could even just have a NumberToString() function, like so:
std::string NumberToString(int n){ std::stringstream s; s << n; return s.str();}//...ElsewhereFileInput >> MaxTextures;aMapTileTexture = new IDirect3DTexture9*[MaxTextures];for (int t=1;t>=MaxTextures;t++){ D3DXCreateTextureFromFile(g_d3d_device,std::string("tile" + NumberToString(t)).c_str(),&aMapTileTexture[t]); }
Quote:Original post by bobthebobert
What I am trying to do is this; create a function that loads textures from a file, in a for loop that uses an integer value to figure out the name of the texture, and goes until it reaches the number of max textures defined in the file. Here is a sample:FileInput >> MaxTextures; aMapTileTexture = new IDirect3DTexture9*[MaxTextures]; for (int t=1;t>=MaxTextures;t++) { char tile[30]="tile"; char num[4]; itoa(t,num,10); strcat(tile, num); strcat(tile,bmpS); D3DXCreateTextureFromFile(g_d3d_device,tile,&aMapTileTexture[t]); }
In theory this should work, but in practice it does not. Help please?
In theory, it should not work. And in practice it doesn't work. That's a good point I think :)
First, you can
sprintf(tile, "tile%04d.bmp", t)
And how do you expect this line to work ?
for (int t=1;t>=MaxTextures;t++) {
Your stop condition is rather strange (remember : it stops when it is no longer true. If MaxTextures is 3 then you loop will not iterate since t++ will be 2 at the next iteration). I am sure that you did not want that. Now, having your stop condition to t<=MaxTextures won't help you - you are referencing aMapTileTexture[t] which is an array of MaxTextures elements. Of course, your last access would be out of bounds. So you'll have to either reference aMapTileTexture[t-1] or to have t going from 0 to MaxTextures.
as a conclusion, you may try
for (int t=0;t<MaxTextures;t++) { char tile[30]="tile"; sprintf(tile, "tile%04d.bmp", t+1); // your first texture is tile0001.bmp D3DXCreateTextureFromFile(g_d3d_device,tile,&aMapTileTexture[t]);}
And voilà.
Now, in theory, this should work :)
PS. I used the %04d formet specifier because IMHO it is better to have numbers from 0000 to 9999 instead of 0 to 9999 - in th explorer, tile2 would be after tile1999...
EDIT : typos
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement