Files
OpenRCT2-Web/src/openrct2/interface/Screenshot.cpp
T

654 lines
20 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2015-12-29 13:50:31 +00:00
2017-06-08 23:02:39 +01:00
#include "Screenshot.h"
2014-10-06 17:36:58 +01:00
2018-06-22 22:59:46 +02:00
#include "../Context.h"
2024-07-09 16:23:31 +02:00
#include "../Diagnostic.h"
2017-11-30 18:17:06 +01:00
#include "../Game.h"
#include "../GameState.h"
2018-06-22 22:59:46 +02:00
#include "../OpenRCT2.h"
#include "../PlatformEnvironment.h"
2023-01-02 14:19:46 +01:00
#include "../actions/CheatSetAction.h"
2025-02-17 07:24:03 -05:00
#include "../audio/Audio.h"
2024-05-08 22:29:32 +02:00
#include "../config/Config.h"
2018-06-22 22:59:46 +02:00
#include "../core/Console.hpp"
2024-12-15 17:39:52 +01:00
#include "../core/EnumUtils.hpp"
#include "../core/File.h"
2018-06-22 22:59:46 +02:00
#include "../core/Imaging.h"
2022-02-26 17:20:07 +01:00
#include "../core/Path.hpp"
2023-07-02 12:59:20 +03:00
#include "../core/String.hpp"
2018-06-22 22:59:46 +02:00
#include "../drawing/Drawing.h"
#include "../drawing/X8DrawingEngine.h"
2021-12-11 18:06:06 -05:00
#include "../localisation/Formatter.h"
#include "../paint/Painter.h"
2022-02-18 21:57:00 +01:00
#include "../platform/Platform.h"
#include "../world/Climate.h"
2018-03-19 23:28:40 +01:00
#include "../world/Map.h"
#include "../world/Park.h"
2024-10-15 22:02:31 +02:00
#include "../world/tile_element/SurfaceElement.h"
2018-01-05 23:45:53 +00:00
#include "Viewport.h"
2017-06-08 23:02:39 +01:00
2019-02-03 22:59:28 +01:00
#include <cctype>
2018-06-22 22:59:46 +02:00
#include <chrono>
#include <cstdlib>
#include <memory>
2020-02-01 22:11:45 +01:00
#include <optional>
2019-02-03 22:59:28 +01:00
#include <string>
2018-06-22 22:59:46 +02:00
2019-02-03 22:59:28 +01:00
using namespace std::literals::string_literals;
2017-06-08 23:02:39 +01:00
using namespace OpenRCT2;
using namespace OpenRCT2::Drawing;
2017-06-08 23:02:39 +01:00
extern CoordsXY gClipSelectionA;
extern CoordsXY gClipSelectionB;
extern uint8_t gClipHeight;
uint8_t gScreenshotCountdown = 0;
2016-09-03 14:15:29 +01:00
static bool WriteDpiToFile(std::string_view path, const RenderTarget& rt, const GamePalette& palette)
2018-05-08 21:41:41 +01:00
{
auto const pixels8 = rt.bits;
auto const pixelsLen = rt.LineStride() * rt.height;
2018-05-08 21:41:41 +01:00
try
{
Image image;
image.Width = rt.width;
image.Height = rt.height;
2018-05-08 21:41:41 +01:00
image.Depth = 8;
image.Stride = rt.LineStride();
image.Palette = palette;
image.Pixels = std::vector<uint8_t>(pixels8, pixels8 + pixelsLen);
2025-03-26 11:59:12 +01:00
Imaging::WriteToFile(path, image, ImageFormat::png);
2018-05-08 21:41:41 +01:00
return true;
}
catch (const std::exception& e)
{
LOG_ERROR("Unable to write png: %s", e.what());
2018-05-08 21:41:41 +01:00
return false;
}
}
/**
2015-09-22 19:02:39 +02:00
*
* rct2: 0x006E3AEC
*/
2023-01-16 21:14:50 +01:00
void ScreenshotCheck()
{
2018-06-22 22:59:46 +02:00
if (gScreenshotCountdown != 0)
{
2017-06-06 23:24:18 +02:00
gScreenshotCountdown--;
2018-06-22 22:59:46 +02:00
if (gScreenshotCountdown == 0)
{
2020-09-14 21:36:30 +02:00
// update_weather_animation();
2023-01-16 21:14:50 +01:00
std::string screenshotPath = ScreenshotDump();
2019-01-31 23:15:05 +01:00
if (!screenshotPath.empty())
2018-06-22 22:59:46 +02:00
{
2022-11-06 20:49:07 +00:00
OpenRCT2::Audio::Play(OpenRCT2::Audio::SoundId::WindowOpen, 100, ContextGetWidth() / 2);
// Show user that screenshot saved successfully
const auto filename = Path::GetFileName(screenshotPath);
Formatter ft;
ft.Add<StringId>(STR_STRING);
ft.Add<const utf8*>(filename.c_str());
2025-01-19 16:59:45 +01:00
ContextShowError(STR_SCREENSHOT_SAVED_AS, kStringIdNone, ft, true);
2018-06-22 22:59:46 +02:00
}
else
{
2025-01-19 16:59:45 +01:00
ContextShowError(STR_SCREENSHOT_FAILED, kStringIdNone, {}, true);
2017-06-06 23:24:18 +02:00
}
2020-09-14 21:36:30 +02:00
// redraw_weather();
2017-06-06 23:24:18 +02:00
}
}
}
2023-01-16 21:14:50 +01:00
static std::string ScreenshotGetParkName()
2019-02-03 22:59:28 +01:00
{
return getGameState().park.Name;
2019-02-03 22:59:28 +01:00
}
2023-01-16 21:14:50 +01:00
static std::string ScreenshotGetDirectory()
2014-04-26 03:17:32 +01:00
{
auto& env = GetContext()->GetPlatformEnvironment();
return env.GetDirectoryPath(DirBase::user, DirId::screenshots);
2019-02-03 22:59:28 +01:00
}
static std::pair<RealWorldDate, RealWorldTime> ScreenshotGetDateTime()
2019-02-03 22:59:28 +01:00
{
auto date = Platform::GetDateLocal();
auto time = Platform::GetTimeLocal();
2019-02-03 22:59:28 +01:00
return { date, time };
}
2023-01-16 21:14:50 +01:00
static std::string ScreenshotGetFormattedDateTime()
2019-02-03 22:59:28 +01:00
{
2023-01-16 21:14:50 +01:00
auto [date, time] = ScreenshotGetDateTime();
2019-02-03 22:59:28 +01:00
char formatted[64];
2018-06-22 22:59:46 +02:00
snprintf(
2019-02-03 22:59:28 +01:00
formatted, sizeof(formatted), "%4d-%02d-%02d %02d-%02d-%02d", date.year, date.month, date.day, time.hour, time.minute,
time.second);
return formatted;
}
2023-01-16 21:14:50 +01:00
static std::optional<std::string> ScreenshotGetNextPath()
2019-02-03 22:59:28 +01:00
{
2023-01-16 21:14:50 +01:00
auto screenshotDirectory = ScreenshotGetDirectory();
2023-05-09 21:08:46 +02:00
if (!Path::CreateDirectory(screenshotDirectory))
2018-06-22 22:59:46 +02:00
{
LOG_ERROR("Unable to save screenshots in OpenRCT2 screenshot directory.");
return std::nullopt;
2017-06-06 23:24:18 +02:00
}
2023-01-16 21:14:50 +01:00
auto parkName = ScreenshotGetParkName();
auto dateTime = ScreenshotGetFormattedDateTime();
2019-05-08 22:03:17 +01:00
auto name = parkName + " " + dateTime;
2019-02-03 22:59:28 +01:00
// Generate a path with a `tries` number
2019-05-08 22:03:17 +01:00
auto pathComposer = [&screenshotDirectory, &name](int tries) {
2022-01-05 15:00:16 +01:00
auto composedFilename = Platform::SanitiseFilename(
name + ((tries > 0) ? " ("s + std::to_string(tries) + ")"s : ""s) + ".png"s);
2019-05-08 22:03:17 +01:00
return screenshotDirectory + PATH_SEPARATOR + composedFilename;
2019-02-03 22:59:28 +01:00
};
for (int tries = 0; tries < 100; tries++)
{
2019-05-08 22:03:17 +01:00
auto path = pathComposer(tries);
if (!File::Exists(path))
2019-05-08 22:03:17 +01:00
{
return path;
}
2017-06-06 23:24:18 +02:00
}
2014-04-26 03:17:32 +01:00
LOG_ERROR("You have too many saved screenshots saved at exactly the same date and time.");
return std::nullopt;
2019-02-03 22:59:28 +01:00
};
2014-04-26 03:17:32 +01:00
std::string ScreenshotDumpPNG(RenderTarget& rt)
{
2017-06-06 23:24:18 +02:00
// Get a free screenshot path
2023-01-16 21:14:50 +01:00
auto path = ScreenshotGetNextPath();
2019-02-03 22:59:28 +01:00
2021-09-13 19:47:13 +03:00
if (!path.has_value())
2018-06-22 22:59:46 +02:00
{
return {};
2017-06-06 23:24:18 +02:00
}
2014-04-26 03:17:32 +01:00
if (WriteDpiToFile(path.value(), rt, gPalette))
2018-05-08 21:41:41 +01:00
{
2021-09-13 19:47:13 +03:00
return path.value();
2018-05-08 21:41:41 +01:00
}
2021-09-15 22:22:15 +02:00
return {};
}
static int32_t GetHighestBaseClearanceZ(const CoordsXY& location, const bool useViewClipping)
{
int32_t z = 0;
auto element = MapGetFirstElementAt(location);
if (element != nullptr)
{
do
{
2024-07-26 18:23:25 +10:00
if (useViewClipping && (element->GetBaseZ() > gClipHeight * kCoordsZStep))
{
continue;
}
z = std::max<int32_t>(z, element->GetBaseZ());
z = std::max<int32_t>(z, element->GetClearanceZ());
} while (!(element++)->IsLastForTile());
}
return z;
}
static int32_t GetTallestVisibleTileTop(
int32_t rotation, TileCoordsXY startCoords, TileCoordsXY endCoords, const bool useViewClipping)
{
int32_t minViewY = std::numeric_limits<int32_t>::max();
for (int32_t y = startCoords.y; y <= endCoords.y; y++)
{
for (int32_t x = startCoords.x; x <= endCoords.x; x++)
{
auto location = TileCoordsXY(x, y).ToCoordsXY();
int32_t z = GetHighestBaseClearanceZ(location, useViewClipping);
int32_t viewY = Translate3DTo2DWithZ(rotation, CoordsXYZ(location.ToTileCentre(), z)).y;
minViewY = std::min(minViewY, viewY);
}
}
// Some objects have a lower clearance than the actual sprite.
return minViewY - 64;
}
static RenderTarget CreateDPI(const Viewport& viewport)
2019-11-03 18:44:58 +01:00
{
RenderTarget rt;
rt.width = viewport.width;
rt.height = viewport.height;
rt.bits = new (std::nothrow) uint8_t[rt.width * rt.height];
if (rt.bits == nullptr)
2019-11-03 18:44:58 +01:00
{
throw std::runtime_error("Giant screenshot failed, unable to allocate memory for image.");
}
if (viewport.flags & VIEWPORT_FLAG_TRANSPARENT_BACKGROUND)
{
std::memset(rt.bits, PaletteIndex::pi0, static_cast<size_t>(rt.width) * rt.height);
2019-11-03 18:44:58 +01:00
}
return rt;
2019-11-03 18:44:58 +01:00
}
static void ReleaseDPI(RenderTarget& rt)
2019-11-03 18:44:58 +01:00
{
if (rt.bits != nullptr)
delete[] rt.bits;
rt.bits = nullptr;
rt.width = 0;
rt.height = 0;
2019-11-03 18:44:58 +01:00
}
static Viewport GetGiantViewport(int32_t rotation, ZoomLevel zoom)
{
auto& gameState = getGameState();
// Get the tile coordinates of each corner
const TileCoordsXY cornerCoords[2][4] = {
{
// Map corners
{ 1, 1 },
{ gameState.mapSize.x - 2, gameState.mapSize.y - 2 },
{ 1, gameState.mapSize.y - 2 },
{ gameState.mapSize.x - 2, 1 },
},
{
// Horizontal view clipping corners
TileCoordsXY{ CoordsXY{ std::max(gClipSelectionA.x, 32), std::max(gClipSelectionA.y, 32) } },
TileCoordsXY{ CoordsXY{ std::min(gClipSelectionB.x, (gameState.mapSize.x - 2) * 32),
std::min(gClipSelectionB.y, (gameState.mapSize.y - 2) * 32) } },
2024-02-12 21:32:08 +00:00
TileCoordsXY{
CoordsXY{ std::max(gClipSelectionA.x, 32), std::min(gClipSelectionB.y, (gameState.mapSize.y - 2) * 32) } },
2024-02-12 21:32:08 +00:00
TileCoordsXY{
CoordsXY{ std::min(gClipSelectionB.x, (gameState.mapSize.x - 2) * 32), std::max(gClipSelectionA.y, 32) } },
},
};
auto* const mainWindow = WindowGetMain();
const auto* const mainViewport = WindowGetViewport(mainWindow);
const bool useViewClipping = (mainViewport != nullptr && mainViewport->flags & VIEWPORT_FLAG_CLIP_VIEW);
// Calculate the viewport bounds
auto corners = cornerCoords[useViewClipping ? 1 : 0];
auto screenCoords1 = Translate3DTo2DWithZ(rotation, { corners[0].ToCoordsXY().ToTileCentre(), 0 });
auto screenCoords2 = Translate3DTo2DWithZ(rotation, { corners[1].ToCoordsXY().ToTileCentre(), 0 });
auto screenCoords3 = Translate3DTo2DWithZ(rotation, { corners[2].ToCoordsXY().ToTileCentre(), 0 });
auto screenCoords4 = Translate3DTo2DWithZ(rotation, { corners[3].ToCoordsXY().ToTileCentre(), 0 });
auto left = std::min({ screenCoords1.x, screenCoords2.x, screenCoords3.x, screenCoords4.x }) - 32;
auto top = GetTallestVisibleTileTop(rotation, corners[0], corners[1], useViewClipping);
auto bottom = std::max({ screenCoords1.y, screenCoords2.y, screenCoords3.y, screenCoords4.y });
auto right = std::max({ screenCoords1.x, screenCoords2.x, screenCoords3.x, screenCoords4.x }) + 32;
Viewport viewport{};
2020-03-01 09:56:14 -03:00
viewport.viewPos = { left, top };
viewport.width = zoom.ApplyInversedTo(right - left);
viewport.height = zoom.ApplyInversedTo(bottom - top);
viewport.zoom = zoom;
viewport.rotation = rotation;
return viewport;
}
static void RenderViewport(IDrawingEngine* drawingEngine, const Viewport& viewport, RenderTarget& rt)
{
2019-08-17 17:04:12 +01:00
// Ensure sprites appear regardless of rotation
ResetAllSpriteQuadrantPlacements();
2019-08-17 17:04:12 +01:00
std::unique_ptr<X8DrawingEngine> tempDrawingEngine;
if (drawingEngine == nullptr)
{
tempDrawingEngine = std::make_unique<X8DrawingEngine>(GetContext()->GetUiContext());
drawingEngine = tempDrawingEngine.get();
}
2025-04-24 18:19:59 +03:00
tempDrawingEngine->BeginDraw();
rt.DrawingEngine = drawingEngine;
ViewportRender(rt, &viewport);
2025-04-24 18:19:59 +03:00
tempDrawingEngine->EndDraw();
}
2015-06-02 14:12:36 +01:00
2023-01-16 21:14:50 +01:00
void ScreenshotGiant()
{
RenderTarget rt{};
try
2018-06-22 22:59:46 +02:00
{
2023-01-16 21:14:50 +01:00
auto path = ScreenshotGetNextPath();
2021-09-13 19:47:13 +03:00
if (!path.has_value())
{
throw std::runtime_error("Giant screenshot failed, unable to find a suitable destination path.");
}
2023-01-16 21:14:50 +01:00
const auto rotation = GetCurrentRotation();
2021-09-29 20:04:56 +02:00
auto zoom = ZoomLevel{ 0 };
auto* mainWindow = WindowGetMain();
const auto* vp = WindowGetViewport(mainWindow);
if (mainWindow != nullptr && vp != nullptr)
{
zoom = vp->zoom;
}
auto viewport = GetGiantViewport(rotation, zoom);
if (vp != nullptr)
{
viewport.flags = vp->flags;
}
2024-05-10 12:10:21 +02:00
if (Config::Get().general.TransparentScreenshot)
{
viewport.flags |= VIEWPORT_FLAG_TRANSPARENT_BACKGROUND;
}
rt = CreateDPI(viewport);
2019-11-03 18:44:58 +01:00
RenderViewport(nullptr, viewport, rt);
WriteDpiToFile(path.value(), rt, gPalette);
// Show user that screenshot saved successfully
2022-02-26 17:20:07 +01:00
const auto filename = Path::GetFileName(path.value());
Formatter ft;
2022-07-31 14:22:58 +02:00
ft.Add<StringId>(STR_STRING);
2022-02-26 17:20:07 +01:00
ft.Add<const utf8*>(filename.c_str());
2025-01-19 16:59:45 +01:00
ContextShowError(STR_SCREENSHOT_SAVED_AS, kStringIdNone, ft, true);
}
catch (const std::exception& e)
{
LOG_ERROR("%s", e.what());
2025-01-19 16:59:45 +01:00
ContextShowError(STR_SCREENSHOT_FAILED, kStringIdNone, {}, true);
2017-06-06 23:24:18 +02:00
}
2019-11-03 18:44:58 +01:00
ReleaseDPI(rt);
2019-11-03 18:44:58 +01:00
}
static void ApplyOptions(const ScreenshotOptions* options, Viewport& viewport)
{
if (options->weather != WeatherType::Sunny && options->weather != WeatherType::Count)
2019-05-30 11:30:29 +02:00
{
ClimateForceWeather(WeatherType{ static_cast<uint8_t>(EnumValue(options->weather) - 1) });
2017-06-08 23:02:39 +01:00
}
2019-05-30 11:30:29 +02:00
if (options->hide_guests)
{
2022-03-06 14:26:36 +00:00
viewport.flags |= VIEWPORT_FLAG_HIDE_GUESTS | VIEWPORT_FLAG_HIDE_STAFF;
2019-05-30 11:30:29 +02:00
}
if (options->hide_sprites)
{
2022-03-06 14:26:36 +00:00
viewport.flags |= VIEWPORT_FLAG_HIDE_ENTITIES;
2019-05-30 11:30:29 +02:00
}
if (options->mowed_grass)
{
CheatsSet(CheatType::SetGrassLength, GRASS_LENGTH_MOWED);
}
if (options->clear_grass || options->tidy_up_park)
{
CheatsSet(CheatType::SetGrassLength, GRASS_LENGTH_CLEAR_0);
}
if (options->water_plants || options->tidy_up_park)
{
CheatsSet(CheatType::WaterPlants);
}
if (options->fix_vandalism || options->tidy_up_park)
{
CheatsSet(CheatType::FixVandalism);
}
if (options->remove_litter || options->tidy_up_park)
{
CheatsSet(CheatType::RemoveLitter);
}
2024-05-10 12:10:21 +02:00
if (options->transparent || Config::Get().general.TransparentScreenshot)
{
viewport.flags |= VIEWPORT_FLAG_TRANSPARENT_BACKGROUND;
}
if (options->draw_bounding_boxes)
{
gPaintBoundingBoxes = true;
}
}
int32_t CommandLineForScreenshot(const char** argv, int32_t argc, ScreenshotOptions* options)
{
// Don't include options in the count (they have been handled by CommandLine::ParseOptions already)
for (int32_t i = 0; i < argc; i++)
{
if (argv[i][0] == '-')
{
// Setting argc to i works, because options can only be at the end of the command
argc = i;
break;
}
}
2019-05-30 11:30:29 +02:00
2024-12-11 22:28:31 +01:00
bool giantScreenshot = (argc == 5) && String::iequals(argv[2], "giant");
if (argc != 4 && argc != 8 && argc != 9 && !giantScreenshot)
{
std::printf("Usage: openrct2 screenshot <file> <output_image> <width> <height> [<x> <y> [<z>] <zoom> <rotation>]\n");
std::printf("Usage: openrct2 screenshot <file> <output_image> giant <zoom> <rotation>\n");
return -1;
}
2019-05-30 11:30:29 +02:00
int32_t exitCode = 1;
RenderTarget rt;
try
{
bool customLocation = false;
bool centreMapX = false;
bool centreMapY = false;
2019-05-30 11:30:29 +02:00
const char* inputPath = argv[0];
const char* outputPath = argv[1];
gOpenRCT2Headless = true;
auto context = CreateContext();
if (!context->Initialise())
{
throw std::runtime_error("Failed to initialize context.");
}
DrawingEngineInit();
if (!context->LoadParkFromFile(inputPath))
{
throw std::runtime_error("Failed to load park.");
}
2025-03-05 21:45:04 +01:00
gLegacyScene = LegacyScene::playing;
Viewport viewport{};
if (giantScreenshot)
{
auto customZoom = static_cast<int8_t>(std::atoi(argv[3]));
auto zoom = ZoomLevel{ customZoom };
auto rotation = std::atoi(argv[4]) & 3;
viewport = GetGiantViewport(rotation, zoom);
}
else
{
int32_t resolutionWidth = std::atoi(argv[2]);
int32_t resolutionHeight = std::atoi(argv[3]);
int32_t customX = 0;
int32_t customY = 0;
int32_t customZoom = 0;
int32_t customRotation = 0;
if (argc >= 8)
{
int32_t argOffset = argc == 8 ? 0 : 1;
customLocation = true;
if (argv[4][0] == 'c')
centreMapX = true;
else
customX = std::atoi(argv[4]);
if (argv[5][0] == 'c')
centreMapY = true;
else
customY = std::atoi(argv[5]);
customZoom = std::atoi(argv[6 + argOffset]);
customRotation = std::atoi(argv[7 + argOffset]) & 3;
}
const auto& mapSize = getGameState().mapSize;
if (resolutionWidth == 0 || resolutionHeight == 0)
{
2024-07-26 18:23:25 +10:00
resolutionWidth = (mapSize.x * kCoordsXYStep * 2) >> customZoom;
resolutionHeight = (mapSize.y * kCoordsXYStep * 1) >> customZoom;
resolutionWidth += 8;
resolutionHeight += 128;
}
viewport.width = resolutionWidth;
viewport.height = resolutionHeight;
if (customLocation)
{
if (centreMapX)
customX = (mapSize.x / 2) * 32 + 16;
if (centreMapY)
customY = (mapSize.y / 2) * 32 + 16;
2022-10-12 06:35:20 +01:00
int32_t z = TileElementHeight({ customX, customY });
if (argc == 9)
z = std::atoi(argv[6]);
CoordsXYZ coords3d = { customX, customY, z };
auto coords2d = Translate3DTo2DWithZ(customRotation, coords3d);
viewport.viewPos = { coords2d.x - ((viewport.ViewWidth() << customZoom) / 2),
coords2d.y - ((viewport.ViewHeight() << customZoom) / 2) };
viewport.zoom = ZoomLevel{ static_cast<int8_t>(customZoom) };
viewport.rotation = customRotation;
}
else
{
auto& gameState = getGameState();
viewport.viewPos = { gameState.savedView
- ScreenCoordsXY{ (viewport.ViewWidth() / 2), (viewport.ViewHeight() / 2) } };
viewport.zoom = gameState.savedViewZoom;
viewport.rotation = gameState.savedViewRotation;
}
}
ApplyOptions(options, viewport);
rt = CreateDPI(viewport);
2019-11-03 18:44:58 +01:00
RenderViewport(nullptr, viewport, rt);
WriteDpiToFile(outputPath, rt, gPalette);
}
catch (const std::exception& e)
{
std::printf("%s\n", e.what());
exitCode = -1;
}
ReleaseDPI(rt);
2019-11-03 18:44:58 +01:00
DrawingEngineDispose();
2019-05-30 11:30:29 +02:00
return exitCode;
2015-09-11 15:23:11 +02:00
}
static bool IsPathChildOf(fs::path x, const fs::path& parent)
{
auto xp = x.parent_path();
while (xp != x)
{
if (xp == parent)
{
return true;
}
x = xp;
xp = x.parent_path();
}
return false;
}
static std::string ResolveFilenameForCapture(const fs::path& filename)
{
if (filename.empty())
{
// Automatic filename
2023-01-16 21:14:50 +01:00
auto path = ScreenshotGetNextPath();
if (!path)
{
throw std::runtime_error("Unable to generate a filename for capture.");
}
return *path;
}
2021-09-15 22:22:15 +02:00
2023-01-16 21:14:50 +01:00
auto screenshotDirectory = fs::u8path(ScreenshotGetDirectory());
2021-09-15 22:22:15 +02:00
auto screenshotPath = fs::absolute(screenshotDirectory / filename);
// Check the filename isn't attempting to leave the screenshot directory for security
if (!IsPathChildOf(screenshotPath, screenshotDirectory))
{
2021-09-15 22:22:15 +02:00
throw std::runtime_error("Filename is not a child of the screenshot directory.");
}
2021-09-15 22:22:15 +02:00
auto directory = screenshotPath.parent_path();
if (!fs::is_directory(directory))
{
if (!fs::create_directory(directory, screenshotDirectory))
{
throw std::runtime_error("Unable to create directory.");
}
}
return screenshotPath.u8string();
}
void CaptureImage(const CaptureOptions& options)
{
Viewport viewport{};
2021-09-13 19:47:13 +03:00
if (options.View.has_value())
{
viewport.width = options.View->Width;
viewport.height = options.View->Height;
2022-10-12 06:35:20 +01:00
auto z = TileElementHeight(options.View->Position);
CoordsXYZ coords3d(options.View->Position, z);
auto coords2d = Translate3DTo2DWithZ(options.Rotation, coords3d);
viewport.viewPos = { coords2d.x - ((options.Zoom.ApplyTo(viewport.ViewWidth())) / 2),
coords2d.y - ((options.Zoom.ApplyTo(viewport.ViewHeight())) / 2) };
viewport.zoom = options.Zoom;
viewport.rotation = options.Rotation;
}
else
{
viewport = GetGiantViewport(options.Rotation, options.Zoom);
}
if (options.Transparent)
{
viewport.flags |= VIEWPORT_FLAG_TRANSPARENT_BACKGROUND;
}
auto outputPath = ResolveFilenameForCapture(options.Filename);
auto rt = CreateDPI(viewport);
RenderViewport(nullptr, viewport, rt);
WriteDpiToFile(outputPath, rt, gPalette);
ReleaseDPI(rt);
}