旋转纹理 sfml 并将输出保存到文件



我想首先说我是一个完整的 c++ 新手。我不知道任何与之相关的词汇,所以如果这个问题微不足道,我很抱歉,因为我现在没有正确的术语或短语来自己谷歌解决方案。

目前正在使用我在 github 上找到的这段代码来捕获我的 3ds 中的游戏玩法。https://github.com/Gotos/Cute3DSCapture/blob/master/main.cpp

我正在尝试添加旋转图像并将其保存到 png 文件的功能。

从第 267 行周围的main.cpp 我正在尝试添加以下功能。

sprite.setTexture(texture);
sprite.rotate(90);
texture = sprite.getTexture();
texture.copyToImage().saveToFile("Something/Place/img.png");

当前纹理和子画面定义如下。

sf::Texture texture;
sf::Sprite sprite;

当我尝试构建和运行时,我得到以下内容

main.cpp:269:25: error: no viable overloaded '='
                texture = sprite.getTexture();
                ~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~
/usr/local/include/SFML/Graphics/Texture.hpp:421:14: note: candidate function
      not viable: no known conversion from 'const sf::Texture *' to
      'const sf::Texture' for 1st argument; dereference the argument with *
    Texture& operator =(const Texture& right);

任何帮助将不胜感激。

sf::Sprite不是纹理操纵器,这意味着sf::Sprite::getTexture()返回的纹理不会被修改。这就是为什么精灵的 ctor 会引用const纹理实例的原因。

如果您只对图像处理感兴趣,我建议您使用 SFML 以外的其他方法,因为您可能会为特定操作获得更好的功能/性能。可能是像图像魔术一样的东西。

使用 SFML,您可以大致像这样操作:

  • 创建一个所需大小的RenderTexture - 将其视为一个虚拟窗口,您可以在调用.display()时访问其纹理。
  • 使用初始纹理和一些操作(例如旋转(创建精灵 - 确保渲染纹理的大小正确设置为"显示"生成的图像
  • 在渲染纹理上绘制精灵并在其上调用display()
  • 然后你可以调用sf::RenderTexture::getTexture()并链接copyToImage()saveToFile()

查看sf::RenderTexture的详细描述以获取使用示例。

最新更新