From 1c1dc345de886576984bb21a1775d93a31ed0336 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sat, 20 Apr 2024 15:34:30 +0200 Subject: [PATCH] Codechange: simplify access to the current screenshot format --- src/screenshot.cpp | 35 +++++++++++++++-------------------- src/screenshot.h | 2 -- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/screenshot.cpp b/src/screenshot.cpp index 6a591afcc2..0f54a89cae 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -38,8 +38,6 @@ static const char * const SCREENSHOT_NAME = "screenshot"; ///< Default filename static const char * const HEIGHTMAP_NAME = "heightmap"; ///< Default filename of a saved heightmap. std::string _screenshot_format_name; ///< Extension of the current screenshot format (corresponds with #_cur_screenshot_format). -uint _num_screenshot_formats; ///< Number of available screenshot formats. -uint _cur_screenshot_format; ///< Index of the currently selected screenshot format in #_screenshot_formats. static std::string _screenshot_name; ///< Filename of the screenshot file. std::string _full_screenshot_path; ///< Pathname of the screenshot file. uint _heightmap_highest_peak; ///< When saving a heightmap, this contains the highest peak on the map. @@ -575,24 +573,26 @@ static const ScreenshotFormat _screenshot_formats[] = { {"pcx", &MakePCXImage}, }; +/* The currently loaded screenshot format. Set to a valid value as it might be used in early crash logs, when InitializeScreenshotFormats has not been called yet. */ +static const ScreenshotFormat *_cur_screenshot_format = std::begin(_screenshot_formats); + /** Get filename extension of current screenshot file format. */ const char *GetCurrentScreenshotExtension() { - return _screenshot_formats[_cur_screenshot_format].extension; + return _cur_screenshot_format->extension; } /** Initialize screenshot format information on startup, with #_screenshot_format_name filled from the loadsave code. */ void InitializeScreenshotFormats() { - uint j = 0; - for (uint i = 0; i < lengthof(_screenshot_formats); i++) { - if (_screenshot_format_name.compare(_screenshot_formats[i].extension) == 0) { - j = i; - break; + for (auto &format : _screenshot_formats) { + if (_screenshot_format_name == format.extension) { + _cur_screenshot_format = &format; + return; } } - _cur_screenshot_format = j; - _num_screenshot_formats = lengthof(_screenshot_formats); + + _cur_screenshot_format = std::begin(_screenshot_formats); } /** @@ -706,8 +706,7 @@ static const char *MakeScreenshotName(const char *default_fn, const char *ext, b /** Make a screenshot of the current screen. */ static bool MakeSmallScreenshot(bool crashlog) { - const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format; - return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension, crashlog), CurrentScreenCallback, nullptr, _screen.width, _screen.height, + return _cur_screenshot_format->proc(MakeScreenshotName(SCREENSHOT_NAME, _cur_screenshot_format->extension, crashlog), CurrentScreenCallback, nullptr, _screen.width, _screen.height, BlitterFactory::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette); } @@ -804,8 +803,7 @@ static bool MakeLargeWorldScreenshot(ScreenshotType t, uint32_t width = 0, uint3 Viewport vp; SetupScreenshotViewport(t, &vp, width, height); - const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format; - return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension), LargeWorldCallback, &vp, vp.width, vp.height, + return _cur_screenshot_format->proc(MakeScreenshotName(SCREENSHOT_NAME, _cur_screenshot_format->extension), LargeWorldCallback, &vp, vp.width, vp.height, BlitterFactory::GetCurrentBlitter()->GetScreenDepth(), _cur_palette.palette); } @@ -852,8 +850,7 @@ bool MakeHeightmapScreenshot(const char *filename) _heightmap_highest_peak = std::max(h, _heightmap_highest_peak); } - const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format; - return sf->proc(filename, HeightmapCallback, nullptr, Map::SizeX(), Map::SizeY(), 8, palette); + return _cur_screenshot_format->proc(filename, HeightmapCallback, nullptr, Map::SizeX(), Map::SizeY(), 8, palette); } static ScreenshotType _confirmed_screenshot_type; ///< Screenshot type the current query is about to confirm. @@ -937,8 +934,7 @@ static bool RealMakeScreenshot(ScreenshotType t, std::string name, uint32_t widt break; case SC_HEIGHTMAP: { - const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format; - ret = MakeHeightmapScreenshot(MakeScreenshotName(HEIGHTMAP_NAME, sf->extension)); + ret = MakeHeightmapScreenshot(MakeScreenshotName(HEIGHTMAP_NAME, _cur_screenshot_format->extension)); break; } @@ -1019,6 +1015,5 @@ static void MinimapScreenCallback(void *, void *buf, uint y, uint pitch, uint n) */ bool MakeMinimapWorldScreenshot() { - const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format; - return sf->proc(MakeScreenshotName(SCREENSHOT_NAME, sf->extension), MinimapScreenCallback, nullptr, Map::SizeX(), Map::SizeY(), 32, _cur_palette.palette); + return _cur_screenshot_format->proc(MakeScreenshotName(SCREENSHOT_NAME, _cur_screenshot_format->extension), MinimapScreenCallback, nullptr, Map::SizeX(), Map::SizeY(), 32, _cur_palette.palette); } diff --git a/src/screenshot.h b/src/screenshot.h index a243050e56..f10a80f70d 100644 --- a/src/screenshot.h +++ b/src/screenshot.h @@ -32,8 +32,6 @@ bool MakeScreenshot(ScreenshotType t, std::string name, uint32_t width = 0, uint bool MakeMinimapWorldScreenshot(); extern std::string _screenshot_format_name; -extern uint _num_screenshot_formats; -extern uint _cur_screenshot_format; extern std::string _full_screenshot_path; #endif /* SCREENSHOT_H */