@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 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.