此文章用于帮助SFML学习者解决一些问题,给后人分享经验
SFML版本3.0.0
问题
使用sf::RenderTexture时,发现如下代码片段运行后,显示的图像意外地被垂直翻转
sf::Texture t("1.png");
sf::Sprite s(t);
s.setPosition({ 100, 100 });
sf::RenderTexture r(w.getSize());
r.clear(sf::Color::Transparent);
r.draw(s);
w.draw(sf::Sprite(r.getTexture()));
w.display();
解决
在翻阅SFML文档后,并没有发现关于这个现象的记录
于是,我搜索SFML论坛,在这个帖子里发现了有关这个问题的记录
clear() is optional, it’s just a shorcut for filling the whole texture and avoid leftovers from a previous frame. But if you want to keep the old content on purpose, or if you cover the entire texture with the entities you draw, then you don’t need it.
display() is mandatory, on some platforms it does nothing, but on others you may end up with a flipped texture (for example).
翻译后
clear()是可选的,它只是填充整个纹理的快捷方式,避免了前一帧的残留物。但是,如果你想故意保留旧内容,或者用你绘制的实体覆盖整个纹理,那么你就不需要它。
display()是必需的,在某些平台上它什么也不做,但在其他平台上,你可能会得到一个翻转的纹理(也许)。
所以,display()并不是表面意思“显示”,而更像是“结束绘制并生成图像”函数,如果不使用display()图像会出现问题
任何draw()在使用后一定要记得用display()!!!