🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Memory problems...

Started by
-1 comments, last by Ake 24 years, 9 months ago
Hi everybody! (Hi Dr.Nick!

I'm having some memory/pointer problems in my engine, and I hope
someone could help me...

I have a class called CPolygon which looks something like this:

code:
	 int    m_iNumVertices;	 int    m_pVertices[ 8 ];	 CTextureCoord m_pTextureCoords[ 8 ];	 int    m_iTexture;	 int    m_iLightmap;	 CPlane m_cPlane;	 long   m_bRendered;	 CPolygon(int iNumVerts)	 {	    m_iNumVertices = iNumVerts;	    m_iTexture  = BM_TEXTURE_NONE;	    m_iLightmap = BM_TEXTURE_NONE;            m_cPlane    = CPlane();	    m_bRendered = 0; //Set to frame number         }

Now, because switching between texture in hardware can be slow, I want to use
a system of sorting my polygon by their texture, and draw all the polygon with
one texture first, and so on. So I created a new class...
code:
class CPolygonSystem { public:  CPolygon *m_cPolygons;  int       m_iMaxPolygons;  int       m_iNumPolygons;  CPolygon *m_cSorted;  CPolygonSystem(int iPolygons)  {     m_cPolygons = (CPolygon*)new CPolygon[iPolygons];     m_cSorted   = (CPolygon*)new CPolygon[iPolygons];     m_iMaxPolygons = iPolygons;     m_iNumPolygons = 0;  }  ~CPolygonSystem()  {    delete m_cPolygons;    delete m_cSorted;    m_iMaxPolygons = 0;    m_iNumPolygons = 0;  }  CPolygon *GetPolygon(int iNum)  {     return (CPolygon*)&m_cPolygons[iNum];  }  void _GetPolygon(CPolygon *poly, int iNum)  {     memcpy( (CPolygon*)poly, (CPolygon*)&m_cPolygons[iNum], sizeof(CPolygon) );  }  void AddPolygonToRender(CPolygon *Polygon)  {     m_iNumPolygons += 1;     if(m_iNumPolygons < m_iMaxPolygons )     {	memcpy( (CPolygon*)&m_cPolygons[m_iNumPolygons - 1], (CPolygon*)&Polygon,  sizeof( CPolygon ) );     }  }};

Now I wanted to test this system...
code:
	CPolygonSystem *cSyst = new CPolygonSystem(1000);	CPolygon *Poly = new CPolygon(5);	Poly->m_iTexture = 153;	cSyst->AddPolygonToRender( Poly );	 	CPolygon *Tmp;//	cSyst->_GetPolygon( Tmp, 0 );	Tmp = cSyst->GetPolygon( 0 );   log_file = fopen("beam.txt","wt");   handle_error( ERROR_CONSOLE, "Numverts %d - Texture %d", Tmp->m_iNumVertices, Tmp->m_iTexture);   handle_error( ERROR_CONSOLE, "\nNumverts %d - Texture %d", Poly->m_iNumVertices, Poly->m_iTexture);   fclose(log_file);

Now this gives the output:
Numverts 7866096 - Texture 1984
Numverts 5 - Texture 153
Not really what I wanted it too, when I use the _GetPolygon instead, the program crashes.
Any idea what could be the problem?

TIA, Tobias

This topic is closed to new replies.

Advertisement