Problema con SDL_DisplayFormatAlpha (c ++)
-
19-08-2019 - |
Pregunta
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 );
}
Esto se compila muy bien, pero cuando lo ejecuto me sale un error de segmentación. El error reportado por 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
El seguimiento de la pila es el siguiente:
#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
Si comento surface = SDL_DisplayFormatAlpha (tempSurface);
y SDL_FreeSurface (tempSurface);
y descomenta surface = tempSurface;
así:
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 );
}
Entonces parece funcionar bien. ¿Alguien puede decirme qué está pasando? En realidad, la transparencia parece funcionar también cuando comento SDL_DisplayFormatAlpha. ¿Esta función solo debe usarse con imágenes que aún no tienen un canal alfa?
Solución
IMG_Load debe manejar PNG transparentes automáticamente, como el final de sus notas de publicación. ¿Cuál es la excepción / error real que se genera? Su seguimiento de pila no muestra eso.
Otros consejos
Si lee el enlace aquí (función relacionada):
" Debe llamar a SDL_Init antes de usar la función SDL_DisplayFormat. Si no lo hace, su programa se bloqueará con una infracción de acceso. & Quot;
¿Podría ser ese tu problema?