简体   繁体   English

SFML 2.1设置所有文本

[英]SFML 2.1 set all text

Is there any way to set all the text I have or some specified text all to the same font, color, etc? 有什么办法可以将我拥有的所有文字或某些指定的文字全部设置为相同的字体,颜色等?
It would be a lot faster and easier to do that, instead of doing this: 这样做会更快,更轻松,而不是这样做:

text1.setColor(sf::Color::Black);
text2.setColor(sf::Color::Black);
text3.setColor(sf::Color::Black);
...

I'm using SFML 2.1 in C++98. 我在C ++ 98中使用SFML 2.1。

If the Text instances are this similar, it might make sense to keep them in a std::vector or some other container class. 如果Text实例与此相似,则将它们保留在std::vector或其他某个容器类中可能是有意义的。 If they can be sensibly placed in such a container, you can simply loop through them and change whatever properties you wish: 如果可以将它们合理地放置在这样的容器中,则可以简单地遍历它们并更改所需的任何属性:

for (std::vector<sf::Text>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
{
    it->setColor(sf::Color::Black);
    it->setFont(myfont);
}

Edit given interest in C++11 in comments: 在注释中编辑对C ++ 11的兴趣:

In C++11 this becomes even simpler as a result of automatic type deduction and range-based for loops . 在C ++ 11中,由于自动类型推断基于范围的for循环,这变得更加简单。 The syntax above simplifies to: 上面的语法简化为:

for (auto& text : myvector)            //don't miss the & or 'text' will be read-only!
{
    text.setColor(sf::Color::Black);
    text.setFont(myfont);
}

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

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