SFML community forums

Help => General => Topic started by: The Illusionist Mirage on August 28, 2013, 01:32:24 pm

Title: [Solved]Loading tile maps
Post by: The Illusionist Mirage on August 28, 2013, 01:32:24 pm
Hello

I downloaded a mario clone source from here : http://en.sfml-dev.org/forums/index.php?topic=12232.msg84944#msg84944 (http://en.sfml-dev.org/forums/index.php?topic=12232.msg84944#msg84944)

But I didn't understand the tile map loading part. Can anyone please explain it to me?

Thanks
Title: Re: Loading tile maps
Post by: G. on August 28, 2013, 04:19:39 pm
The tilemap is hardcoded into an array of sf::String. He doesn't really "load" the tilemap, he directly reads the array and draws it.

To draw the tilemap, he loops on every character in every string of his tilemap: loop from 0 to tilemap height (H) and loop from 0 to tilemap width (W). The character represents the type of the tile, P is ground, C is ?block, etc. He has only one sprite, and change its textureRect (with hardcoded values) according to the character read in TileMap[j], and its position too, then draw it.
Title: Re: Loading tile maps
Post by: The Illusionist Mirage on August 30, 2013, 03:24:48 pm
Thanks! Now I am clear about how stuff works using SFML.

BTW what do you suggesst is better - keeping tile maps in separate text files and using them through the fstream class objects or by declaring them in a sf::String?

Like:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
.
.
.
int main()
{
     std::fstream tilemap("Tilemap.txt", ios::in);
     .
     .
     .
}
 

OR

#include <SFML/Graphics.hpp>

sf::String tilemap[10][10] =
{ "1 1 1 1 1 1 1 1 1 1",
  "1                 1",
  "1                 1",
  "1                 1",
  "1                 1",
  "1     #####       1",
  "1                 1",
  "1                 1",
  "1                 1",
  "1 1 1 1 1 1 1 1 1 1",
};
.
.
.
int main()
{
     .
     .
     .
}


 
Title: Re: Loading tile maps
Post by: eXpl0it3r on August 30, 2013, 03:28:15 pm
External, so you can just edit the map and restart/reload instead of having to recompile every single time. ;)
Title: Re: Loading tile maps
Post by: The Illusionist Mirage on August 30, 2013, 03:37:03 pm
External, so you can just edit the map and restart/reload instead of having to recompile every single time. ;)

Thanks! :D