fallahn / sfml-tmxloader

SFML 2.x based tmx tilemap parser for files created with Tiled tile map editor
106 stars 33 forks source link

GetLayers() doesn't work as spected #64

Closed Tonire closed 8 years ago

Tonire commented 8 years ago

Hi! I'm trying to render each layer individually. For so I made this little function:

tmx::MapLayer Map::GetLayer(std::string layerName){
    for( int num = 0; num < ml.GetLayers().size(); num++)
    {
        tmx::MapLayer res = tmx::MapLayer(ml.GetLayers().at(num));

        if(res.name == layerName)
        {
            return res;
        }
    }
    return tmx::MapLayer(ml.GetLayers().at(0));
}

So im calling it like this:

m->mWindow->draw(GetLayer("Capa 1"));

And the result is a little square at the top of the window image

When the layer is much bigger

image

I don't know if GetLayers() is supposed to work like this or not. Maybe is a bug?

Thank you so much!

PS: Im using gcc 4.8 on Windows, if that helps...

fallahn commented 8 years ago

A couple of things I notice: mainly this

tmx::MapLayer res = tmx::MapLayer(ml.GetLayers().at(num));

is going to create a copy of a layer and is probably not going to perform brilliantly compared to fetching a reference. Even worse it gets copied again when it's returned from your function. gcc 4.8 should support range-based loops so:

const MapLayer& getLayer(const std::string& name)
{
    const auto& layers = ml.getLayers();
    for(const auto& l : layers)
    {
        if(l.name == name) return l;
    }
    return layers[0];
}

might serve you better. On the other hand (although I admit I can't remember off the top of my head) the map loader does some behind the scenes stuff with culling and setting views to optimise layer drawing. In which case drawing the layer directly won't work and you'll need something like

ml.draw(window, index, false);

to draw a specific layer. In this case your getLayer() function should just return the index of the layer that contains the name you want, or 0 if it does not exist.

ml.draw(window, getLayer("myLayer"), false);
Tonire commented 8 years ago

Thank you very much! Performance problems aside, I can't call ml->Draw() because the first parameter is a RenderTarget. I think I'm missing something here, I can't pass it my sf::RenderWindow :/

Tonire commented 8 years ago

Anyways, I made It to work implementing MapLoater::Draw(sf::RenderWindow rw, MapLayer ml, bool debug = false); myself and It is working nice!

Thank you again <3

fallahn commented 8 years ago

No problem. From what it looks like in your sample code your window is a pointer, so this should work:

ml.draw(*window, index, false);

as RenderWindow inherits RenderTarget.