直接从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");
}