2017-06-22 32 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