使用 SDL 2 显示文本的高度和宽度问题

Problem with height and width for displaying Text with SDL 2

我在 SDL2 中显示文本时遇到问题。 Message_rect.w = 100 和 Message_rect.h = 100 的文本看起来很奇怪。我手动更改它,但它不是自动的。你知道我怎样才能自动知道文本的完美高度和宽度吗?

这是我的实际代码:

TTF_Font* Sans = TTF_OpenFont("asset/arial.ttf", 24);
SDL_Color White = {255, 255, 255, 0};
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(Sans, "put your text here", White);
SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
SDL_Rect Message_rect;
Message_rect.x = 600;
Message_rect.y = 200;
Message_rect.w = 100;
Message_rect.h = 100;
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(Message);

我找到了答案,我只需要将 Message_rect 提供给 'SDL_GetClipRect' 函数即可。 所以现在的代码是:

TTF_Font* Sans = TTF_OpenFont("asset/arial.ttf", 24);
SDL_Color White = {255, 255, 255, 0};
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(Sans, "This is a good test", White);
SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
SDL_Rect Message_rect;
SDL_GetClipRect(surfaceMessage, &Message_rect);
Message_rect.x = 600;
Message_rect.y = 200;
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(Message);