Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with SDL_DisplayFormatAlpha (c++)
    text
    copied!<p>As I stated in <a href="https://stackoverflow.com/questions/341192/segmentation-fault-using-sdl-with-c-trying-to-blit-images">this question</a>, I am using SDL for a small game I'm developing. Now I am having problems with SDL_DisplayFormatAlpha. I am trying to create a surface with an alpha channel from a PNG image. It was working before, but now that I've done some slight refactoring something got broken. I've narrowed it down to this constructor:</p> <pre><code> 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 ); } </code></pre> <p>This compiles just fine, but when I run it I get a Segmentation fault. The error reported by gdb:</p> <pre> Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb79c16c0 (LWP 8089)] 0xb7e8b9a3 in SDL_DisplayFormatAlpha () from /usr/lib/libSDL-1.2.so.0 </pre> <p>The stack trace is as follows:</p> <pre> #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 </pre> <p>If I comment out <code>surface = SDL_DisplayFormatAlpha( tempSurface );</code> and <code>SDL_FreeSurface( tempSurface );</code> and uncomment <code>surface = tempSurface;</code> like so:</p> <pre><code> 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 ); }</code></pre> <p>Then it seems to work just fine. Can anyone tell me what's going on? Actually, the transparency seems to work, too when I comment out SDL_DisplayFormatAlpha. Is that function only meant to be used with images that do not already have an alpha channel?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload