我的 SDL_ttf 呈现的文本最终被拉伸。我该如何避免这种情况?
My SDL_ttf rendered text ends up streched. How do I avoid that?
这是我如何呈现 SDL 文本的最小示例:
#include <SDL_ttf.h>
void runttf()
{
constexpr auto SCREEN_WIDTH{300};
constexpr auto SCREEN_HEIGHT{300};
constexpr auto font_path = "/usr/share/fonts/truetype/fonts-beng-extra/MuktiNarrow.ttf"; //any font on your system
constexpr SDL_Rect destination = {10,10,200,80};
constexpr SDL_Color text_color={0,0,0};
SDL_Init( SDL_INIT_VIDEO );
TTF_Init();
auto window = SDL_CreateWindow( "SDL TTF demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
auto renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
auto font = TTF_OpenFont( font_path, 28 );
auto textSurface = TTF_RenderText_Solid( font, "I wish I this would not stretch", text_color );
auto textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Event e;
auto run = true;
while(run) {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, textTexture, NULL, &destination);
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&e) != 0) if (e.type == SDL_QUIT) run = false;
}
SDL_DestroyTexture(textTexture);
SDL_FreeSurface( textSurface );
TTF_CloseFont(font);
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
TTF_Quit();
SDL_Quit();
}
当我渲染文本时,我最终得到这样的结果
您可以看到我绘制文本的方式如何使它延伸到我想在其中绘制文本的选定区域。我宁愿让文本显示被裁剪掉或类似的东西 - 除了拉伸。
我是不是用错了函数?我是否需要对文本的渲染和放置做更多的数学计算?呈现非拉伸文本的规范 sdl ttf 方式是什么?
您将 destination
矩形设置为不同于渲染到表面的文本,因此它会拉伸。
您还应该在主循环之前释放表面。
所以我认为您正在寻找的代码:
#include <SDL_ttf.h>
void runttf()
{
constexpr auto SCREEN_WIDTH{ 300 };
constexpr auto SCREEN_HEIGHT{ 300 };
constexpr auto font_path = "/usr/share/fonts/truetype/fonts-beng-extra/MuktiNarrow.ttf"; //any font on your system
constexpr SDL_Color text_color = { 0,0,0 };
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
auto window = SDL_CreateWindow("SDL TTF demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
auto renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
auto font = TTF_OpenFont(font_path, 28);
auto textSurface = TTF_RenderText_Solid(font, "I wish I this would not stretch", text_color);
auto textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect destination;
destination.w = textSurface->w;
destination.h = textSurface->h;
destination.x = 0;
destination.y = 0;
SDL_FreeSurface(textSurface);
SDL_Event e;
auto run = true;
while (run) {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, textTexture, NULL, &destination);
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&e) != 0) if (e.type == SDL_QUIT) run = false;
}
SDL_DestroyTexture(textTexture);
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
}
否则我认为代码看起来不错。如果您不在乎外观而在乎性能,则可以使用 TTF_RenderText_Blended
而不是 TTF_RenderText_Solid
。
这是我如何呈现 SDL 文本的最小示例:
#include <SDL_ttf.h>
void runttf()
{
constexpr auto SCREEN_WIDTH{300};
constexpr auto SCREEN_HEIGHT{300};
constexpr auto font_path = "/usr/share/fonts/truetype/fonts-beng-extra/MuktiNarrow.ttf"; //any font on your system
constexpr SDL_Rect destination = {10,10,200,80};
constexpr SDL_Color text_color={0,0,0};
SDL_Init( SDL_INIT_VIDEO );
TTF_Init();
auto window = SDL_CreateWindow( "SDL TTF demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
auto renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
auto font = TTF_OpenFont( font_path, 28 );
auto textSurface = TTF_RenderText_Solid( font, "I wish I this would not stretch", text_color );
auto textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Event e;
auto run = true;
while(run) {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, textTexture, NULL, &destination);
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&e) != 0) if (e.type == SDL_QUIT) run = false;
}
SDL_DestroyTexture(textTexture);
SDL_FreeSurface( textSurface );
TTF_CloseFont(font);
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
TTF_Quit();
SDL_Quit();
}
当我渲染文本时,我最终得到这样的结果
您可以看到我绘制文本的方式如何使它延伸到我想在其中绘制文本的选定区域。我宁愿让文本显示被裁剪掉或类似的东西 - 除了拉伸。
我是不是用错了函数?我是否需要对文本的渲染和放置做更多的数学计算?呈现非拉伸文本的规范 sdl ttf 方式是什么?
您将 destination
矩形设置为不同于渲染到表面的文本,因此它会拉伸。
您还应该在主循环之前释放表面。
所以我认为您正在寻找的代码:
#include <SDL_ttf.h>
void runttf()
{
constexpr auto SCREEN_WIDTH{ 300 };
constexpr auto SCREEN_HEIGHT{ 300 };
constexpr auto font_path = "/usr/share/fonts/truetype/fonts-beng-extra/MuktiNarrow.ttf"; //any font on your system
constexpr SDL_Color text_color = { 0,0,0 };
SDL_Init(SDL_INIT_VIDEO);
TTF_Init();
auto window = SDL_CreateWindow("SDL TTF demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
auto renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
auto font = TTF_OpenFont(font_path, 28);
auto textSurface = TTF_RenderText_Solid(font, "I wish I this would not stretch", text_color);
auto textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect destination;
destination.w = textSurface->w;
destination.h = textSurface->h;
destination.x = 0;
destination.y = 0;
SDL_FreeSurface(textSurface);
SDL_Event e;
auto run = true;
while (run) {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, textTexture, NULL, &destination);
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&e) != 0) if (e.type == SDL_QUIT) run = false;
}
SDL_DestroyTexture(textTexture);
TTF_CloseFont(font);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
SDL_Quit();
}
否则我认为代码看起来不错。如果您不在乎外观而在乎性能,则可以使用 TTF_RenderText_Blended
而不是 TTF_RenderText_Solid
。