简体   繁体   中英

Rendering text with SDL2, problems updating texture / output

I have recently started using SDL2 in a C++ project, which is mainly used as a training project so I can learn using SDL2. One of the things I tried, was to write a simple label class. The Idea is that the label is created with given position and dimensions at start. Then when I call my 'setText()' function, the text would be rendered and shown on window. At first I used this approach:

// get a surface from given text    
m_surf = TTF_RenderText_Solid(m_font, txt.c_str(), fgCol);
// get a texture from the previous surface  
m_texture = SDL_CreateTextureFromSurface(r, m_surf);
// render it in 'r' which is the window renderer
SDL_RenderCopy(r, m_texture, NULL, &rect);

This works for me. So I thought to remove the overhead of creating textures every time text changes, then destroy them, and use SDL_UpdateTexture() . Well, this never worked! It always produces a corrupt output on screen, even though the call is successful:

// Once at ctor create a texture
m_texture = SDL_CreateTexture(r, SDL_PIXELFORMAT_RGBA32,SDL_TEXTUREACCESS_STATIC, rect.w, rect.h);
// ...
// later update when setText() is called get a surface from given text and update texture   
m_surf = TTF_RenderText_Solid(m_font, txt.c_str(), fgCol);
rc = SDL_UpdateTexture(m_texture,  NULL, m_surf->pixels, m_surf->pitch);

My first thought was that it's something wrong with the pixel formatting... so I also used a format convert before updating: m_surf = SDL_ConvertSurfaceFormat(ts, SDL_PIXELFORMAT_RGBA32, 0); where 'ts' is a temp surface where the text was rendered, and constant SDL_PIXELFORMAT_RGBA32 is same as the one I used to create the m_texture.

here is what I get on window when using 'SDL_UpdateTexture()'损坏的输出

Any Ideas ?

Thank you, Alex

Code Snip: I have written a compact piece of code that is related to reproduce problem

// label class declaration
class label
{
    friend class panel;
    public:
        label(SDL_Window* const w, TTF_Font* const fnt, SDL_Rect rect);
        ~label();

        int draw(SDL_Rect& rect);
        int draw(SDL_Rect& rect,const std::string& txt, SDL_Color fgCol, int quality);

    private:
    std::string     m_txt;
    TTF_Font* const m_font = nullptr;
    SDL_Surface     *m_surf = nullptr;
    SDL_Texture     *m_texture = nullptr;
    SDL_Rect        m_rect{0,0,0,0};
    SDL_Renderer    *m_ren;
    SDL_Window      *m_wnd;
    Uint32          m_pixForm;
};

// label class constructor
label::label(SDL_Window* const w, TTF_Font* const fnt, SDL_Rect rect):m_wnd(w),m_font(fnt),m_surf (nullptr), m_rect(rect)
{
// save window info locally for label object
    m_ren =SDL_GetRenderer(m_wnd);
    m_pixForm =SDL_GetWindowPixelFormat(m_wnd);
    m_texture = SDL_CreateTexture(m_ren, m_pixForm,SDL_TEXTUREACCESS_STATIC, rect.w, rect.h);
    m_surf = nullptr;
    std::cout << "PixelFormat " << m_pixForm << " : " << SDL_GetPixelFormatName(m_pixForm) <<  std::endl;
}

// label class methods to render/draw text on screen
int label::setText(const std::string& txt, SDL_Color fgCol, int quality=0)
{
    int w,h;
    SDL_Surface *ts;
    if(m_surf!=nullptr)SDL_FreeSurface(m_surf);
    switch(quality)
    {
        case 1:
        {
            ts = TTF_RenderText_Shaded(m_font, txt.c_str(), fgCol, SDL_Color{0,0,0,0});
            break;
        }
        case 2:
        {
            ts = TTF_RenderText_Blended(m_font,txt.c_str(), fgCol);
            break;
        }
        default:
        {
            ts = TTF_RenderText_Solid(m_font, txt.c_str(), fgCol);
            break;
        }
    }
    std::cout << "Before pixFormat ts " << SDL_GetPixelFormatName(ts->format->format)<< std::endl;
    m_surf =  SDL_ConvertSurfaceFormat(ts, m_pixForm, 0);
    std::cout << "After pixFormat surf : " <<  SDL_GetPixelFormatName(m_surf->format->format) << " ts " << SDL_GetPixelFormatName(ts->format->format)<< std::endl;
    SDL_FreeSurface(ts);
    TTF_SizeText(m_font, txt.c_str(), &w, &h);
    std::cout << "Set text '" << txt << "' w: " << w << " h: " << h << std::endl;
}

