Problema con SDL_DisplayFormatAlpha (c ++)
-
19-08-2019 - |
Domanda
Come ho affermato in questa domanda , Sto usando SDL per un piccolo gioco che sto sviluppando. Ora sto riscontrando problemi con SDL_DisplayFormatAlpha. Sto cercando di creare una superficie con un canale alfa da un'immagine PNG. Prima funzionava, ma ora che ho fatto qualche leggero refactoring qualcosa si è rotto. L'ho ridotto a questo costruttore:
Surface::Surface( tfilename file ) {
// initialize the surface data member to the image indicated by filename
SDL_Surface *tempSurface;
tempSurface = IMG_Load( file.c_str() );
if ( !tempSurface ) {
surface = NULL;
exit(1);
}
else {
surface = SDL_DisplayFormatAlpha( tempSurface );
//surface = tempSurface;
}
SDL_FreeSurface( tempSurface );
}
Questo si compila bene, ma quando lo eseguo ho un errore di segmentazione. L'errore segnalato da gdb:
Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb79c16c0 (LWP 8089)] 0xb7e8b9a3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0
La traccia dello stack è la seguente:
#0 0xb7e8b9a3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0 #1 0x0804987e in Surface (this=0x804d060, file=@0xbfb20760) at Surface.cpp:16 #2 0x0804a159 in Image (this=0x804d038, x=0, y=0, file=@0xbfb207a0) at Image.cpp:16 #3 0x0804a3de in Object (this=0x804d028, imageFile=@0xbfb207dc) at Object.cpp:4 #4 0x080491cb in Application (this=0xbfb20810) at Application.cpp:8 #5 0x08048e0d in main () at main.cpp:5
Se commento surface = SDL_DisplayFormatAlpha (tempSurface);
e SDL_FreeSurface (tempSurface);
e decommenta surface = tempSurface;
in questo modo:
Surface::Surface( tfilename file ) {
// initialize the surface data member to the image indicated by filename
SDL_Surface *tempSurface;
tempSurface = IMG_Load( file.c_str() );
if ( !tempSurface ) {
surface = NULL;
exit(1);
}
else {
//surface = SDL_DisplayFormatAlpha( tempSurface );
surface = tempSurface;
}
//SDL_FreeSurface( tempSurface );
}
Quindi sembra funzionare bene. Qualcuno può dirmi cosa sta succedendo? In realtà, la trasparenza sembra funzionare anche quando commento SDL_DisplayFormatAlpha. Questa funzione è pensata per essere utilizzata solo con immagini che non dispongono già di un canale alfa?
Soluzione
IMG_Load dovrebbe gestire automaticamente i PNG trasparenti, come la fine delle note del tuo post. Qual è l'eccezione / errore effettivo generato? La traccia dello stack non lo mostra.
Altri suggerimenti
Se leggi il link qui (funzione correlata):
" Devi chiamare SDL_Init prima di utilizzare la funzione SDL_DisplayFormat. In caso contrario, il programma si arresterà in modo anomalo con una violazione di accesso. & Quot;
Potrebbe essere questo il tuo problema?