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

Rotate texture sfml and save output to file

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

目前正在使用我在 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()返回的纹理不会被修改。这就是为什么 Sprite 的构造函数引用 const 纹理实例。

如果您只对图像处理感兴趣,我建议您使用 SFML 以外的东西,因为您可能会更好地 features/performance 进行特定操作。可能像 imagemagick.

使用SFML,你可以大致像这样:

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

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