SFML库关于RenderTexture意外地被垂直翻转的问题

此文章用于帮助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()!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值