Question

As said, I'm trying to display a text using a SDL_Surface converted in a texture here's my code :

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

which works very well with IMG_LOAD() but not with TTF_RenderText_Solid(), so I managed to find it by my self, at first I tought that the surface from TTF_RTS() came with not the good pixel mapping but I was able to find out, thanks to an home-made memory dump that it was the good one.

Was it helpful?

Solution

Ok I was missing 2 lines in my rendering loop:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_ALPHA);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top