Pregunta

He estado siguiendo los tutoriales en SFML e intenté cargar una textura utilizando el siguiente código

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

Esto no puede cargar la textura, el sprite es blanco, que no es el color de mi sprite.

¿Fue útil?

Solución

Como se toma directamente del tutorial de Sprite Graphics en el sitio web de 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." 

Por lo tanto, asegúrese de que su imagen se denomine en primer lugar correctamente y se asegure en segundo lugar de que esté en la carpeta correcta, es decir, en su directorio de trabajo.

Además, si la textura no se carga en lugar de devolver un sprite vacío, podría informar un error a la consola y luego lanzar una excepción. De esta manera, se le dirá que el sprite no se carga correctamente y el programa tendrá que manejar la excepción de lo contrario se terminará. De esta manera, ningún sprite en el juego debe tener una textura blanca a menos que sea intencional

Algo como esto:

sf::Texture texture;
if (!texture.loadFromFile("image.png"))
{
    throw std::runtime_error("Could not load image.png");
}

Otros consejos

¿Cargando un PNG? Hazlo 8 bits. Se pueden cargar otros formatos PNG, pero siempre se muestran como cuadrados blancos.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top