如何使文字SDL2? [英] How to render text in SDL2?

查看:181
本文介绍了如何使文字SDL2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何呈现文本与SDL2。我发现所谓的SDL_TTF和一些教程一个API,但他们不与我的情况下工作。

I was wondering how to render text with SDL2. I found an API called SDL_TTF and some tutorials however they do not work with my situation.

我使用的是SDL_Window和SDL_Renderer,而教程是针对SDL_Surface。

I'm using an SDL_Window and SDL_Renderer, whereas the tutorials are specific to SDL_Surface.

是否有可能使用SDL_TTF与SDL_Render / SDL_Window?如果是这样,怎么样?

Is it possible to use SDL_TTF with SDL_Render/SDL_Window? If so, how?

推荐答案

是的,这是可能的,因为你有一个渲染器和一个窗口,再加上你真的没有与表面涉足任何想法,那么你可能要介意创造的质感,这里是一个示例code

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 understance

//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

这是一个工作code,我向你保证。
我试图解释由线code线,你看不到任何窗口在那里,因为我已经假定你知道如何初始化渲染器,这将使我一个想法,你也知道如何初始化的窗口,那么所有你需要的是如何初始化纹理的想法。

This is a working code, I assure you that. 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.

小问题在这里,你没打开的窗口?它是黑颜色的?这样的话我的想法是正确的,如果没有,那么你可以问我,我可以改变这个code来实现整个部分,它由一个渲染器和窗口。

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.

编码快乐! :)

这篇关于如何使文字SDL2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