Loading many files with concise code

Hi,

I would like to ask how to load multiple files/images, without having to write (in extreme cases) thousands of lines of code:

For example if I want to create the following (sfml),
I would have to write 100 times the same with numbers from 1 to 100:


sf::Image mapimage1;
mapimage1.loadFromFile("mapimage1.png");
......

sf::Imagemapimage100;
mapimage100.loadFromFile("mapimage100.png");


Now I've tried this, but sf::Image somehow doesn't accept the std::string "ImageName":
Any help appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  for(int i=1; i<=100; i++)
{

	std::stringstream stream;
	stream << "mapimage" << i << ".png";
	std::string fileName = stream.str();
	

	std::stringstream stream2;
	stream2 << "mapimage" << i;
	std::string ImageName = stream2.str();

	sf::Image ImageName;
	ImageName.loadFromFile(fileName);
}
Last edited on
Now I've tried this, but sf::Image somehow doesn't accept the std::string "ImageName":
Any help appreciated.

Either compile the program using a C++11 or greater compiler or convert the string to a c_str().

You can't have ImageName as a std::string and an sf::Image within the same scope. You can't declare what is inside a string as a variable name. Perhaps you can use a struct or union to hold the values you want?
It is the sense of the whole program to use the string as sf::Image variable name.
I can't imagine how to achieve the desired effect by a different approach. In which way could a struct help here?
Last edited on
I've tried it with a string array, but it's the same problem.
I don't have the know-how and don't know what to do to prevent having to write:

1
2
3
4
5
6
sf::Image mapimage1;
sf::Image mapimage2;
sf::Image mapimage3;
....
sf::Image mapimage9998;
sf::Image mapimage9999;


Could you just use a container like std::vector<sf::Image*> mapImages and create a loop to load them dynamically? Why do you need the variable name to coincide with the image file name?
Last edited on
You're right and my solution looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	sf::Image ImageName;
	sf::Texture TextureName;
	sf::Sprite SpriteName;

	std::vector<std::tuple<sf::Image,sf::Texture,std::string>> Map;
	std::vector<sf::Sprite> MapSprite;

	int x = 0;

	for(int i=0; i<3; i++)
	{
		std::stringstream stream;
		stream << "map" << i << ".png";
		std::string FileName = stream.str();
		
		Map.push_back(std::make_tuple(ImageName,TextureName,FileName));
		MapSprite.push_back(SpriteName);

		std::get<0>(Map[i]).loadFromFile(std::get<2>(Map[i]));
		std::get<1>(Map[i]).loadFromImage(std::get<0>(Map[i]));
		(MapSprite[i]).setPosition(x,0);
		x=x+500;

		
	}

	for(int i=0; i<3; i++)
	{
	(MapSprite[i]).setTexture(std::get<1>(Map[i]));
	}





It's not perfect, because if you remove some images, there is a white rect problem due to the nature of vectors (memory adresses change when vector size is altered), so I'm playing now with arrays and try to overcome that somehow :-).
Last edited on
Topic archived. No new replies allowed.