...i don't know how to show different pictures without listing the names of the files in the program |
This problem seems separate from the random display issue. If I have a list of file names needed in a program I will put those names in one file, then hard code the name of that one file in the program. Although, if the file names really are as you have shown, you could generate the names in the program as codewalker outlined above.
Taking the 1st approach, as it's more general. The filenames can be any legal name.
Suppose we have a fileNames.txt with the following content
Put no newline following the last line, OK?
1 2 3 4 5
|
std::vector<std::string> imageFileNames;
std::ifstream fin("fileNames.txt");
std::string temp;
while( fin >> temp )
imageFileNames.push_back( temp );
|
That should get all the image file names loaded into a vector.
I assume you can generate your random #'s OK (say, unsigned int randomIndex). I'll assume that they fall in the range 0 to imageFileNames.size() - 1, inclusive.
You can postpone loading the images until you need to display them.
Assuming we also have a vector of default constructed sf::Image:
std::vector<sf::Image> imageVec( imageFileNames.size(), sf::Image() );// create vector with all needed sf::Images in it already
Then, when it's time to draw an image:
1 2 3 4 5 6 7 8
|
if( imageVec[randomIndex].getPixelsPtr() == NULL )// image has not been loaded yet. Assuming SFML v2.1 here
imageVec[randomIndex].loadFromFile( imageFileNames[randomIndex].c_str() );
theSprite.setTexture( imageVec[randomIndex] );// theSprite is whatever sprite you're using to draw an image through
// Draw it. rw is your sf::RenderWindow instance
rw.draw( theSprite );
|