Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

Pointer to Multi-D array

This topic is 4849 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, i'm having some problems with an multi-D array of pointers. This is what I have so far
class TileSet
{
public:
  int *TileInformation[][];  //contains data such as invalid tiles    
  Tile Grass;
  Tile Flower;
  Tile Dirt;

  TileSet();
};
This is my tile set. I want TileInformation to vary in size depending on GameEngine.Scree_Width and Height. So in my constructor
                             
TileSet::TileSet()
{
 this->TileInformation = new int[GameEngine.Screen_Width / 32 + 64][GameEngine.Screen_Height / 32 + 64];
}
I know its ugly, just want to get it working. Except this clearly isn't the way to make a new multi-d array. I've looked in my books, and on the net, but I couldn't find what I was looking for. Perhaps i'm just being slow (i've been canoeing all week, so be kind :P) Any help would be deadly. IF this this isn't the best way to handle this, then what would you suggest? BTW, the GameEngine is declared before TileSet. and GameEngine.Screen_Height (and width) are called and set *after* TileSet is created. So i'm thinking I have a problem here. I just finished reading all of André Lamothe's Tricks of the Game Programming Gurus 2nd edition in one 6hr sitting driving back from Calgary, so my brain is total mush atm :P Cheers! [Edited by - _Sigma on August 6, 2005 1:49:39 AM]

Share this post


Link to post
Share on other sites
Advertisement
A few ways to do this:

//Method 1: An array of pointers to arrays.
int ** twodeearray;

twodeearray = new int * [ width ];
for ( unsigned int x = 0 ; x != width ; ++x ) {
twodeearray[ x ] = new int[ height ];
for ( unsigned int y = 0 ; y != height ; ++y ) {
twodeearray[ x ][ y ] = ...;
}
}

...

for ( unsigned int x = 0 ; x != width ; ++x ) {
delete[] twodeearray[ x ];
}
delete[] twodeearray;

//Method 2: Use a single array.
int * onedeearray;
onedeearray = new int [ width * height ];

for ( unsigned int x = 0 ; x != width ; ++x ) {
for ( unsigned int y = 0 ; y != height ; ++y ) {
onedeearray[ x + y * width ] = ...;
}
}

delete[] onedeearray;

//Method 3: boost::multi_array (see http://boost.org/ for info on the boost libraries)
boost::multi_array< int , 2 > twodeeboostarray( boost::extents[width][height] );

for ( unsigned int x = 0 ; x != width ; ++x ) {
for ( unsigned int y = 0 ; y != height ; ++y ) {
twodeeboostarray[x][y] = ...;
}
}

//no need to manually delete, the mutli_array owns the data and cleans it up automatically when it is destroyed.




I prefer the last method myself. Methods 1 and 2 can optionally use std::vector or similar as well. But boost::multi_array owns.

Share this post


Link to post
Share on other sites
Hi there.

In your TileSet class you seem to be declaring that TileInformation is a 2D array of int pointers, but during the construction you're assigning the array to be of type Tile. Shouldn't the array be declared:

Tile* TileInformation[][];

The construction looks a bit dodgy (array = new type[row][col]), think you'll need to loop through the row and columns allocating Tile(s), oh and be careful on destruction to use the delete [] while looping thru the rows.

To be honest...must be coz of your mushy brain, you didn't really say what your problem was, so am just taking a guess..:)

Hope you feel better after a bit of sleep;)

Oh...as an aside. Maybe implementing this as either a 2D std::vector<> or a single std::vector<> might be wiser as memory allocation is done implicitly.

EDIT: Beaten by a cats whisker.

Share this post


Link to post
Share on other sites
Before tottal reading everything, yes it should be int, not tile. i have no idea why i put tile there :P

Share this post


Link to post
Share on other sites
Oh, and the problem is the fact I don't know how to make a 2d array of pointers :P or how ever its worded. jeez...i need sleep :S

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
Oh, and the problem is the fact I don't know how to make a 2d array of pointers :P or how ever its worded. jeez...i need sleep :S


*points at his own post made earlier*

Unfortunately you can't simply do new[x][y], you can either:

1) create a 1d array of pointers to 1d arrays (giving the illusion of a single 2d array)
2) create a 1d array and use math to access it as if it were 2d
3) use boost::multi_array

These numbers also correspond to the example methods provided, which show the creation, access, and cleanup used in each of these methods.

Share this post


Link to post
Share on other sites
Yes Mauling, I know, I know....:S sorry, i was super out of it while I posted that post. I think i'll use the boost libraries. It seems the nicest. Will it be as fast as a single-d array, used like a 2d array? Is it easy to work with?It being the BOOST libs.?

Cheers

Share this post


Link to post
Share on other sites
Alright, i think i'll go with the Boost library as you suggested MaulingMonkey. My question is this tho. Thanks for the help you two!

Share this post


Link to post
Share on other sites
Erm...dumb question. How do I use BOOST? DO i have to build somthing? Thats what the read me says...but...i'm a noob at 3rd party stuff like this, so be kind :P
//edit, WTF is a 'JAM' file!?!?!?!?!? Oh boy...

Share this post


Link to post
Share on other sites
Getting Started

A lot of the libraries are header only, which means you can simply copy their headers into the included directory of your choice - the Boost.MultiArray library is one of those. You'll need to build boost (AFAIK) in order to use: Boost.Serialization, Boost.Threads, Boost.Filesystem, Boost.DateTime, Boost.Regex, Boost.ProgramOptions, Boost.Signals, or Boost.Python (which also requires an installation of a python compiler).

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!