I've now set VSync(true) and removed the couts.
Now I have a constant framerate and it works without laggs.
I still have another question:
I want to build my RPG out of many rectangles and I will save them in a 2D Array.
How should I structure the data in the file? And which file format would you recommend me to use?
...
Good to hear that it works!
Assuming you're going to have pre-built maps you should have a look at the
Tiled map editor. It uses XML as a format.
If you're going to have generated maps though and you want to save them to a file... you would probably still be best off using XML. Although it's not going to be much use to you for just tiles, you're going to love it later on when you're saving enemies, sprites etc.
Another problem is that I want to expand and update the Map sometimes.
So if I save the users last position, how can I place his character on the same position after the map update?
I don't quite understand what you mean. Do you mean that you're going to update the map at runtime? (e.g. secret passageway opens up by removing a tile). Or do you mean you're going to update the map via a map editor?
For both cases you shouldn't need to worry. However do keep in mind that moving objects shouldn't be tiles.
You're going to have some trouble with transparency and many other things otherwise.
The best solution would be to just use tilemaps of maybe 255x255 rectangles and a 4D Array.
The first two dimension would specify the position of the tilemap and the second two the content of the tilemap.
A 4D array? You should only need 2 dimensions. If you wanted to you could even make a 1D array.
Here's an example for a 1D array which uses numbers to represent different types of tiles:
const int map[16] =
{
2, 2, 2, 2,
2, 1, 1, 2,
2, 2, 1, 2,
2, 2, 2, 2
};
To get a tile you would do this:
map[x * mapWidth + y];An example for this case would be:
map[3 * 4 + 2]; (coordinates start at 0)
which would return '2' because the tile at [3, 2] has the ID 2.
Also each rectangle should be filled with a sprite for the surface.
So should I just use an Array of Sprites and use a switch case to load the textures into each rectangle(sprite)?
Kind of. Use an
std::vector<sf::Texture>.
Here's an example:
std::vector<sf::Texture> textureMap;
textureMap.push_back(...); // Texture for Tile ID 1
textureMap.push_back(...); // Texture for Tile ID 2
// and so on...
Here's an example for drawing with the above setup:
sf::Sprite tileSprite; // This should preferrably be a class member.
for (unsigned int x = 0; x < mapWidth; ++x)
{
for (unsigned int y = 0, y < mapHeight; ++y)
{
int id = map[x * mapWidth + y]; // Get the ID at [x, y] from the map.
tileSprite.setTexture(textureMap[id - 1]); // - 1 since id #1 is #0 in the vector.
window.draw(tileSprite); // Draw it.
}
}
There also must be a value for each rectangle whether clipping is enables for it or not.
I also need to save the height because I have at least 2 terrain levels.
The ground and objects that shoud be drawn over it, like houses, trees etc.
You could use a seperate array just for the collision map. But that's pretty inefficient, I advise against it.
So instead what you should do is make a vector like above that stores bools rather than textures.
For the layers, you would add another dimension to the array.
I've removed the pointer..
After these changes the programm still need constantly 33% of my CPU?!
And sometimes, instead of making big jumps like before, it still scrolls faster?!
Have you tried a release build? From my experience a debug build with a framelimit of 60fps will take up to 10-40% CPU. While a release build produces a mere 03-10%.