# Pointer members...Dynamic...

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

## Recommended Posts

I was working on a loose Terrain class, quick rough draft, when I ran into a problem.
class CTerrain
{
public:

CTerrain()
{
}
~CTerrain();

bool GenerateTerrainFromHeightMap(unsigned char * heightMap, int mapWidth, int mapLength);
bool drawTerrain();
private:
float * terrainMap[][];
int width;
int length;
};
CTerrain::~CTerrain()
{
delete terrainMap;
}
bool CTerrain::GenerateTerrainFromHeightMap(unsigned char * heightMap, int mapWidth, int mapLength)
{
width=mapWidth;
length=mapLength;

terrainMap = new float array [width][height];

for(int x=width;x>=0;x--)
{
for(int z=length;z>=0;z--)
{
terrainMap [x][z] =(float)(
((float)heightMap[((x)*width*3)+(z*3)]/255.0f)+
((float)heightMap[((x)*width*3)+(z*3)+1]/255.0f)+
((float)heightMap[((x)*width*3)+(z*3)+2]/255.0f));
}
}
return true;
}

bool CTerrain::drawTerrain()
{
for(int x=width;x>=0;x--)
{
glBegin(GL_LINE_STRIP);
for(int z=length;z>=0;z--)
{
glVertex3f( (float)x, terrainMap[x][z], (float)z);
glVertex3f( (float)(x+1), terrainMap[x+1][z], (float)z);
}
glEnd;
}
return true;
}

I need away to dynamically allocate the pointer(and it's fine on the heap, not really gonna go over 256 at the moment size wise), but obviously what I've done isn't legal. Good way around this? Thanks.

##### Share on other sites
class CTerrain{    public:                        CTerrain()        {		terrainMap = 0;		width = 0;		height = 0;        }        ~CTerrain();                bool GenerateTerrainFromHeightMap(unsigned char * heightMap, int mapWidth, int mapLength);        bool drawTerrain();    private:        float** terrainMap;        int width;        int length;  	void Destroy();};void CTerrain::Destroy(){	if(terrainMap)	{		for(int x=0;x<width)		{			delete[] terrainMap[x];		}	   	delete terrainMap;		terrainMap = 0;	}	width = 0;	height = 0;}CTerrain::~CTerrain(){	Destroy();	}    bool CTerrain::GenerateTerrainFromHeightMap(unsigned char * heightMap, int mapWidth, int mapLength){	Destroy();        width=mapWidth;        length=mapLength;                terrainMap = new float*[width];                 for(int x=width;x>=0;x--)         {	    terrainMap[x] = new float[height];            for(int z=length;z>=0;z--)            {                terrainMap [x][z] =(float)(                    ((float)heightMap[((x)*width*3)+(z*3)]/255.0f)+                    ((float)heightMap[((x)*width*3)+(z*3)+1]/255.0f)+                    ((float)heightMap[((x)*width*3)+(z*3)+2]/255.0f));            }        }          return true;}    bool CTerrain::drawTerrain(){    for(int x=width;x>=0;x--)    {        glBegin(GL_LINE_STRIP);        for(int z=length;z>=0;z--)        {            glVertex3f( (float)x, terrainMap[x][z], (float)z);            glVertex3f( (float)(x+1), terrainMap[x+1][z], (float)z);        }        glEnd;    }    return true;}

##### Share on other sites
Thanks, never would've thought of that. (Probably because I'd never seen it).

So, it creates a...pointer to an array of what?

terrainMap*[width];

That confuses me.

Hmm, I'll have to research that.

Thanks again though.

##### Share on other sites
Quote:
 Original post by HemoGlobenThanks, never would've thought of that. (Probably because I'd never seen it).So, it creates a...pointer to an array of what?terrainMap*[width];That confuses me.Hmm, I'll have to research that.Thanks again though.

terrainMap = new float*[width];
Creates a new array of width float*s. So if width is 4, you get an array of 4 float pointers. Then you initialise each pointer by setting it to point to an array of floats. So you have an array of pointers to floats. And each pointer points to an array fo floats.

##### Share on other sites
Hmm. I missed a few of those lines when I first read through the code. I had rtouble implementing it last night. Finally got it to compile, and it crashed the second I opened it. Maybe I'll post the entire code later(on school PC's at the moment).

Thanks for the help though, and I'll probably conver to container classes a little later.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 10
• 9
• 15
• 22