   [SOLVED] SDL blitting issue (Seg Fault)
  I am writing a game with SDL and C. I have one animated image being displayed on the screen. I want to display a background underneath it. This is, however, not working at all. Here is my code. The backdrop part is where it seg faults. I must be missing something simple.struct character{struct stats status;struct physics physics;struct image image;struct animation *left,*right,*up,*down,*active;};struct image{const char *fname;SDL_Surface *surface;};struct animation{struct image image;struct animation *next;};struct character Main;struct image backdrop;void gameInit(){loadImage("./images/c1_1.bmp", &Main.down->image);Main.active=Main.down;Main.physics.x=100;Main.physics.y=200;loadImage("./images/testbg.bmp",&backdrop);return;}void gameRender(){show(0,0,&backdrop);  //Seg Fault hereshow(Main.physics.x, Main.physics.y, &Main.active->image);Main.active=Main.active->next;display();return;}void show(double x, double y, struct image *obj){SDL_Rect src, dest; src.x = 0;src.y = 0;src.w = obj->surface->w;src.h = obj->surface->h; dest.x = x;dest.y = y;dest.w = obj->surface->w;dest.h = obj->surface->h;SDL_BlitSurface(obj->surface, &src, screen, &dest);return;}[Edited by - prushik on April 21, 2010 2:47:32 AM]
  What does the code for loadImage() look like?   ;Quote:Original post by 10essee_GuyWhat does the code for loadImage() look like?Oops, sorry. I meant to include that:void loadImage(const char *fname, struct image *obj){obj->fname=fname;SDL_Surface *temp = SDL_LoadBMP(fname);obj->surface = SDL_DisplayFormat(temp);SDL_FreeSurface(temp); //SDL_SetAlpha(obj->surface, SDL_SRCALPHA | SDL_RLEACCEL, 0xFF);SDL_SetColorKey(obj->surface, SDL_SRCCOLORKEY, SDL_MapRGB(obj->surface->format, 0x0, 0xFF, 0x0)); return;}   ;  I can't find anything wrong on the spot, this code look good. Is the image file missing from disk?Just before the segfault, try to call SDL_GetError() to see if you have something there. Also of course, check the value of both surface pointers to see if they are valid.The only time I had a crash using SDL_BlitSurface() was when, due to a bug in my engine, I was trying to blit the framebuffer surface on itself. Took a while to find out, but it don't seems to be the case here.Check the sizes of the src/dst rect, perhaps the backdrop image would be bigger than the framebuffer. How is this "screen" created, maybe something could be wrong with it (but I doubt it)...   ;Quote:Original post by DungeI can't find anything wrong on the spot, this code look good. Is the image file missing from disk?Just before the segfault, try to call SDL_GetError() to see if you have something there. Also of course, check the value of both surface pointers to see if they are valid.The only time I had a crash using SDL_BlitSurface() was when, due to a bug in my engine, I was trying to blit the framebuffer surface on itself. Took a while to find out, but it don't seems to be the case here.Check the sizes of the src/dst rect, perhaps the backdrop image would be bigger than the framebuffer. How is this "screen" created, maybe something could be wrong with it (but I doubt it)...The image is there. If I substitute c1_1.bmp (the image I know is there), it still seg faults. I places some printfs in the show() function (Thats where its crashing), and the last line is see printed is the line right beforesrc.w = obj->surface->w; Is there something wrong with this line? and if there is, why does it work for the other image?   ;  My guess is that obj->surface is NULL. Don't you know how to use a debugger? Since you are talking about segfault, you probably are on linux. Most visual IDE debuggers suck on linux, but learn to use GDB and you'll find the problem in less than a minute.Copy/Paste the result of this here:gdb ./program_namer*receive segfault*btprint objprint obj->surface   ;  Here is GDB output as you requested. It doesn't seem to show anything useful. Yes, I do believe that obj->surface is indeed NULL. I used printf to print out memory addresses:4198884:228640964198884:22864096is Main.active->image and Main.active->image.surface before and after its passed to show(). As you see they are the same.4198864:228636160:0is backdrop. As you can see, before it gets passed to show() its valid memory, but afterwards its 0.This GDB was configured as "x86_64-linux-gnu"...(gdb) rStarting program: /home/prushik/Sandbox/games/Lesser Evil/tmp [Thread debugging using libthread_db enabled][New Thread 0x7fb45164b6f0 (LWP 27409)]22863488::2286361622864512::2286409622863888::2286932822863920::2286956822863952::228790724198884:228640964198884:2286409612344198864:228636160:01Program received signal SIGSEGV, Segmentation fault.[Switching to Thread 0x7fb45164b6f0 (LWP 27409)]0x0000000000400cf0 in show ()Current language:  auto; currently asm(gdb) bt#0  0x0000000000400cf0 in show ()#1  0x0000000000400ff6 in gameRender ()#2  0x000000000040109b in main ()(gdb) print objNo symbol "obj" in current context.(gdb) print obj->surfaceNo symbol "obj" in current context.(gdb) print BG$1 = 0(gdb) q;  Ok. I am sorry, I did not compile with debugging symbols that time. Here is more output that confirms what you suspect:Program received signal SIGSEGV, Segmentation fault.[Switching to Thread 0x7f1f6a2a06f0 (LWP 28233)]0x0000000000400cf0 in show (x=200, y=200, obj={fname = 0x0, surface = 0x0})    at graphics_sdl.c:6666src.w = obj.surface->w;(gdb) print obj$1 = {fname = 0x0, surface = 0x0}(gdb) print obj.surface$2 = (SDL_Surface *) 0x0(gdb) ;  Problem solved. Although I am still baffled by the explanation. The answer is shocking and surprising. and has nothing to do with invalid pointers or my BG structure at all.Apparently, the C compiler isn't smart enough to turn 0 into 0.0 when being passed into a function that requires a double. Changing show(0,0,BG.active->image) to show(0.0,0.0,BG.active->image) solved the problem.   