【问题标题】:Rendering text with SDL2, problems updating texture / output使用 SDL2 渲染文本,更新纹理/输出时出现问题
【发布时间】:2020-04-16 12:24:26
【问题描述】:

我最近开始在一个 C++ 项目中使用 SDL2,该项目主要用作培训项目,因此我可以学习使用 SDL2。 我尝试过的一件事是编写一个简单的标签类。想法是在开始时使用给定的位置和尺寸创建标签。然后,当我调用我的“setText()”函数时,文本将被渲染并显示在窗口上。起初我使用这种方法:

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

这对我有用。所以我想消除每次文本更改时创建纹理的开销,然后销毁它们,并使用SDL_UpdateTexture()。 好吧,这从来没有奏效!即使调用成功,它也总是在屏幕上产生损坏的输出:

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

我的第一个想法是像素格式有问题......所以我在更新之前也使用了格式转换: m_surf = SDL_ConvertSurfaceFormat(ts, SDL_PIXELFORMAT_RGBA32, 0); 其中“ts”是渲染文本的临时表面,常量 SDL_PIXELFORMAT_RGBA32 与我用来创建 m_texture 的相同。

这是我在使用“SDL_UpdateTexture()”时在窗口上看到的内容

有什么想法吗?

谢谢你, 亚历克斯

代码片段: 我写了一段与重现问题相关的紧凑代码

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

【问题讨论】:

  • 也许只是RGB?这些颜色通道看起来发生了变化。
  • 嗨,我不确定我是否理解您的评论。如果你注意到了,我使用了SDL_ConvertSurfaceFormat() 来确保从文本渲染创建的表面与它要进入的纹理的像素格式相匹配。这是你的意思吗?
  • Surface 是在TTF_RenderText_Solid 调用中创建的,它的格式根本不需要匹配纹理格式。
  • 这也是我的怀疑,但在哪些方面他们不匹配?最终,它将是一个有效的 SDL2 Surface,因此由于 SDL_CreateTextureFromSurface() 正确处理它,我希望 SDL_UpdateTexture() 也能处理它,或者至少还有一个转换步骤,例如我在 SDL_UpdateTexture 之前调用 SDL_ConvertSurfaceFormat() 的那个()。我认为这错了吗?
  • 请提供minimal reproducible example。您可以假设可用的 SDL 库。

标签: c++ sdl-2 text-rendering


【解决方案1】:

我在 MacOS Catalina 和最新版本的 SDL2 和 SDL2_ttf 上测试了您的代码,发现分段错误。

调试后发现label::setText()中使用了质量参数,如果传递quality=2(使用TTF_RenderText_Blended()函数),最终会出现分段错误或垃圾像素。

但函数TTF_RenderText_Shaded() 和TTF_RenderText_Solid() 运行良好,我认为这与TTF_RenderText_Blended() 的表面具有透明度这一事实直接相关。

编辑:
在深入挖掘之后,我解决了我之前遇到的 分段错误,方法是替换了 rect 的 NULL 值:

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

按 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);

编辑 2: 我直接查看 SDL2 的源文件,以了解我的测试与函数 SDL_CreateTexture() 内部完成的代码之间的区别,您必须添加以下几行:

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

找到here我的原始调试文件(这不漂亮)

【讨论】:

  • 感谢您抽出宝贵时间尝试!我在 Linux 上运行,特别是 raspbian,最新的 raspberry(buster)操作系统。我没有收到段错误,但这在不同平台上可能是随机的,具体取决于内存损坏。这里的重要线索是,尽管质量设置,我总是得到完全相同的噪声模式。我尝试 quality = 0,1,2 。都产生相同的像素模式。
  • @Sagi2313 我修复了我遇到的分段错误,请参阅我的 anwser 中的编辑部分,结果不是很好,但我更接近 :)
  • @Sagi2313 我找到了解决方案,请参阅 Edit 2 部分:)
  • 你好,不幸的是,我仍然得到噪音而不是文字。我有点怀疑,因为在你的测试中,你从一开始就有质量 0,1,只是在 2 上遇到了麻烦。就我而言,不管质量值如何,我总是得到相同的输出。感谢您花时间在上面!
  • @Sagi2313 在我的调试文件中(可通过我的答案中的链接获得)我做了其他修改,如添加SDL_Init(SDL_INIT_VIDEO) 和TTF_Init(),请检查我的工作调试文件和您的版本之间的差异。我想你会成功的。
猜你喜欢
  • 1970-01-01
  • 2014-01-14
  • 2014-01-31
  • 1970-01-01
  • 2016-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多