C语言 如何在 SDL2 中呈现文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22886500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to render text in SDL2?
提问by Ethan Webster
I was wondering how to render text with SDL2. I found an API called SDL_TTFand some tutorials, however they do not work with my situation.
我想知道如何使用 SDL2 呈现文本。我发现了一个 APISDL_TTF和一些教程,但是它们不适用于我的情况。
I'm using an SDL_Windowand SDL_Renderer, whereas the tutorials are specific to SDL_Surface.
我正在使用SDL_Windowand SDL_Renderer,而教程特定于SDL_Surface.
Is it possible to use SDL_TTFwith SDL_Render/SDL_Window? If so, how?
是否有可能使用SDL_TTF与SDL_Render/SDL_Window?如果是这样,如何?
回答by kdyz
Yep, it is possible, given that you have a renderer and a window plus you don't really have any thoughts on dabbling with surfaces then you might want to mind on creating texture, here is a sample code
是的,这是可能的,因为你有一个渲染器和一个窗口,而且你真的没有任何涉足表面的想法,那么你可能想要创建纹理,这里是一个示例代码
TTF_Font* Sans = TTF_OpenFont("Sans.ttf", 24); //this opens a font style and sets a size
SDL_Color White = {255, 255, 255}; // this is the color in rgb format, maxing out all would give you the color white, and it will be your text's color
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(Sans, "put your text here", White); // as TTF_RenderText_Solid could only be used on SDL_Surface then you have to create the surface first
SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); //now you can convert it into a texture
SDL_Rect Message_rect; //create a rect
Message_rect.x = 0; //controls the rect's x coordinate
Message_rect.y = 0; // controls the rect's y coordinte
Message_rect.w = 100; // controls the width of the rect
Message_rect.h = 100; // controls the height of the rect
//Mind you that (0,0) is on the top left of the window/screen, think a rect as the text's box, that way it would be very simple to understand
//Now since it's a texture, you have to put RenderCopy in your game loop area, the area where the whole code executes
SDL_RenderCopy(renderer, Message, NULL, &Message_rect); //you put the renderer's name first, the Message, the crop size(you can ignore this if you don't want to dabble with cropping), and the rect which is the size and coordinate of your texture
//Don't forget to free your surface and texture
SDL_FreeSurface(surfaceMessage);
SDL_DestroyTexture(Message);
I tried to explain the code line by line, you don't see any window right there since I already assumed that you knew how to initialize a renderer which would give me an idea that you also know how to initialize a window, then all you need is the idea on how to initialize a texture.
我试图逐行解释代码,你没有在那里看到任何窗口,因为我已经假设你知道如何初始化渲染器,这会让我知道你也知道如何初始化窗口,然后你所有的需要的是如何初始化纹理的想法。
Minor questions here, did your window open? was it colored black? if so then my thoughts were right, if not, then you can just ask me and I could change this code to implement the whole section which consists of a renderer and a window.
小问题在这里,你的窗户打开了吗?它是黑色的吗?如果是这样,那么我的想法是对的,如果不是,那么您可以问我,我可以更改此代码以实现由渲染器和窗口组成的整个部分。
回答by jpw
Yes it is. You create a surface with the text you want and then convert it to a texture that you can render.
是的。您可以使用所需的文本创建一个表面,然后将其转换为可以渲染的纹理。
Some sample code from one of my projects:
我的一个项目中的一些示例代码:
std::string score_text = "score: " + std::to_string(score);
SDL_Color textColor = { 255, 255, 255, 0 };
SDL_Surface* textSurface = TTF_RenderText_Solid(font, score_text.c_str(), textColor);
SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, textSurface);
int text_width = textSurface->w;
int text_height = textSurface->h;
SDL_FreeSurface(textSurface);
SDL_Rect renderQuad = { 20, win_height - 30, text_width, text_height };
SDL_RenderCopy(renderer, text, NULL, &renderQuad);
SDL_DestroyTexture(text);
This assumes you've properly initialized SDL_ttf and loaded a font. In the example scoreis an int. The screen gets cleared and rendered to somewhere else (I didn't include that part).
这假设您已正确初始化 SDL_ttf 并加载了字体。在示例中score是一个 int。屏幕被清除并渲染到其他地方(我没有包括那部分)。
For a full working example, check out the tutorial for SDL_ttf in SDL2 at Lazy Foo.
有关完整的工作示例,请查看Lazy Foo 中 SDL2 中的 SDL_ttf 教程。
回答by Bruno Myrrha
Since there are some people struggling with more complex code, I've included my own snippet here to help some beginners like myself. This will just show a red screen with a black hello world. Don't forget to add -lsdl2 and -lsdl2_ttf on your build and include the Verdana.ttf font on the same folder.
由于有些人在为更复杂的代码而苦苦挣扎,因此我在此处包含了自己的代码段,以帮助像我这样的初学者。这将只显示一个带有黑色 hello world 的红色屏幕。不要忘记在您的构建中添加 -lsdl2 和 -lsdl2_ttf,并将 Verdana.ttf 字体包含在同一文件夹中。
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
const char* TITLE = "Hello World SDL2 + TTF";
const int WIDTH = 1280, HEIGHT = 720;
int main() {
SDL_Window *window = SDL_CreateWindow(TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Event windowEvent;
TTF_Init();
TTF_Font *verdanaFont = TTF_OpenFont("Verdana.ttf", 128);
SDL_Color textColor = { 0, 0, 0, 255 };
SDL_Surface *textSurface = TTF_RenderText_Solid(verdanaFont, "Hello World", textColor);
SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_Rect textRect;
textRect.x = WIDTH - textSurface->w * 0.5;
textRect.y = HEIGHT - textSurface->h * 0.5;
textRect.w = textSurface->w;
textRect.h = textSurface->h;
SDL_FreeSurface(textSurface);
TTF_Quit();
bool isRunning = true;
while (isRunning) {
while(SDL_PollEvent(&windowEvent)) {
switch (windowEvent.type) {
case SDL_QUIT:
isRunning = false;
break;
}
}
SDL_RenderCopy(renderer, textTexture, NULL, &textRect);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(textTexture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}

