diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-10-15 14:11:17 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-10-15 14:11:17 +0200 |
commit | e7790891cf0bd6211b71d084044324a6e95573d8 (patch) | |
tree | 026b52e746853ccb724a9580353c687513285876 /src | |
parent | 53833f3f145dd1d937d9e7754c03ffadb3db15e8 (diff) | |
download | mana-e7790891cf0bd6211b71d084044324a6e95573d8.tar.gz mana-e7790891cf0bd6211b71d084044324a6e95573d8.tar.bz2 mana-e7790891cf0bd6211b71d084044324a6e95573d8.tar.xz mana-e7790891cf0bd6211b71d084044324a6e95573d8.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 23 | ||||
-rw-r--r-- | src/sdlgraphics.cpp | 20 |
2 files changed, 28 insertions, 15 deletions
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 <fstream> #include <sstream> -#include <string> 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; } |