使用SDL_ttf和OpenGL,TTF_RenderUTF8_Blended打印红色矩形

时间:2012-10-02 23:53:11

标签: c++ opengl sdl sdl-ttf

当我使用TTF_RenderUTF8_Blended渲染文字时,我在屏幕上获得一个实心矩形。颜色取决于我选择的颜色,在我的情况下,矩形是红色。

Result using TTF_RenderUTF8_Blended

我的问题

我错过了什么?好像我没有从SDL_DisplayFormatAlpha(TTF_RenderUTF8_Blended( ... ))生成的表面获得正确的Alpha值,或者我?有人认识或知道这个问题吗?

附加信息

如果我使用TTF_RenderUTF8_SolidTTF_RenderUTF8_Shaded,则会正确绘制文字,但当然不会混合。

我也在屏幕上绘制其他纹理,所以我最后绘制文本以确保混合将考虑当前表面。

编辑: SDL_Color g_textColor = {255, 0, 0, 0};< - 我尝试使用和不使用alpha值,但我得到相同的结果。

我试图在不删除太多细节的情况下总结代码。前缀为“g_”的变量是全局变量。

Init()函数

// This function creates the required texture.
bool Init()
{
    // ...

    g_pFont = TTF_OpenFont("../arial.ttf", 12);
    if(g_pFont == NULL)
        return false;

    // Write text to surface
    g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Blended(g_pFont, "My first Text!", g_textColor)); //< Doesn't work

    // Note that Solid and Shaded Does work properly if I uncomment them.
    //g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Solid(g_pFont, "My first Text!", g_textColor));
    //g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Shaded(g_pFont, "My first Text!", g_textColor, g_bgColor));

    if(g_pText == NULL)
        return false;

    // Prepare the texture for the font
    GLenum textFormat;
    if(g_pText->format->BytesPerPixel == 4)
    {
        // alpha
        if(g_pText->format->Rmask == 0x000000ff)
            textFormat = GL_RGBA;
        else
            textFormat = GL_BGRA_EXT;
    }

    // Create the font's texture
    glGenTextures(1, &g_FontTextureId);
    glBindTexture(GL_TEXTURE_2D, g_FontTextureId);
    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, g_pText->format->BytesPerPixel, g_pText->w, g_pText->h, 0, textFormat, GL_UNSIGNED_BYTE, g_pText->pixels);

    // ...
}

DrawText()函数

// this function is called each frame
void DrawText()
{
    SDL_Rect sourceRect;
    sourceRect.x = 0;
    sourceRect.y = 0;
    sourceRect.h = 10;
    sourceRect.w = 173;

    // DestRect is null so the rect is drawn at 0,0
    SDL_BlitSurface(g_pText, &sourceRect, g_pSurfaceDisplay, NULL);

    glBindTexture(GL_TEXTURE_2D, g_FontTextureId);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBegin( GL_QUADS );

        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(0.0f, 0.0f);

        glTexCoord2f(0.0f, 1.0f);
        glVertex2f(0.0f, 10.0f);

        glTexCoord2f(1.0f, 1.0f);
        glVertex2f(173.0f, 10.0f);

        glTexCoord2f(1.0f, 0.0f);
        glVertex2f(173.0f, 0.0f);

    glEnd();
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
}

1 个答案:

答案 0 :(得分:4)

你犯了一个相当常见的错误。这是OpenGL的结尾。

当您在DrawText()中渲染纹理四边形时,您启用了OpenGL的混合功能,但您从未指定混合功能(即 应该如何混合)!

您需要此代码才能在OpenGL中启用常规Alpha混合:

glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

此信息曾经在OpenGL网站上,但我现在找不到它。

应该阻止它出现红色。其他人工作的原因是因为它们不是alpha混合的,它们实际上只是没有alpha的黑色红色图像,因此混合功能无关紧要。但混合的只包含红色,带有alpha通道使其不那么红。

我注意到你的程序中还有其他一些小问题。

DrawText()函数中,您使用SDL 使用OpenGL渲染表面。使用OpenGL时不应使用常规SDL blitting;它不起作用。所以这条线不应该在那里:

SDL_BlitSurface(g_pText, &sourceRect, g_pSurfaceDisplay, NULL);

此外,此行泄漏了内存:

g_pText = SDL_DisplayFormatAlpha( TTF_RenderUTF8_Blended(...) );

TTF_RenderUTF8_Blended()返回指向SDL_Surface的指针,该指针必须使用SDL_FreeSurface()释放。因为你将它传递给SDL_DisplayFormatAlpha(),所以你会忘记它,它永远不会被释放(因此内存泄漏)。

好消息是你在这里不需要SDL_DisplayFormatAlpha,因为TTF_RenderUTF8_Blended无论如何都会返回带有alpha通道的32位曲面!因此,您可以将此行重写为:

g_pText = TTF_RenderUTF8_Blended(g_pFont, "My first Text!", g_textColor);
相关问题