Page 1 of 1

Loading Maps..

Posted: Mon Nov 24, 2008 9:08 pm
by dandymcgee
Okay, so I'm working on my tile engine (a.k.a. my "game"), and I'm trying to figure out how best to handle loading and storing maps in the most dynamic way possible.

First off @ MarauderIIC:
Is this at all similar to what you tried to explain?:

level.txt

Code: Select all

//tilex, tiley, levelx, levely
32 32 160 160
tiles.txt
tile 2 0 0
tile 4 1 0
tile 1 2 0
tile 1 3 0
tile 0 4 0

tile 3 0 1
tile 3 1 1
tile 2 2 1
tile 2 3 1
tile 0 4 1

tile 2 0 2
tile 0 1 2
tile 1 2 2
tile 1 3 2
tile 1 4 2

tile 0 0 3
tile 2 1 3
tile 4 2 3
tile 1 3 3
tile 1 4 3

tile 4 0 4
tile 3 1 4
tile 0 2 4
tile 3 3 4
tile 3 4 4
tiles.txt

Code: Select all

a.png 0 //tile 0
b.png 0 //tile 1
c.png 0 //tile 2
a.png 1 //tile 3 (same as tile 0 but with collision)
b.png 1 //tile 4 (same as tile 1 but with collision)
Back on topic...

This is what my map file looks like at the moment:
test.map (line 1 is tilex, tiley, levelx, levely, -1 to terminate):

Code: Select all

32 32 640 480 -1

21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21

21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21

21 21 21 21 21 21 21 21 21 27 32 32 32 39 21 21 21 21 21 21

21 21 21 36 21 21 21 21 21 38 21 21 21 28 31 21 21 21 21 21

21 21 21 29 32 32 32 32 32 41 21 21 21 38 21 21 21 21 21 21

21 21 21 21 21 21 21 21 21 21 21 21 21 38 21 21 21 21 21 21

21 21 21 21 21 21 21 21 21 21 21 21 21 28 32 31 21 21 21 21

21 21 21 27 32 32 32 32 32 32 32 32 32 41 21 21 21 21 21 21

21 21 21 38 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21

21 21 21 38 21 27 32 32 32 32 39 21 21 21 21 21 21 21 21 21

21 21 21 38 21 38 21 21 21 21 38 21 21 21 21 21 21 21 21 21

21 21 21 38 21 38 21 21 21 21 38 21 21 21 21 21 21 21 21 21

21 21 21 38 21 29 32 33 33 32 41 21 21 27 32 32 39 21 21 21

21 21 21 38 21 21 21 38 38 21 21 21 21 38 21 21 29 31 21 21

21 21 21 29 32 32 32 35 35 32 32 32 32 41 21 21 21 21 21 21
Level.h (Level class):

Code: Select all

class Level
{
    private:
        int mapData[20][15];
    public:
        Level();
        int Load( std::string filename );
        int Draw();
};
The question is how can I change ( int mapData[20][15]; ) so that I can create an array ( int mapData[tilesWide][tilesHigh]; ) depending on the values placed at the top of the map files? Is there some way to declare the size of a private class variable from within the Load function after I read it from the map? I was thinking about using a 2d vector, but that just seemed way too complicated. There must be something I'm just thinking way too hard about.

Thanks in advance for any help :)
-Dan

Re: Loading Maps..

Posted: Tue Nov 25, 2008 12:08 pm
by Amarant
Here's a website on creating a dynamically allocated multidimensional array http://www.eskimo.com/~scs/cclass/int/sx9b.html < it's from the Guides & Resources Topic.

The method described on that website could however be overkill. What I usually end up doing is creating a flat array like this:

Code: Select all

int * mapData = new int[width * height];
and then calculate the index for a coordinate on the fly like this.

Code: Select all

mapData[x + y * width] = ??;
int tile = mapData[x + y * width];
also don't forget to delete the allocated array in the destructor of your level class.

Code: Select all

class Level
{
    private:
        int * mapData;
        int width;
        int height;
    public:
        Level() { }
        ~Level()
        {
            if(mapData != 0)
                delete[] mapData;
        }

        int Load( std::string filename )
        {
            width = ?;
            height = ?;
            mapData = new int[width * height]; 
        } 
        int Draw();
};

Re: Loading Maps..

Posted: Tue Nov 25, 2008 2:25 pm
by dandymcgee
Hey Amarant, thanks for the link and thank you more for the alternate solution. That's really what I was looking for, as I was sure there had to be a much easier way to handle this. Haha, thanks again for helping out ;)

Re: Loading Maps..

Posted: Thu Nov 27, 2008 11:48 pm
by MarauderIIC
Actually a 2d vector isn't hard. It's just a vector of vectors. So you populate your child vectors and then push them into the parent vector just as if they were another object. Level.txt you could probably stand to remove the word "tile" :)

Edit: For instance... a simple, inefficient example

Code: Select all

vector <vector <Tile> > tiles;

...
void ...LoadRow(...) {
    vector<Tile> row;
    row.push_back(some tile);
    tiles.push_back(row);
}
But using a large 2d array would be fine in this case unless there's really no sensible standard upper bound for your level.

Re: Loading Maps..

Posted: Sat Nov 29, 2008 8:26 pm
by dandymcgee
MarauderIIC wrote:Level.txt you could probably stand to remove the word "tile" :)
Lol, I agree.
MarauderIIC wrote: But using a large 2d array would be fine in this case unless there's really no sensible standard upper bound for your level.
I'm basically using Amarant's example but with a vector as opposed to an array. It allows the whole dynamic thing, but without having to worry about a vector of vectors. In this case 2 dimensions aren't completely necessary, but if I ever need an example in the future I'll know where to look ;)

Thanks Marauder.