Sign in to follow this  
Renze_Tantra

dynamic 2D array in a map file

Recommended Posts

I'm programming a strategy role playing game. My plan is to create one "HexTile" class to represent a tile, and one "Map" class for the whole map itself. The map itself should contain a dynamically allocated 2d array of the tiles, among other info. At the end of the day, I want to save an instance of the map to a file to be loaded into the actual engine. Is there anything wrong with this general plan? I'm already having a problem getting a dynamic 2D array into the map object. 1st issue: I didn't realize that dynamically allocating a 2D array is drastically different from dynamically allocating a 1D array. Here's the general way I was trying to set the dimensions of the 2D array (nevermind syntax errors, you see what I was hoping to do):
void MapMetal::SetDimensions(int x, int y)
{
	horiz = x;
	vert = y; //sets these values for our information

	//now to allocate the array of hex tiles in this size, and set the pointer to it.
//	TileArray = new *HexTile[x][y]; //could this possibly work
	
};
TileArray is a HexTile object. MapMetal is the main map class, and HexTile is the individual tile type that I wish to create a 2d array of. Is there any way to keep this simple? 2nd issue: Am I going to have to overload the = operator since I'm using my own object type? I always plan on having the same type on each side of the = sign... do I still need to overload it?

Share this post


Link to post
Share on other sites
Actually it would be; TileArray = new *HexTile[x * y];

But that is not the big problem here. Where do you free the memory that you just allocated?

theTroll

Share this post


Link to post
Share on other sites
I would go with the 1D Array. Then you can easily access it by

tile[y*width+x]

2D arrays over complicate unnecessarily in this case. Well, you use the term HexTile which makes me think isometric and I am not sure of rendering techniques for that kind of map. But I am sure you still wont need a 2d array.

Don't forget to delete before you new again (re-dimensioning it).

BUT...if this is a learning exercise...go ahead and do the 2D...

To create a 2D array, I think (its been a while since I used them) you have to do something like

Tile = new HexTile[x][y];
for (i = 0; i < x; i++)
Tile[i] = new HexTile[y];

And to delete you have to loop through again.
Someone please correct me if I am wrong.

Share this post


Link to post
Share on other sites
Somehow, yes... 1D array

Yeah!

I can use a 1D array to represent my map. Every (width) I'll add a (length).

I feel dirty...

[Edited by - Renze_Tantra on March 7, 2008 1:00:07 PM]

Share this post


Link to post
Share on other sites

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