Jump to content
  • Advertisement
Sign in to follow this  
Geno the Great

I seem to have hit a snag in my map loader

This topic is 5036 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
Advertisement
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 = new int[Height];

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

// Deallocation
for(int i = 0; i < Width; ++i)
delete[] MapArray;
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!