From: Ingo Ruhnke Date: Wed, 30 Jul 2014 06:02:57 +0000 (+0200) Subject: Implemented SDLRenderer::do_take_screenshot() X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=388740fd96dcb04267f253225c3fd68c00bb0156;p=supertux.git Implemented SDLRenderer::do_take_screenshot() --- diff --git a/src/video/sdl/sdl_renderer.cpp b/src/video/sdl/sdl_renderer.cpp index 53b55d3b4..798e17721 100644 --- a/src/video/sdl/sdl_renderer.cpp +++ b/src/video/sdl/sdl_renderer.cpp @@ -604,29 +604,66 @@ void SDLRenderer::do_take_screenshot() { // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it? - - SDL_Surface *screen = SDL_GetWindowSurface(window); - - // save screenshot - static const std::string writeDir = PHYSFS_getWriteDir(); - static const std::string dirSep = PHYSFS_getDirSeparator(); - static const std::string baseName = "screenshot"; - static const std::string fileExt = ".bmp"; - std::string fullFilename; - for (int num = 0; num < 1000; num++) { - std::ostringstream oss; - oss << baseName; - oss << std::setw(3) << std::setfill('0') << num; - oss << fileExt; - std::string fileName = oss.str(); - fullFilename = writeDir + dirSep + fileName; - if (!PHYSFS_exists(fileName.c_str())) { - SDL_SaveBMP(screen, fullFilename.c_str()); - log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl; - return; + int width; + int height; + if (SDL_GetRendererOutputSize(renderer, &width, &height) != 0) + { + log_warning << "SDL_GetRenderOutputSize failed: " << SDL_GetError() << std::endl; + } + else + { +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + Uint32 rmask = 0xff000000; + Uint32 gmask = 0x00ff0000; + Uint32 bmask = 0x0000ff00; + Uint32 amask = 0x000000ff; +#else + Uint32 rmask = 0x000000ff; + Uint32 gmask = 0x0000ff00; + Uint32 bmask = 0x00ff0000; + Uint32 amask = 0xff000000; +#endif + SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 32, + rmask, gmask, bmask, amask); + if (!surface) + { + log_warning << "SDL_CreateRGBSurface failed: " << SDL_GetError() << std::endl; + } + else + { + int ret = SDL_RenderReadPixels(renderer, NULL, + SDL_PIXELFORMAT_ABGR8888, + surface->pixels, + surface->pitch); + if (ret != 0) + { + log_warning << "SDL_RenderReadPixels failed: " << SDL_GetError() << std::endl; + } + else + { + // save screenshot + static const std::string writeDir = PHYSFS_getWriteDir(); + static const std::string dirSep = PHYSFS_getDirSeparator(); + static const std::string baseName = "screenshot"; + static const std::string fileExt = ".bmp"; + std::string fullFilename; + for (int num = 0; num < 1000; num++) { + std::ostringstream oss; + oss << baseName; + oss << std::setw(3) << std::setfill('0') << num; + oss << fileExt; + std::string fileName = oss.str(); + fullFilename = writeDir + dirSep + fileName; + if (!PHYSFS_exists(fileName.c_str())) { + SDL_SaveBMP(surface, fullFilename.c_str()); + log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl; + return; + } + } + log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl; + } } } - log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl; } void