Reputation: 551
In my game, when the "time" is active, it would always crash after 5 "hours". When it's running, it's working fine until it starts displaying an incorrect time (for reasons unknown) and it would crash after 5 "hours" due to a segmentation fault. That's weird because there's no such thing before and it's supposed to be doing the same thing over and over. This is my code. Try to tell me if there's a place where a memory leak could occur over time. BTW, the "time_tick()" occurs every "while" loop.
struct global_struct
{
int seconds;
int minutes;
int hours;
TTF_Font * font;
SDL_Color font_color;
SDL_Color bk_color;
};
global_struct global;
void GL_RENDER_TTF(const char * Text, TTF_Font * Font, SDL_Color Textcolor, SDL_Color bk_color, int x, int y, float depth)
{
SDL_Surface * surface;
int w;
int h;
if(!(Font == NULL))
{
surface = TTF_RenderUTF8_Shaded(Font,Text,Textcolor,bk_color);
if(!(surface == NULL))
{
w = surface->w;
h = surface->h;
GLuint texture;
GLenum texture_format;
GLint nOfColors;
surface = SDL_DisplayFormatAlpha(surface);
nOfColors = surface->format->BytesPerPixel;
if (nOfColors == 4)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
else if (nOfColors == 3)
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0, texture_format, GL_UNSIGNED_BYTE, surface->pixels );
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f); glVertex3f(x,y,depth);
glTexCoord2f(1.0f,0.0f); glVertex3f(x+w,y,depth);
glTexCoord2f(1.0f,1.0f); glVertex3f(x+w,y+h,depth);
glTexCoord2f(0.0f,1.0f); glVertex3f(x,y+h,depth);
glEnd();
};
};
};
void time_tick(float depth1)
{
global.seconds = global.seconds + 1;
if(global.seconds > 59)
{
global.seconds = 0;
global.minutes = global.minutes + 1;
};
if(global.minutes > 59)
{
global.minutes = 0;
global.hours = global.hours + 1;
};
if(global.hours > 23)
{
global.hours = 0;
};
string time;
string shours;
string sminutes;
string sseconds;
stringstream ssseconds;
stringstream ssminutes;
stringstream sshours;
ssseconds << global.seconds;
ssminutes << global.minutes;
sshours << global.hours;
if(global.hours < 10)
{
shours = "0"+sshours.str();
}
else
{
shours = sshours.str();
};
if(global.minutes < 10)
{
sminutes = "0"+ssminutes.str();
}
else
{
sminutes = ssminutes.str();
};
if(global.seconds < 10)
{
sseconds = "0"+ssseconds.str();
}
else
{
sseconds = ssseconds.str();
};
time = "time: "+shours+"h "+sminutes+"m "+sseconds+"s";
GL_RENDER_TTF(time.c_str(),global.font,global.font_color,global.bk_color,0,0,depth1);
SDL_Delay(10);
};
Upvotes: 1
Views: 509
Reputation: 162164
Everytime you call RENDER_TTF
you're creating a new texture, whilst the texture from the previous one still lingers around. If you can use a constant texture size, use glTexSubImage2D
to replace the texture contents. Otherwise use glDeleteTextures
to free the only texture, before creating a new one.
You can also reuse a texture ID by just calling glTexImage2D
anew with the old texture object being bound (i.e. no glGenTextures
, but this is discouraged by recent versions of OpenGL and glTexStorage
completely disallows it).
Upvotes: 2