2

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

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

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

4

2 回答 2

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");
}
于 2013-11-08T09:38:24.697 回答
-1

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

于 2013-11-08T09:42:38.383 回答