From a55ea5d152d57fe422d6f7db38d13b8a0d2ee77d Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Sun, 7 Jun 2020 13:22:36 +0100 Subject: [PATCH] SDL: Simplify code for taking screenshots --- .../surfacesdl/surfacesdl-graphics.cpp | 73 +++++-------------- 1 file changed, 19 insertions(+), 54 deletions(-) diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp index 00a81717368..d3982e2703f 100644 --- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp +++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp @@ -30,6 +30,7 @@ #include "common/textconsole.h" #include "common/translation.h" #include "common/util.h" +#include "common/file.h" #include "common/frac.h" #ifdef USE_RGB_COLOR #include "common/list.h" @@ -42,8 +43,9 @@ #include "gui/debugger.h" #include "gui/EventRecorder.h" #ifdef USE_PNG -#include "common/file.h" #include "image/png.h" +#else +#include "image/bmp.h" #endif #ifdef USE_TTS #include "common/text-to-speech.h" @@ -439,11 +441,17 @@ OSystem::TransactionError SurfaceSdlGraphicsManager::endGFXTransaction() { } Graphics::PixelFormat SurfaceSdlGraphicsManager::convertSDLPixelFormat(SDL_PixelFormat *in) const { - return Graphics::PixelFormat(in->BytesPerPixel, + Graphics::PixelFormat out(in->BytesPerPixel, 8 - in->Rloss, 8 - in->Gloss, 8 - in->Bloss, 8 - in->Aloss, in->Rshift, in->Gshift, in->Bshift, in->Ashift); + + // Workaround to SDL not providing an accurate Aloss value on some platforms. + if (in->Amask == 0) + out.aLoss = 8; + + return out; } #ifdef USE_RGB_COLOR @@ -548,10 +556,6 @@ void SurfaceSdlGraphicsManager::detectSupportedFormats() { // Get our currently set hardware format Graphics::PixelFormat hwFormat = convertSDLPixelFormat(_hwScreen->format); - // Workaround to SDL not providing an accurate Aloss value on Mac OS X. - if (_hwScreen->format->Amask == 0) - hwFormat.aLoss = 8; - _supportedFormats.push_back(hwFormat); #if !SDL_VERSION_ATLEAST(2, 0, 0) @@ -1405,69 +1409,30 @@ bool SurfaceSdlGraphicsManager::saveScreenshot(const Common::String &filename) c assert(_hwScreen != NULL); Common::StackLock lock(_graphicsMutex); -#ifdef USE_PNG + Common::DumpFile out; if (!out.open(filename)) { return false; } -#if SDL_VERSION_ATLEAST(2, 0, 0) - SDL_Surface *rgbScreen = SDL_ConvertSurfaceFormat(_hwScreen, SDL_PIXELFORMAT_RGB24, 0); -#else - // This block of code was taken mostly as-is from SDL 1.2's SDL_SaveBMP_RW - SDL_Surface *rgbScreen = SDL_CreateRGBSurface(SDL_SWSURFACE, - _hwScreen->w, - _hwScreen->h, - 24, -#ifdef SCUMM_LITTLE_ENDIAN - 0x0000FF, 0x00FF00, 0xFF0000, -#else - 0xFF0000, 0x00FF00, 0x0000FF, -#endif - 0); - if (rgbScreen == nullptr) { - warning("Could not create RGB24 surface"); - return false; - } - - SDL_Rect bounds; - bounds.x = bounds.y = 0; - bounds.w = _hwScreen->w; - bounds.h = _hwScreen->h; - if (SDL_LowerBlit(_hwScreen, &bounds, rgbScreen, &bounds) < 0) { - SDL_FreeSurface(rgbScreen); - rgbScreen = nullptr; - } -#endif - - if (rgbScreen == nullptr) { - warning("Could not convert hardware surface to RGB24"); - return false; - } - - int result = SDL_LockSurface(rgbScreen); + int result = SDL_LockSurface(_hwScreen); if (result < 0) { warning("Could not lock RGB surface"); - SDL_FreeSurface(rgbScreen); return false; } -#ifdef SCUMM_LITTLE_ENDIAN - const Graphics::PixelFormat format(3, 8, 8, 8, 0, 0, 8, 16, 0); -#else - const Graphics::PixelFormat format(3, 8, 8, 8, 0, 16, 8, 0, 0); -#endif + Graphics::PixelFormat format = convertSDLPixelFormat(_hwScreen->format); Graphics::Surface data; - data.init(rgbScreen->w, rgbScreen->h, rgbScreen->pitch, rgbScreen->pixels, format); + data.init(_hwScreen->w, _hwScreen->h, _hwScreen->pitch, _hwScreen->pixels, format); +#ifdef USE_PNG const bool success = Image::writePNG(out, data); +#else + const bool success = Image::writeBMP(out, data); +#endif - SDL_UnlockSurface(rgbScreen); - SDL_FreeSurface(rgbScreen); + SDL_UnlockSurface(_hwScreen); return success; -#else - return SDL_SaveBMP(_hwScreen, filename.c_str()) == 0; -#endif } void SurfaceSdlGraphicsManager::setFullscreenMode(bool enable) {