이유에 대한 메시지 없이 SFML에서 텍스처 로드가 실패합니다.

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

  •  29-07-2022
  •  | 
  •  

문제

나는 SFML에 대한 튜토리얼을 따르고 있으며 다음 코드를 사용하여 텍스처를 로드하려고 시도했습니다.

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

텍스처를 로드하지 못했습니다. 스프라이트가 흰색이어서 내 스프라이트의 색상이 아닙니다.

도움이 되었습니까?

해결책

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

다른 팁

PNG로드? 8 비트로 만드십시오. 다른 PNG 형식은로드 될 수 있지만 항상 흰색 사각형으로 표시됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top