Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trying to create a tilemap in a class.  (Read 1856 times)

0 Members and 1 Guest are viewing this topic.

Darkpony

  • Newbie
  • *
  • Posts: 7
    • View Profile
Trying to create a tilemap in a class.
« 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;
}

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Trying to create a tilemap in a class.
« Reply #1 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.
{
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Martin Sand

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: Trying to create a tilemap in a class.
« Reply #2 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