Problema com SDL_DisplayFormatAlpha (c ++)
-
19-08-2019 - |
Pergunta
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 );
}
Isso compila bem, mas quando eu executá-lo eu recebo uma falha de segmentação. O erro relatado pelo 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
O rastreamento de pilha é o seguinte:
#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 eu comentar surface = SDL_DisplayFormatAlpha( tempSurface );
e SDL_FreeSurface( tempSurface );
e surface = tempSurface;
uncomment assim:
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 );
}
Em seguida, ele parece funcionar muito bem. Alguém pode me dizer o que está acontecendo? Na verdade, a transparência parece trabalho, também quando eu comentar SDL_DisplayFormatAlpha. É que a função só significou para ser usado com imagens que ainda não tem um canal alfa?
Solução
IMG_Load deve lidar transparente PNG da automaticamente, como o fim do seu post notas. O que é a exceção / erro real que está sendo jogado? O seu rastreamento de pilha não mostra isso.
Outras dicas
Se você ler o link aqui (função relacionada):
"Você tem que chamar SDL_Init antes de usar a função SDL_DisplayFormat. Se não o fizer, o programa irá falhar com uma violação de acesso."
Que poderia ser o seu problema?