Jump to content
  • Advertisement
Sign in to follow this  
chbrules

Need help figuring out how to design an efficient container for data

This topic is 4913 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

Alright, my ongoing process of creating this game has led me to a dead end when I try to get the value of a nested pointer from another class' struct array's pointer in an int array. Ya pretty bad design. So I wanted to redo this part a lot better; working for that matter. I was considering something like this:
int *Map = new int[Y][X];
I'd then fill those values up, write it to a file in binary using fwrite(), then fread() them into my engine from file when I call that file. I have a class that handles the importing of the map data for my game, then I pass a pointer over to my other class which handles/sort the data that the other class imports. Do any of you think this is a good idea for storing my data and being able to actually read it when I try to read the value that it points to? This is the mess I have now that I want to redo:
struct sMap{ int *tile_ID; bool *clip; }; //Our map's tile and clipping data

//Create new map struct arrays
	Map = new sMap[Header->y];

	for(int i=0; i < Header->y; i++)
	{
		//Make 'new' vars
		Map.tile_ID = new int[Header->x];
		Map.clip = new bool[Header->x];

		for(int q=0; q < Header->x; q++)
		{
			//Set vars
			Map.tile_ID[q] = rand() % 3;
			Map.clip[q] = 0;
		}
	}
Somehow I still think that my method is goin to screw up into a mess again. :S

Share this post


Link to post
Share on other sites
Advertisement
hmm, since your using C++ (or that's how it looks, since your using the new keyword, I would do something like this (Note: Untested code):

template<typename Type>
class Array2D {
Type** array;
int width, height;
public:
Array2D(int width, int height) : array(new Type[width][height]) {
this->width = width;
this->height = height;
}
~Array2D() {
for (int i = 0; i < width; i++) {
delete[] array[width];
}
}

Type& get(int width, int height) {
return array[width][height];
}
};

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!