1
General / When creating a tile map, sprites do not always texture properly
« on: May 07, 2013, 03:28:33 pm »
So, as the title says, I'm making a tile map loader (using the Tiled format).
The images show 2 instances of the program loading the map. One, the tiles are fine. Another, the tiles are blacked out, but only on one layer. I first thought that this was a problem with the vector in which I store my tiles, but its not. The hex values are pointers to the sf::Textures that I have loaded in my texture manager. You're wondering, well if it works in one instance, then why are you here? The images are the same program. I was stress testing my program; loading the map over and over and noticed this problem. Sometimes it works, sometimes it doesn't. I'm baffled. The textures are fine, cuz the memory is the same. The vector should be reserved correctly, but I'm not sure. The tile id's are the same every single time I load. I'm trusting that SFML is fine, unless I discovered some issue.
Here is my map parsing code. I use RapidXML to parse the .tmx file.
Here is what I use to load textures.
I won't post more code unless needed to save everyone a headache. I got an itch that this is where the issue lies.
Tiled documentation: https://github.com/bjorn/tiled/wiki/TMX-Map-Format
The images show 2 instances of the program loading the map. One, the tiles are fine. Another, the tiles are blacked out, but only on one layer. I first thought that this was a problem with the vector in which I store my tiles, but its not. The hex values are pointers to the sf::Textures that I have loaded in my texture manager. You're wondering, well if it works in one instance, then why are you here? The images are the same program. I was stress testing my program; loading the map over and over and noticed this problem. Sometimes it works, sometimes it doesn't. I'm baffled. The textures are fine, cuz the memory is the same. The vector should be reserved correctly, but I'm not sure. The tile id's are the same every single time I load. I'm trusting that SFML is fine, unless I discovered some issue.
Here is my map parsing code. I use RapidXML to parse the .tmx file.
Quote
bool MapManager::parseData(const std::string& src)
{
try
{
rapidxml::xml_node<> *node_root = m_document->first_node("map"), *node_layer = node_root->first_node("layer"),
*node_data = 0;
std::string encoding = "", compression = "";
for(; node_layer; node_layer = node_layer->next_sibling("layer"))
{
std::deque<unsigned> split;
node_data = node_layer->first_node("data");
if(node_data->first_attribute("encoding"))
{
encoding = node_data->first_attribute("encoding")->value();
if(encoding == "csv")
{
split = m_parse_csv(node_data->value());
}
else if(encoding == "base64")
{
if(node_data->first_attribute("compression"))
{
if((compression = node_data->first_attribute("compression")->value()) == "zlib")
{
split = m_parse_zb64(node_data->value());
}
else
{
split = m_parse_b64(node_data->value());
}
}
}
else
{
return false;
}
for(int y = 0; y < m_height; y++)
{
for(int x = 0; x < m_width; x++)
{
int id = split.front();
be::Object::TileSet* t = m_decide_tileset(id);
if(id != 0 && t && !split.empty())
{
sf::Vector2i pos(x, y), size(t->width, t->height);
sf::IntRect text_split = m_decide_texture_split(id, t);
m_tile_map.push_back(new be::Object::Tile(id, pos, size, text_split, t->texture));
}
split.pop_front();
}
}
}
}
}
catch(const rapidxml::parse_error &e)
{
return false;
}
return true;
}
Here is what I use to load textures.
Quote
sf::Texture* TextureManager::getTexture(const std::string& src)
{
for(auto i = m_textures.begin(); i != m_textures.end(); i++)
{
if(i->first == src)
{
std::cout << i->second << std::endl;
return i->second;
}
}
sf::Texture* t = new sf::Texture;
if(t->loadFromFile(src))
{
std::cout << "Loading a new texture: " << src << std::endl;
m_textures[src] = t;
return t;
}
else
{
delete t;
return 0;
}
}
I won't post more code unless needed to save everyone a headache. I got an itch that this is where the issue lies.
Tiled documentation: https://github.com/bjorn/tiled/wiki/TMX-Map-Format