Search code examples
c++multidimensional-arrayvisual-studio-2012sfml

Tilemap array of unknown size


I want the users to pick there map size so I do not know the size of the map at run time. I have been looking around how I can implement this with little success. Closest to something that is working is with the vector variant but while it runs it still does not work properly.

I want a 3 dimensional array so in my header i declare the tilemap like this.

std::vector<std::vector<std::vector<Tile>>> tileMap;

and test it in the class like this:

World::World(int width, int height, int depth)
{
tileMap.resize(width);
tileMap[0].resize(height);
tileMap[0][0].resize(depth);

tileMap.resize(width);

for (int i = 0;i<width;i++)
{
    tileMap[i].resize(height);
}



for (int y = 0; y < height;y++)
{
    for (int x = 0; x < width; x++)
    {
        tileMap[x,y].resize(depth);
    }
}

std::cout << sizeof tileMap / sizeof tileMap[0][0][0] << std::endl;

}

I am aware that not all the dimensions of the array are resized properly but the final line just outputs "0".

Is there i better way to create my tilemap of unknown size? I need a lot of sizes like 64*64, 256*256, 64*1024, etc (the z levels will be determined after generating the level). so creating an array for each size seems inefficient. But maybe there is a better way of declaring my array or i am doing something wrong somewhere.


Solution

  • You want tileMap.size() * tileMap[0].size() * tileMap[0][0].size() to get the number of tiles.