Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ake

Memory problems...

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

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

Share this post


Link to post
Share on other sites
Advertisement

  • 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!