SFMLにテクスチャをロードすると、その理由についてメッセージがなく失敗します

StackOverflow https://stackoverflow.com/questions/19855730

  •  29-07-2022
  •  | 
  •  

質問

私はSFMLのチュートリアルに従って、次のコードを使用してテクスチャをロードしようとしました

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

これはテクスチャをロードできません。スプライトは白で、私のスプライトの色ではありません。

役に立ちましたか?

解決

SFML Webサイトのグラフィックスプライトチュートリアルから直接取られているように

"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をロードしますか? 8bitにします。他のPNG形式はロードできますが、常に白い正方形として表示されます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top