Manl400
Manl400

Reputation: 35

How can I have a map array?

How do I load Levels in my game, as in Layer 1 would be Objects, Layer 2 would be Characters and so on. I only need 3 layers, and they will all be put on top of each other. i.e having a flower with a transparent background to be put on grass or dirt on the layer below.I would like to Read From the same file too. How would i go about doing this? Any help would be appreciated.

I load the map from a level file which are just numbers corresponding to a tile in the tilesheet.

And here is the code that interprets it

void LoadMap(const char *filename, std::vector< std::vector <int> > &map)
{
    std::ifstream openfile(filename); 
    if(openfile.is_open())
    {
        std::string line, value;
        int space;

        while(!openfile.eof())
        {
            std::getline(openfile, line);

            if(line.find("[TileSet]") != std::string::npos)
            {
                state = TileSet;
                continue;
            }
            else if (line.find("[Layer1]") != std::string::npos)
            {
                state = Map;
                continue;
            }

            switch(state)
            {
            case TileSet:
                if(line.length() > 0)
                    tileSet = al_load_bitmap(line.c_str());
                break;
            case Map: 

                std::stringstream str(line);
                std::vector<int> tempVector;

                while(!str.eof())
                {
                    std::getline(str, value, ' ');
                    if(value.length() > 0)
                                 tempVector.push_back(atoi(value.c_str()));
                }
                map.push_back(tempVector);
                break;
            }
        }
    }
    else
    {
  }
  }

and this is how it draws the map. Also the tile sheet is 1280 by 1280 and the tilesizeX and tilesizeY is 64

void DrawMap(std::vector <std::vector <int> > map)
{    
    int mapRowCount = map.size();

    for(int i, j = 0; i < mapRowCount; i ++)
    {
        int mapColCount = map[i].size();

        for (int j = 0; j < mapColCount; ++j)
        {
              int tilesetIndex = map[i][j];
              int tilesetRow = floor(tilesetIndex / TILESET_COLCOUNT);
              int tilesetCol = tilesetIndex % TILESET_COLCOUNT;    
              al_draw_bitmap_region(tileSet, tilesetCol * TileSizeX, tilesetRow * TileSizeY, TileSizeX, TileSizeY, j * TileSizeX, i * TileSizeX, NULL);
        }
    }
}

EDIT: https://i.sstatic.net/VDUqv.jpg

Upvotes: 1

Views: 131

Answers (1)

Thomas Matthews
Thomas Matthews

Reputation: 57678

Looks like you need more than one two-dimensional array.

Try something like this:

unsigned int map[3][8][8];
#define OBJECTS_LAYER 0
#define CHARACTER_LAYER 1
#define FLOWER_ID 2

//...

map[OBJECTS_LAYER][3][4] = FLOWER_ID;

Edit 1

Per the OP's description, a layer is a 2 dimensional array, 8 x 8, which would be represented as:

unsigned int layer[8][8];

Th OP's map has multiple layers. This translates into a container of layers.
One method is to use another dimension of the array. Much like a layered cake, or floors in a building.

unsigned int map[/* maximum layers */][8 /* from layer above */][8 /* from layer above */];

To access the position [3,4] of layer 1 the expression would be:

map[0][3][4]

Since indices are zero-based in C++, the first layer has index of zero.

To access floor 3, row 2, column 7 of a building one would use the nomenclature:

empire_state_building[2][1][6]

Upvotes: 2

Related Questions