简体   繁体   English

切割sf :: Text,sf :: Texture等零件?

[英]Cutting of parts of sf::Text, sf::Texture, etc?

I'm creating a simple chat client and server and I'm having some issues with how sf::Text is rendered. 我正在创建一个简单的聊天客户端和服务器,并且在呈现sf :: Text时遇到一些问题。 Say in the chat history for example. 例如说在聊天记录中。 When users type their messages, the old messages will be pushed up. 用户键入消息时,旧消息将被上推。 Eventually the message hits the top and I'll want to only display the part of the message that is still within the chat history. 最终,该消息到达顶部,我只想显示仍然在聊天记录中的消息部分。 This is where I'm lost. 这是我迷路的地方。

The element containing the text has two visible objects - sf::RectangleShape (the background for the text) and a sf::Text. 包含文本的元素有两个可见的对象-sf :: RectangleShape(文本的背景)和一个sf :: Text。 I can easily adjust the size of the shape once it hits the top so it's not visible outside the chat history, but I can't adjust the size of the text without distorting the contents. 一旦它到达顶部,我就可以轻松调整形状的大小,因此在聊天记录之外不可见,但是在不扭曲内容的情况下,我无法调整文本的大小。 Changing the character size obviously isn't ideal either. 更改字符大小显然也不理想。

What I want is to cut the text in half horizontally if half of the text is outside the chat history. 我想要的是如果文本的一半不在聊天历史记录中,则将文本水平切成两半。 My first thought was to mask it somehow but it seems like SFML has no support for that. 我的第一个想法是以某种方式掩盖它,但是SFML似乎对此不支持。

The reason I'm mentioning sf::Texture in the title as well is because I've had the same issue with that in the past. 我在标题中也提到sf :: Texture的原因是因为过去我也遇到过同样的问题。 I just couldn't find a way to cut off parts of it, I only managed to distort it. 我只是找不到切断它的一部分的方法,只是设法使它变形。

Is this possible at all in SFML? SFML完全有可能吗?

One way of doing this is to draw the chat history to a sf::RenderTexture , which is the tool used for off-screen rendering. 一种方法是将聊天记录绘制到sf :: RenderTexture ,这是用于屏幕外渲染的工具。

Once you've drawn the chat history to your render texture, you can draw the texture on to the screen. 将聊天历史记录绘制到渲染纹理后,即可将纹理绘制到屏幕上。

So if you place text that is half on the render texture and half outside, only the part that actually fits on there will be drawn. 因此,如果将文本一半放在渲染纹理上,一半放在外部,则仅绘制实际适合该部分的部分。

Be aware though that render_texture are fairly expensive to construct, so you you want to re-use them and not create new ones all the time. 请注意,尽管render_texture的构建相当昂贵,所以您要重用它们而不是一直创建新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM