SDL_SaveBMP() problems

Started by
3 comments, last by sparkx 18 years, 7 months ago
I'm writing a software to visualize volume-data with SDL/OpenGL on linux, and I thought that it would be nice to have a function to take a screenshot of the window. It happens that calling SDL_SaveBMP(app_screen "screenshot.bmp") where: SDL_Surface *app_screen is a valid (checked not NULL) pointer to the whole-screen surface, I always get a segmentation fault. SDL version is 1.2.something (debian sid), Anyone experiencing this? Marco
Advertisement
There could be a few problems.

1. The screen surface could be locked. Try unlocking it.
2. You don't have access to write the bitmap. (check permissions)
Try saving to "./screenshot.bmp" instead of "screenshot.bmp"

Try to see what it returns. SDL_SaveBMP() returns 0 if successful or -1 if there is an error. Hopefully you can get the result before it crashes. [Edit: If the image wasn't created then it probly didn't work.]

if(SDL_SaveBMP(app_screen, "./screenshot.bmp") != 0){    std::cerr << "SDL_SaveBMP() Failed." << std::endl;}


Personally I would add a time stamp or look up my OS's filesystem code and increment a number on the screenshot file name. ei. "screenshot#.bmp"
first, thanks for your help :)

the actual code was like yours:
.../* grabbing the s key */if (keysym.sym == SDLK_s)  {    if (SDL_SaveBMP(app_screen, "screen.bmp") != 0)      {        fprintf(stderr, "--> Failed to take screenshot\n");      }    else      {        printf("--> Screenshot saved to screen.bmp\n");      }  }...


My output is only: "Fatal signal: Segmentation Fault (SDL Parachute Deployed)" I never get the return value, so I think that the segfault happens during the execution of SDL_SaveBMP()
When I will be proficient in gdb...

I tried locking first the surface, nothing.
I tried unlocking... the same.


Permissions are ok, an empty screen.bmp file is created.
:)

... i was thinking like you about the auto-incremented filename, actually something more than a counter... maybe the coords of the plane showed within the volume or something else...

Thanks Anyway :)
Marco
If you're using OpenGL you can't use SDL_SaveBMP to make a screenshot. In this case you have to read the framebuffer yourself and write the contents to a file.
Here's an example I found on this board
void Screendump(char *destFile, short W, short H) { FILE   *out = fopen(destFile, "w"); char   pixel_data[3*W*H]; short  TGAhead[] = {0, 2, 0, 0, 0, 0, W, H, 24}; glReadBuffer(GL_FRONT); glReadPixels(0, 0, W, H, GL_BGR, GL_UNSIGNED_BYTE, pixel_data); fwrite(&TGAhead, sizeof(TGAhead), 1, out); fwrite(pixel_data, 3*W*H, 1, out); fclose(out); }/*       ______________________________________________________________   creates a 24-bit uncompressed true color tga-file, width W and height   H. for a complete description of tga go to www.wotsit.org. if you use   a doublebuffered configuration, be sure to swap buffers before you   call the screendump function.*/
baumep
It seems so :)
I just wrongly assumed that the screen was just another SDL_Surface while initializing SDL to provide the OpenGL context.

Thank you very much indeed.



This topic is closed to new replies.

Advertisement