2017-06-22 103 views
2

我最近使用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; 
} 
+0

我看不出有什么办法来控制行距。 [这里是源代码的链接](https://fossies.org/dox/SDL2_ttf-2.0.14/SDL__ttf_8c_source.html#l01876)。你尝试过不同的字体吗?你比较过'TTF_FontHeight()'和'TTF_FontLineSkip()'返回的值吗? –

+0

感谢您的链接。线跳过大于高度,我尝试了几个字体。我确定有一个字体可以工作,但我宁愿不去寻找。 –

+0

你可以发布你使用的字体吗?我在我的系统上尝试了一些字体,但无法重现问题。 – Tim

回答

1

最简单的解决办法是找到不具有该问题的字体。该FreeMono font有更多的间距:

using the FreeMono.ttf

从看source code for TTF_RenderUTF8_Blended_Wrapped,这是由TTF_RenderText_Blended_Wrapped叫,也没有设置行间距配置的方式。请参阅1893行。

但是,尽管lineSpace设置为2,但它在计算要渲染的每个像素的地址时未使用。这是该行间距有效设置为0,我报出这是SDL_ttf库中的缺陷:https://bugzilla.libsdl.org/show_bug.cgi?id=3679

我能够与以下更改解决问题SDL_ttf 2.0.14:

--- a/SDL_ttf.c Fri Jan 27 17:54:34 2017 -0800 
+++ b/SDL_ttf.c Thu Jun 22 16:54:38 2017 -0700 
@@ -1996,7 +1996,7 @@ 
     return(NULL); 
    } 

- rowSize = textbuf->pitch/4 * height; 
+ rowSize = textbuf->pitch/4 * (height + lineSpace); 

    /* Adding bound checking to avoid all kinds of memory corruption errors 
     that may occur. */ 

随着应用上述补丁,您的示例程序显示了正确的符合Anonymous font间距:

correct line spacing with patched SDL_ttf