代码之家  ›  专栏  ›  技术社区  ›  Xan

为什么将图像传输到纹理对象对我不起作用?

  •  -1
  • Xan  · 技术社区  · 1 年前

    我看了关于如何使用SFML的指南,可以清楚地看到它将图像文件传输到纹理对象。我得到以下错误:“没有合适的用户定义的从“sf::image”到“const std::string”的转换。”

    Image heroimage; 
    heroimage.loadFromFile("images/hero.png");
    
    Texture herotexture;
    herotexture.loadFromFile(heroimage);
    
    1 回复  |  直到 1 年前
        1
  •  -1
  •   Moorse    1 年前

    在SFML中,使用sf::Texture::loadFromFile方法从文件中加载纹理,该方法需要文件路径作为字符串

    sf::Texture texture;
    if (!texture.loadFromFile("path/to/image.png")) {
        // Handle error
    }
    

    如果您试图使用sf::Image对象来创建纹理,则需要改用sf::texture::loadFromImage。以下是一个示例:

    sf::Image image;
    if (!image.loadFromFile("path/to/image.png")) {
        // Handle error
    }
    
    sf::Texture texture;
    if (!texture.loadFromImage(image)) {
        // Handle error
    }
    

    确保在您的代码中,您使用了正确的方法来处理您所拥有的数据类型(sf::Image或文件路径)。如果要使用文件路径,请确保将字符串传递给loadFromFile,而不是sf::Image对象。

    推荐文章