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

标签 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_”为前缀的变量是全局变量。

初始化()函数

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

最佳答案

您犯了一个相当常见的错误。它在 OpenGL 端。

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

您需要此代码以在 OpenGL 中启用常规 alpha 混合:

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

这个信息曾经在 OpenGL 网站上,但我现在找不到了。

这应该会阻止它显示为纯红色。其他人工作的原因是因为它们不是 alpha 混合,它们实际上只是没有 alpha 的红黑图像,所以混合功能无关紧要。但是混合后的颜色只包含红色,带有 alpha channel 以使其不那么红。

不过,我注意到您的程序中还有其他一些小问题。

DrawText() 函数中,您正在使用 SDL 和 OpenGL 渲染来 blit 表面。使用 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 channel 的 32 位表面!所以你可以将这一行重写为:

g_pText = TTF_RenderUTF8_Blended(g_pFont, "My first Text!", g_textColor);

关于c++ - 使用 SDL_ttf 和 OpenGL,TTF_RenderUTF8_Blended 打印红色矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700085/

相关文章:

c++ - 为什么我们说#define Processor 在程序中创建多个拷贝?

C++ 调用模板函数错误

c++ - 在 gluBuild2DMipmaps 中将图像插入 openGL 时出错

c++ - 避免复制粘贴代码初始化结构中的一系列 SDL_Rect

c++ - SDL2 理解基本模型

c++ - 如何在不显式设置、不显式删除和不使用静态函数的情况下使对象指针为 NULL?

c++ - 将结构 vector 作为函数参数传递要求分配器

c++ - OpenGL 的 GLM 库没有幅度函数吗?

windows - 是否可以在没有 GL 加载程序库的情况下在 Windows 上链接到 OpenGL > 1.1?

c++ - 双 'button' SDL_Event 成员访问?