Pregunta

Como dije en esta pregunta , Estoy usando SDL para un pequeño juego que estoy desarrollando. Ahora tengo problemas con SDL_DisplayFormatAlpha. Estoy tratando de crear una superficie con un canal alfa a partir de una imagen PNG. Funcionaba antes, pero ahora que he hecho una pequeña refactorización, algo se rompió. Lo he reducido a este constructor:


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?

¿Fue útil?

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):

SDL_DisplayFormat

" 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?

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