Sdl_ttf文本换行

Sdl_ttf Text Wrapping

本文关键字:换行 文本 ttf Sdl      更新时间:2023-10-16

当一串文本超过长度300时,我希望它下降到新行并继续。我读到这就是你的做法,但它不起作用 - 它只是拉伸文本。

如何实现这一点?

const char* message = "example text to test that it drops to a new line."; 
std::string fontFile = "Font/font.ttf";
int fontSize = 16;
TTF_Font *font = nullptr;
font = TTF_OpenFont(fontFile.c_str(), fontSize);
SDL_Color textColor = { 0, 300, 200 };
SDL_Surface *surf = TTF_RenderText_Blended_Wrapped(font, message, textColor, 300);
texture = SDL_CreateTextureFromSurface(m_p_Renderer, surf);
int w,h;
TTF_SizeText(font,message,&w,&h);
srcRect.x = 0;
srcRect.y = 0;
destRect.x = 0;
destRect.y = 0;
srcRect.w =w;
srcRect.h = h;
destRect.w =w;
destRect.h = h;

A 认为错误出在TTF_SizeText(...)调用上。它不知道您设置的边界,也可能忽略端点。尝试删除该行并放置这些:

int w, h;
w = surf->w;
h = surf->h;

我希望它有所帮助。