SDL Saving window as BMP

2019-09-19 20:13发布

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?

2条回答
放我归山
2楼-- · 2019-09-19 20:26
    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); 
查看更多
萌系小妹纸
3楼-- · 2019-09-19 20:39

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

查看更多
登录 后发表回答