I want to load a map from text tile, but the coordinates are not aligned to a grid. So, a map of tiles is not useful for me. I write, in a text file (manually), the positions of the sprites.
Something like this:
name:path:x:y
sprite1:resources\sprite1.png:25:24
So I load:
while(reading)
{
std::string line;
std::string p[4];
getline(..., line)
split(line, ':', &p); //in this case I'll create a method to do this
add p[0] and p[1] to a map (<string, string>) and return the id of the created texture
create a sprite (sf::Sprite name(id_created_texture))
set the position of the sprite to (p[2], p[3])
}
ps: the above is a draft of what I do.
I want to receive tips for making it.
Thanks in advance!
Okay well then open the text file and using a for loop, cycle through each line. Save one line at a time inside of a std::string and then us another for loop to go character by character and, depending on the character, save a specific image into a vector.
for (int row = 0; file.good(); ++row) {
std::string line;
getline(file, line);
for (int col = 0; col < line.length(); ++col) {
if (line[col] == '1') {
// save image for ID of 1
}
// .... so on and so forth
}
}
Well for some reason the function he passes the string[] in to sees the string[] as a char and not a string so it won't compile...
I see what you're saying. Allow me to explain. A std::string is nothing more than a character array. Therefore using the brackets on a string is simply accessing a specific element in the character array.
std::string line = "Tresky";
line[0] will return 'T'
line[2] will return 'e'
etc.
@mastodona7x: Now you just need to write code=cpp instead of just code to get a code tag that highlights C++ code! ;)
Can anyone give me an idea of how to save / load a map with objects in different positions (not aligned to a grid)? I am spending much time on this.
How does your game look like up to now? Because having objects with their own positions, probably requires something into the direction of an entity system.
For now I just assume that you have a st::map<std::string, sf::Sprite> that you will fill with sprites and use a key to access them more easily.
For a start you have to think about how your map format will look like. I could imagine something like this:
name:path/to/the/texture.png:10:20
This means each line starts with the name of the sprite/character/whatever that will end up as key, then we use a delimited ':' to seperate the diffrent properties. Next to the name you put the path to the texture that should be used for the sprite and the the position as x and y coordination.
Now you have to read that file line by line (code can be found earlier on this thread) and then split each line into it's individual parts. For that you can easily google "split string c++". And since you now have everything as string you'll have to convert the numbers into real numbers (int, float...), this can also be found easily with google. But keep in mind google is only a help to find a solution fast, to really understand it you'll have to figure it out on your own, or at least try to understand every step of the code you'd find.
Since you now have everything you need, you can just go ahead and create a new texture, load it from the provided path, then hand the texture over to the new sprite and set its position.
In the following pastie (http://pastebin.com/1HxUnzBj) you can find a solution that I've used in one of my application. I may have missed some variables when copying. This solution is only one way of doing things, for example I don't use a std::map to hold my sprites as suggested. Also the use of std::move isn't always appropriate, so don't use it if you don't know what you're doing.