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

标签 c++ opengl sfml

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

目前正在研究我在 github 上找到的这段代码,用于从我的 3ds 中捕捉游戏玩法。 https://github.com/Gotos/Cute3DSCapture/blob/master/main.cpp

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

main.cpp 的第 267 行开始,我尝试添加以下功能。

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

当前贴图和 Sprite 定义如下。

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 以外的东西,因为您可能会为特定操作获得更好的特性/性能。可能类似于 imagemagick。

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

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

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

关于c++ - 旋转纹理 sfml 并将输出保存到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35386108/

相关文章:

c - 关于 glPixelStorei() 对齐的问题

c++ - 渲染到帧缓冲区/纹理显示为空

c++ - 使用 OpenGL 在 SFML 中获取正确的鼠标位置

c++ - SFML 获取全屏模式

C++ Boost::MPL fold 示例 - 参数数量错误

c++ - 如何递归调用以结构数组为参数的函数

opengl - 使用 IBO 制作动画——好还是坏?

linux - Linux 上的 SFML 锁定为 60 Hz?

c++ - Doxygen - 如何在没有换行符的情况下结束注意力 block

c++ - 在 C++ 中实现多变量正态 pdf 以进行图像分类