我最近使用SDL2编写了一个小型文本冒险游戏,并且遇到了换行问题。我正在使用TTF_RenderText_Blended_Wrapped()来渲染我的字符串,这给我一些很好的包装线。但是线条高度是一个问题,线条似乎在一起挤压,字母'jqg'与字母'tli'重叠。SDL TTF - 包装线的换行高度?
有谁知道是否有办法改变行高? TTF_RenderText_Blended_Wrapped()仍然不在SDL_ttf的文档中。我应该写我自己的文字包装功能吗?
字体大小为16pt,样式为TTF_STYLE_BOLD,字体可以找到here。下面的代码应该重现错误,虽然几乎没有错误检查,但要自担风险。这里是代码的输出:
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main(int argc, char *argv[]) {
SDL_Window *gui;
SDL_Surface *screen, *text;
SDL_Event ev;
TTF_Font *font;
int running = 1;
const char *SAMPLETEXT = "This is an example of my problem, for most lines it works fine, albeit it looks a bit tight. But for any letters that \"hang\" below the line, there is a chance of overlapping with the letters below. This isn\'t the end of the world, but I think it makes for rather cluttered text.\n\nNotice the p and k on line 1/2, and the g/t on 2/3 and 3/4.";
// init SDL/TTF
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
// Open and set up font
font = TTF_OpenFont("Anonymous.ttf", 16);
if(font == NULL) {
fprintf(stderr, "Error: font could not be opened.\n");
return 0;
}
TTF_SetFontStyle(font, TTF_STYLE_BOLD);
// Create GUI
gui = SDL_CreateWindow("Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
640, 480, SDL_WINDOW_SHOWN);
// Grab GUI surface
screen = SDL_GetWindowSurface(gui);
// Clear screen black
SDL_FillRect(screen, NULL, 0);
// Draw some text to screen
SDL_Color color = {0xff, 0xff, 0xff, 0xff};
text = TTF_RenderText_Blended_Wrapped(font, SAMPLETEXT, color, screen->w);
SDL_BlitSurface(text, NULL, screen, NULL);
while(running) { // Main loop
while(SDL_PollEvent(&ev)) {
switch(ev.type){
case SDL_QUIT:
running = 0;
break;
}
}
SDL_UpdateWindowSurface(gui); // Refresh window
SDL_Delay(20); // Delay loop
}
// Destroy resources and quit
TTF_CloseFont(font);
TTF_Quit();
SDL_FreeSurface(text);
SDL_DestroyWindow(gui);
SDL_Quit();
return 0;
}
我看不出有什么办法来控制行距。 [这里是源代码的链接](https://fossies.org/dox/SDL2_ttf-2.0.14/SDL__ttf_8c_source.html#l01876)。你尝试过不同的字体吗?你比较过'TTF_FontHeight()'和'TTF_FontLineSkip()'返回的值吗? –
感谢您的链接。线跳过大于高度,我尝试了几个字体。我确定有一个字体可以工作,但我宁愿不去寻找。 –
你可以发布你使用的字体吗?我在我的系统上尝试了一些字体,但无法重现问题。 – Tim