Perdita di memoria Quando si utilizza la libreria FreeType
Domanda
Nel seguente frammento di codice, perdono 52 byte di memoria per ogni iterazione del ciclo per, ma dopo diverse ore non riesco a determinare perché =/
Qualsiasi aiuto sarà apprezzato.
for(unsigned char i=0; i<128; ++i)
{
ttf.loadGlyph(i);
FT_Glyph glyph;
FT_Get_Glyph(ttf.ftFace_->glyph, &glyph);
FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
FT_Bitmap& bitmap = bitmap_glyph->bitmap;
int width = nextPowerOf2(bitmap.width);
int height = nextPowerOf2(bitmap.rows);
GLubyte* expanded_data = new GLubyte[ 2 * width * height]; // Allocate Memory For The Texture Data.
for(int j=0; j <height;j++) {
for(int i=0; i < width; i++){
expanded_data[2*(i+j*width)]= expanded_data[2*(i+j*width)+1] =
(i>=bitmap.width || j>=bitmap.rows) ?
0 : bitmap.buffer[i + bitmap.width*j];
}
}
glBindTexture( GL_TEXTURE_2D, fontData_.pTextures_[i]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data );
delete [] expanded_data;
glNewList(fontData_.fontBase_+i, GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, fontData_.pTextures_[i]);
glPushMatrix();
glTranslatef( (GLfloat)bitmap_glyph->left, 0.0f, 0.0f );
glTranslatef(0.0f, (GLfloat)bitmap_glyph->top-bitmap.rows, 0.0f);
float x=(float)bitmap.width / (float)width,
y=(float)bitmap.rows / (float)height;
glBegin(GL_QUADS);
glTexCoord2d(0,0); glVertex3f( 0, 0, 1 ); //Bottom-left vertex (corner)
glTexCoord2d(0,y); glVertex3f( 0, (GLfloat)bitmap.rows, 1 ); // top left vertex
glTexCoord2d(x,y); glVertex3f( (GLfloat)bitmap.width, (GLfloat)bitmap.rows, 1 ); // top right vertex
glTexCoord2d(x,0); glVertex3f( (GLfloat)bitmap.width, 0, 1 ); // bottom right
glEnd();
glPopMatrix();
glTranslatef( (GLfloat)( ttf.ftFace_->glyph->advance.x >> 6 ), 0, 0);
glEndList();
}
Soluzione
Da i documenti:
Ft_get_glyph
Una funzione utilizzata per estrarre un'immagine di glifi da uno slot. Si noti che l'oggetto ft_glyph creato deve essere rilasciato con ft_done_glyph.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow