SFML community forums

Help => Graphics => Topic started by: Darkpony on April 16, 2017, 08:11:38 pm

Title: Trying to create a tilemap in a class.
Post by: Darkpony on April 16, 2017, 08:11:38 pm
In my constructor I have my TileMap object definition. But other functions of the class cant use the map. I tried defining map in the header but it really dosent work like that. I have a render function that does all my rendering like window.clear and window.draw(map). I also have a processEvents that processes all the events. I cant pass it to the function as a parameter either.

        window.create(sf::VideoMode(528, 256), "Tilemap");
                // define the level with an array of tile indices
        const int level[] =
        {
                        4, 4, 4,  4,  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                        4, 4, 4,  4,  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                        4, 4, 4,  4,  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                        12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
                        4, 4, 4,  4,  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                        4, 4, 4,  4,  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                        4, 4, 4,  4,  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                        4, 4, 4,  4,  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
                };

                //create the tilemap from the level definition
        TileMap map;
        if (!map.load("tileset.png", sf::Vector2u(33, 33), level, 16, 8))
                        std::cout << "Cant upload map" << std::endl;
}
Title: Re: Trying to create a tilemap in a class.
Post by: Hapax on April 17, 2017, 02:03:38 am
The level array only lasts as long as the scope in which it is contained. Therefore, it is destroyed at the end of this constructor.

What do you mean by it not really working in the header? You could define it outside the class since it's a constant array of integers but I'm guessing you want it as a member of the class and because it is const, it requires that it is initialized at created, not assigned to later?

.hpp
class A
{
public:
    const int level[10]; // remember to specify the size of the array

    A();
}

.cpp
A::A()
    : level{ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 } // etc.
{
}
Title: Re: Trying to create a tilemap in a class.
Post by: Martin Sand on April 17, 2017, 05:04:53 pm
[...]
But other functions of the class cant use the map.
[...]
I cant pass it to the function as a parameter either.

Darkpony, can you please provide more details about the context and include the functions which do not work?

Best regards
Martin