Pointer members...Dynamic...

Started by
4 comments, last by HemoGloben 19 years, 7 months ago
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.
if(this.post == SATISFYING){money.send(1.00,&HemoGloben);return thanks;}
Advertisement
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;}
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.
if(this.post == SATISFYING){money.send(1.00,&HemoGloben);return thanks;}
Quote:Original post by HemoGloben
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.

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.
Here it is one more time for everyone who missed it...
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.
if(this.post == SATISFYING){money.send(1.00,&HemoGloben);return thanks;}

This topic is closed to new replies.

Advertisement