我最近使用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