Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Memory problems...


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 Ake   Members   -  Reputation: 122

Like
Likes
Like

Posted 26 September 1999 - 08:41 AM

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


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS