我一直在遵循SFML上的教程,并尝试使用以下代码加载纹理

sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
    return sf::Texture();
}

这无法加载纹理,精灵是白色的,这不是我的精灵的颜色。

有帮助吗?

解决方案

直接从SFML网站上的Graphics-Sprite教程中获取

"The loadFromFile function sometimes fails with no obvious reason. First, check the error message printed by SFML in the
standard output (check the console). If the message is unable to open file, make sure that the working directory (which is the
directory any file path will be interpreted relatively to) is what you think it is: when you run the application from the explorer, the
working directory is the executable folder, but when you launch your program from your IDE (Visual Studio, Code::Blocks, ...) the
working directory is sometimes set to the project directory instead. This can generally be changed easily in the project settings." 

因此,请确保您的图像首先正确命名,并次要确保其在您的工作目录中的正确文件夹中。

同样,如果纹理无法加载而不是返回空的精灵,则可以向控制台报告错误,然后投掷异常。这样,您将被告知Sprite无法正确加载,并且该程序将必须处理异常,否则将终止。这样,除非有意

这样的事情:

sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
    throw std::runtime_error("Could not load image.png");
}

其他提示

加载PNG?使其8位。可以加载其他PNG格式,但始终显示为白色正方形。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top