Sign in to follow this  

I seem to have hit a snag in my map loader

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In short, I need a way to create a 2 dimensional array of a size that isn't known (The map size) and then set the size when it is known (In the map class's constructor) I've tried using a 2D vector, but this got confusing since I've never had to use vectors before. Is there any other way? Or should I just make it so that maps have a fixed size (Not my favorite solution, but it WOULD work...)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Of course there is a way, but im making an asumption here that that is a std::vector container, therefore in C++ ;)


int * MapArray;

//Allocation
MapArray = new int[Height * Width];

// Itheration
for(int x = 0; x < Width; ++x)
for(int y = 0; y < Height ; ++y)
MapArray[y * Width + x] = FooBar;

//Deallocation
delete[] MapArray;



or


int ** MapArray;

//Allocation
MapArray = new int*[Width];
for(int i = 0; i < Width; ++i)
MapArray[i] = new int[Height];

// Access
MapArray[5][5] = FooBar;

// Deallocation
for(int i = 0; i < Width; ++i)
delete[] MapArray[i];
delete[] MapArray;




IMO A stl vector is better, typesaftey ;)

-Decsonic

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
i would do it like our good friend 'Anonymous Poster' said. firstly however, you would have to load some sizes from a file (say SizeX, SizeY) and then (id use the second example by AP. this way you know exactly which tile you want to do something with - you dont need to work it out from a 1d array) use those sizes to create the 2d array of map data/tiles.

kinda like this little alogrithm-ish thing!:
int sizex, sizey;
int **mapdata;
Load MapSizeX (from file) into sizex;
Load MapSizeY (from file) into sizey;
Create mapdata array (same as AP using sizex and sizey as the sizes for the array);
Load the tiles from the file into mapdata array using 2 'for' loops, one for each dimension of the array:
for(int x = 0; x < sizex; ++x)
for(int y = 0; y < sizey; ++y)
mapdata[x][y] = --whatever the current tile in the file is (so like tile[x][y] from the file)--;

thats about it for loading maps. man i rambled on a fair bit there! sorry! hope you understood it all - thats the basic idea i used for loading the maps with my editor that im making and it works fine! ;)

let us know if ur still having probs. ;)
the_moo

Share this post


Link to post
Share on other sites

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this