1

私のゲームでは、「時間」がアクティブな場合、5「時間」後に常にクラッシュします。実行中は、(理由は不明ですが)誤った時刻が表示され始めるまで正常に動作し、セグメンテーション違反のために5"時間"後にクラッシュします。これまでにそのようなことはなく、同じことを何度も繰り返すことになっているので、それは奇妙です。これは私のコードです。時間の経過とともにメモリリークが発生する可能性がある場所があるかどうかを教えてください。ところで、「time_tick()」は「while」ループごとに発生します。

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);
};
4

1 に答える 1

2

電話をかけるRENDER_TTFたびに、新しいテクスチャを作成していますが、前のテクスチャのテクスチャはまだ残っています。一定のテクスチャサイズを使用できる場合は、を使用glTexSubImage2Dしてテクスチャの内容を置き換えます。それ以外の場合は、新しいテクスチャを作成する前に、を使用glDeleteTexturesして唯一のテクスチャを解放します。

古いテクスチャオブジェクトをバインドして新たに呼び出すだけでテクスチャIDを再利用することもできますglTexImage2D(つまり、いいえglGenTextures。ただし、これは最近のバージョンのOpenGLでは推奨されておらず、glTexStorage完全に禁止されています)。

于 2012-07-07T23:37:08.117 に答える