【发布时间】: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