我一直在关注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."
因此,请确保首先正确命名映像,然后确保其位于正确的文件夹中,即在您的工作目录中。
同样,如果纹理加载失败,而不是返回空的精灵,则可以向控制台报告错误,然后引发异常。这样,您将被告知精灵未正确加载,程序将不得不处理异常,否则它将被终止。这样,除非有意,否则游戏中的任何精灵都不应该具有白色纹理
像这样的东西:
sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
throw std::runtime_error("Could not load image.png");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句