Il caricamento di una trama in SFML non riesce senza alcun messaggio sul perché

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

  •  29-07-2022
  •  | 
  •  

Domanda

Ho seguito i tutorial su SFML e ho provato a caricare una trama usando il seguente codice

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

Questo non riesce a caricare la trama, lo sprite è bianco che non è il colore del mio sprite ..

È stato utile?

Soluzione

Come tratto direttamente dal tutorial grafico-sprite sul sito Web 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." 

Pertanto, assicurati che la tua immagine sia prima denominata correttamente e secondo, assicurati che sia nella cartella corretta, ad esempio nella directory di lavoro.

Inoltre, se la trama non riesce a caricare invece di restituire uno sprite vuoto, è possibile segnalare un errore alla console e quindi lanciare un'eccezione. In questo modo ti verrà detto che lo sprite non si carica correttamente e il programma dovrà gestire l'eccezione altrimenti verrà terminata. In questo modo nessun sprite nel gioco dovrebbe avere una trama bianca a meno che non sia intenzionale

Qualcosa come questo:

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

Altri suggerimenti

Caricamento di un PNG? Fallo 8 bit. Altri formati PNG possono essere caricati ma sono sempre visualizzati come quadrati bianchi.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top