From e7790891cf0bd6211b71d084044324a6e95573d8 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Tue, 15 Oct 2024 14:11:17 +0200 Subject: Fixed crash when taking screenshot with scaled graphics Because the allocated surface was based on the unscaled size, writing the actual pixels was overwriting random memory. --- src/game.cpp | 23 ++++++++++++++--------- src/sdlgraphics.cpp | 20 ++++++++++++++------ 2 files changed, 28 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index 0bf2e48a..d2bd1430 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -86,7 +86,6 @@ #include #include -#include Joystick *joystick; @@ -286,10 +285,17 @@ static bool saveScreenshot() actorSpriteManager->updatePlayerNames(); } + if (!screenshot) + { + SERVER_NOTICE(_("Could not take screenshot!")) + logger->log("Error: could not take screenshot."); + return false; + } + // Search for an unused screenshot name - std::stringstream filenameSuffix; - std::stringstream filename; - std::fstream testExists; + std::ostringstream filenameSuffix; + std::ostringstream filename; + std::ifstream testExists; std::string screenshotDirectory = Client::getScreenshotDirectory(); bool found = false; @@ -305,12 +311,11 @@ static bool saveScreenshot() { screenshotCount++; filenameSuffix.str(std::string()); - filename.str(std::string()); - filename << screenshotDirectory << "/"; filenameSuffix << branding.getValue("appShort", "Mana") << "_Screenshot_" << screenshotCount << ".png"; - filename << filenameSuffix.str(); - testExists.open(filename.str().c_str(), std::ios::in); + filename.str(std::string()); + filename << screenshotDirectory << "/" << filenameSuffix.str(); + testExists.open(filename.str()); found = !testExists.is_open(); testExists.close(); } @@ -320,7 +325,7 @@ static bool saveScreenshot() if (success) { - std::stringstream chatlogentry; + std::ostringstream chatlogentry; // TODO: Make it one complete gettext string below chatlogentry << _("Screenshot saved as ") << filenameSuffix.str(); SERVER_NOTICE(chatlogentry.str()) diff --git a/src/sdlgraphics.cpp b/src/sdlgraphics.cpp index a2fc2268..d3ff72e2 100644 --- a/src/sdlgraphics.cpp +++ b/src/sdlgraphics.cpp @@ -236,13 +236,21 @@ SDL_Surface *SDLGraphics::getScreenshot() #endif int amask = 0x00000000; - SDL_Surface *screenshot = SDL_CreateRGBSurface(0, mWidth, - mHeight, 24, rmask, gmask, bmask, amask); + int width, height; + if (SDL_GetRendererOutputSize(mRenderer, &width, &height) != 0) + return nullptr; - SDL_RenderReadPixels(mRenderer, nullptr, - screenshot->format->format, - screenshot->pixels, - screenshot->pitch); + SDL_Surface *screenshot = SDL_CreateRGBSurface(0, width, height, 24, + rmask, gmask, bmask, amask); + + if (SDL_RenderReadPixels(mRenderer, nullptr, + screenshot->format->format, + screenshot->pixels, + screenshot->pitch) != 0) + { + SDL_FreeSurface(screenshot); + screenshot = nullptr; + } return screenshot; } -- cgit v1.2.3-70-g09d2