使用在纹理中转换的SDL_Surface显示文本

时间:2013-05-16 09:16:52

标签: opengl textures sdl true-type-fonts geometry-surface

如上所述,我正在尝试使用在纹理中转换的SDL_Surface来显示文本,这是我的代码:

if (SDL_Init(SDL_INIT_VIDEO) == 0) {

    //Create a surface
    switch (method) {
    case 2: // load a texture with SDL_Image:
    {
        surface = IMG_Load("../../../../../data/box3.png");
    }
        break;
    case 3: // load a texture with SDL_TTF:

        SDL_Color textColor={ 255, 255, 0, 1 };
        if (TTF_Init() == 0){;
            TTF_Font *font;
            font = TTF_OpenFont("../../../../../data/Bedizen.ttf", 20);
            if (font != NULL){
                qDebug() << TTF_FontFaceFamilyName(font);
                surface = TTF_RenderText_Solid(font, ".....", textColor );
            }
            else
                qDebug() << "Error (Font) : " << TTF_GetError();
        }
        else
            qDebug() << "Error (Font) : " << TTF_GetError();

        break;
    }

    if (surface != NULL){
        GLint  nbOfColors;
        GLenum texture_format = 0;

        qDebug("surface : %dx%d / %dbpp / %d", surface->w, surface->h,
                       surface->format->BytesPerPixel, surface->pitch);

        MemoryDump(surface->pixels, surface->pitch, surface->h, surface->format->BytesPerPixel);

        // get the number of channels in the SDL surface
        nbOfColors = surface->format->BytesPerPixel;

        switch (nbOfColors) {
        case 1:
            texture_format = GL_ALPHA;
            break;
        case 3:     // no alpha channel
            if (surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGB;
            else
                texture_format = GL_BGR;
            break;
        case 4:     // contains an alpha channel
            if (surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGBA;
            else
                texture_format = GL_BGRA;
            break;
        default:
            qDebug() << "Warning: the image is not truecolor...";
            break;
        }

        glEnable( GL_TEXTURE_2D );
        // Have OpenGL generate a texture object handle for us
        glGenTextures( 1, &texture );

        // Bind the texture object
        glBindTexture( GL_TEXTURE_2D, texture );

        // Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

        // Edit the texture object's image data using the information SDL_Surface gives us
        glTexImage2D( GL_TEXTURE_2D, 0, nbOfColors, surface->w, surface->h, 0,
                              texture_format, GL_UNSIGNED_BYTE, surface->pixels );
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    }
    else{
        qDebug() << "Error (SDL) : " << SDL_GetError();
    }
}

适用于IMG_LOAD()但不适用于TTF_RenderText_Solid(),所以我设法自己找到它,起初我认为TTF_RTS()的表面没有良好的像素映射,但我能够通过自制的内存转储找出它是好的。

1 个答案:

答案 0 :(得分:0)

好的我在渲染循环中缺少2行:

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