int label::draw(SDL_Rect& rect)
{
    // HERE is the one that draw the noise on screen!
    {
        int rc;
        rc = SDL_UpdateTexture(m_texture,  NULL, m_surf->pixels, m_surf->pitch);
        if(rc)
        {
            std::cout << "Error updating texture " << rc <<std::endl;
            return(rc);
        }
    }
    // if it is replaced with the following it works:
    /*
    {
        if(m_texture!=nullptr)SDL_DestroyTexture(m_texture);
        m_texture = SDL_CreateTextureFromSurface(r, m_surf);
    }
    */

    SDL_RenderCopy(m_ren, m_texture, NULL, &rect);
}

int label::draw(SDL_Rect& rect,const std::string& txt, SDL_Color fgCol, int q)
{
    setText(txt,fgCol,q);
    draw(rect);
}

// main functiom. Init SDL, create window, create label and draw it:
int main( int argc, char * argv[] )
{
    SDL_Window *wnd ;
    SDL_Renderer *wrend;
    int i;

    wnd = SDL_CreateWindow("TestWin",SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,500,300,0);
    wrend = SDL_CreateRenderer(wnd,-1,SDL_RENDERER_ACCELERATED);
    SDL_SetRenderDrawBlendMode(wrend,SDL_BLENDMODE_ADD);
    SDL_SetRenderDrawColor(wrend,0,0,0,255);
    SDL_RenderClear(wrend);

    SDL_Color fcol{255,255,0,128};
    SDL_Rect lblBox;
    lblBox.h = 120;
    lblBox.w = 280;
    lblBox.x = 1;
    lblBox.y = 1;

    label lbl(wnd, getCurrentFont(), lblBox); // create a label (getCurrentFont returns a valid TTF_Font pointer)
    lbl.draw(lblBox,std::string("Text Test"), fcol, 2);
    SDL_RenderPresent(wrend);
    SDL_ShowWindow(wnd);
    // .. more code here
}

I tested your code on MacOS Catalina and last version of SDL2 and SDL2_ttf and had segmentation fault .

After debugging I found that in label::setText() you use a quality parameter and if you pass quality=2 (to use the function TTF_RenderText_Blended() ), you will end up with a segmentation fault or garbage pixels.

But functions TTF_RenderText_Shaded() and TTF_RenderText_Solid() work well, I think this is directly linked to the fact that with TTF_RenderText_Blended( ) you have transparency in your surface.

Edit:
After digging a bit more I solved the segmentation fault I had previously by replacing the NULL value for the rect in line :

 rc = SDL_UpdateTexture(m_texture,  NULL, m_surf->pixels, m_surf->pitch);

by the size of m_surf:

SDL_Rect SurfRect;

SurfRect.x = 0;
SurfRect.y = 0;
SurfRect.w = m_surf->w;
SurfRect.h = m_surf->h;
...
rc = SDL_UpdateTexture(m_texture, SurfRect, m_surf->pixels, m_surf->pitch);

Edit 2 : I directly look into the source file of the SDL2 to understand what was the difference between my tests and the code done internally by the function SDL_CreateTexture() and you have to add these lines :

int rc = SDL_UpdateTexture(m_texture, &SurfRect, m_surf->pixels, m_surf->pitch);
{
    Uint8 r, g, b, a;
    SDL_BlendMode blendMode;
    SDL_GetSurfaceColorMod(m_surf, &r, &g, &b);
    SDL_SetTextureColorMod(m_texture, r, g, b);

    SDL_GetSurfaceAlphaMod(m_surf, &a);
    SDL_SetTextureAlphaMod(m_texture, a);

    if (SDL_HasColorKey(m_surf)) {
        /* We converted to a texture with alpha format */
        SDL_SetTextureBlendMode(m_texture, SDL_BLENDMODE_BLEND);
    } else {
        SDL_GetSurfaceBlendMode(m_surf, &blendMode);
        SDL_SetTextureBlendMode(m_texture, blendMode);
    }
}

TTF_RenderText_Blended() 的结果 Find here my raw debug file (this isn't pretty)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM