Sign in to follow this  

Strings problem

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
If you don't mind using the STL, then std::stringstream can help you. You'll need to include <string> and <sstream>.

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();
}

//...Elsewhere
FileInput >> 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]);
}


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Ya, after looking at my code for a while I just realied the for condition looked wierd. I changed it a bit (making the for condition make sense :P) and now it works. Thanks for your suggestions though.

Share this post


Link to post
Share on other sites

This topic is 4868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this