SDL Saving window as BMP

2019-09-19 20:12发布

问题:

I am writing a program in SDL and C and I want to be able to save the window as an image.

This is my code:

screen = SDL_GetWindowSurface(win);
SDL_SaveBMP(screen,"screen");

But when I execute it I get:

Segmentation Fault

From other sources I gather that its about pointers and memory access. Any help?

回答1:

Call SDL_LockSurface on a window surface before saving bitmap, and SDL_UnlockSurface after that.



回答2:

    SDL_Surface *sshot = SDL_CreateRGBSurface(0, 750, 750, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
    SDL_RenderReadPixels(renderer, NULL, SDL_PIXELFORMAT_ARGB8888, sshot->pixels, sshot->pitch);
    SDL_SaveBMP(sshot, "screenshot.bmp");
    SDL_FreeSurface(sshot);