在SFML中加载纹理失败,没有关于原因的消息

时间:2013-11-08 09:37:15

标签: sfml

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

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

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

2 个答案:

答案 0 :(得分:1)

直接来自SFML网站上的图形精灵教程

"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." 

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

此外,如果纹理无法加载而不是返回空的精灵,您可以向控制台报告错误然后抛出异常。 这样你就会被告知精灵没有正确加载,程序必须处理异常,否则它将被终止。这样游戏中就不会有精灵 除非故意

,否则会有白色纹理

这样的事情:

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

答案 1 :(得分:-1)

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