SDL2 C++ Taking a screenshot

后端 未结 2 1956
后悔当初
后悔当初 2020-12-14 21:19

Hi I would like to know if it is possible to simply take a screenshot with SDL2. I tried SDL_GetWindowSurface but I get an error saying:

2条回答
  •  悲哀的现实
    2020-12-14 21:41

    It seems like you are mixing the rendering systems. That method will only work in the context of software rendering. For hardware rendering you should use the method SDL_RenderReadPixels(). To save the screenshot you would need a code like that:

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

    Where w and h are the screen width and height (you can get these values using SDL_GetRendererOutputSize()).

提交回复
热议问题