Creating sprites directly from textfile. (SFML)

Hi there,
I would like to ask how to create SFML sprites directly from a textfile.
The textfile holds some sprite names.
I know that it's not possible to force text/stringdata into a sprite vector, but how to convert that stringdata
in the textfile to a "real" spritename that is accepted by the spritecontainer for the sprite creation?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <vector>

//textfile looks like this:
/////////////////////////////////////////vehiclesprites.txt/////////////////////////////
//spritebus
//spritemaluch
///////////////////////////////////////////////////////////////////////////////////////

int main()
{
    sf::RenderWindow window(sf::VideoMode(500,500), "Map", sf::Style::Close);
    window.setFramerateLimit(30);

    sf::Texture texturebus;
    texturebus.loadFromFile("bus.png");
    sf::Sprite spritebus(texturebus);

    sf::Texture texturemaluch;
    texturemaluch.loadFromFile("maluch.png");
    sf::Sprite spritemaluch(texturemaluch);



    // WORKING EXAMPLE: reading string data from text file into string vector
    // and creation of sprites in the normal way

    //std::vector<std::string> VehicleNamesVector;
    //std::string stringdata;
    //
    //std::ifstream fileName;							
    //fileName.open("vehiclesprites.txt");
    //
    //while(fileName >> stringdata)
    //{
    //	VehicleNamesVector.push_back(stringdata);
    //}
    //
    //for(int i = 0;i<VehicleNamesVector.size();i++)
    //{
    //	std::cout << VehicleNamesVector[i] << std::endl;
    //}
    //
    //
    //
    //std::vector<sf::Sprite> VehicleSpritesVector;	
    //VehicleSpritesVector.push_back(spritemaluch);
    ////////////////////////////////////////////////////////////////




    //DOES NOT WORK: How to create a sprite from textfile?
    //How to teach him to interpret textfile content as spritename?

    std::ifstream fileName;
    fileName.open("vehiclesprites.txt");
	
    std::vector<sf::Sprite> VehicleSpritesVector;
    std::string stringdata;
	
	
    while(fileName >> stringdata)
    {
        VehicleSpritesVector.push_back(stringdata); // sprite container incompatible with stringdata
    }												
    //////////////////////////////////////////////////////////////

    while (window.isOpen())
    {
    sf::Event event;
		
    while (window.pollEvent(event))
    {
        switch (event.type)
	{
	    case sf::Event::Closed:
	        window.close();
		break;
	}
    }

    window.clear();
    for (auto& it = VehicleSpritesVector.rbegin(); it != VehicleSpritesVector.rend(); ++it)
    {
        window.draw(*it);
    }
    window.display();
}
    return 0;
}
Last edited on
You're trying to push a std::string into a vector of sf::Sprite type.

The first example works because you are pushing strings into a vector of strings, but in the second example you are trying to push strings into a vector of sprites.
If you want to push into VehicleSpritesVector you'll have to create a sprite with the string you read from the file and then push that sprite you created into the vector.
But I did create the sprites with identical names like in the text file. How to convert those spritenames in the textfile so that it will not try to read it as string, but as a sprite? I know how to convert string in int etc. (stringstreams...), but it doesn't work with sf::Sprite.
Last edited on
Are the names in the text file the names of the texture files for the sprites?
You can read the file name for a texture from a file and make a sprite using that, but you can't read a sprite from a file.

If you want a vector of sprites, you'll have to create a sprite and push that into the vector. You can use info from the text file to make the sprite, like what texture it'll use, but you have to push an actual sprite into that vector instead of a string.
Ok, thanks for the help. I think, that I've found now a revolutionary approach:
It seems, that in this case here, the spritenames aren't even relevant.
I only need the png file names in the text file for it to recognize everything correctly......

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <vector>

//textfile looks like this:
/////////////////////////////////////////vehiclenames.txt/////////////////////////////
//bus.png
//maluch.png
///////////////////////////////////////////////////////////////////////////////////////

int main()
{
    sf::RenderWindow window(sf::VideoMode(500,500), "Map", sf::Style::Close);
    window.setFramerateLimit(30);

    int textlinescounter = 0;

    sf::Texture TextureName;
    sf::Sprite SpriteName;
    std::string StringData;

    std::vector<sf::Texture> TextureVector;
    std::vector<sf::Sprite> SpriteVector;
    std::vector<std::string> FileNameVector;

    std::ifstream FileNameIfStream;							
    FileNameIfStream.open("vehiclenames.txt");
	
    while(FileNameIfStream >> StringData)
    {
        textlinescounter++;
	FileNameVector.push_back(StringData);
    }

    for(int i=0; i<textlinescounter; i++)
    {
        TextureVector.push_back(TextureName);
	SpriteVector.push_back(SpriteName);
	FileNameVector.push_back(FileNameVector[i]);
    }

    for(int i=0; i<textlinescounter; i++)
    {
        (TextureVector[i]).loadFromFile(FileNameVector[i]);
        (SpriteVector[i]).setTexture(TextureVector[i]);
    }

    while (window.isOpen())
    {
        sf::Event event;
		
	while (window.pollEvent(event))
	{
	    switch (event.type)
	    {
                case sf::Event::Closed:
		    window.close();
		    break;
		}
	    }

            window.clear();

	    for (auto& it = SpriteVector.rbegin(); it != SpriteVector.rend(); ++it)
            {
	        window.draw(*it);
            }

            window.display();
        }

        return 0;
    }
Last edited on
Topic archived. No new replies allowed.