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

669 lines
20 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2024 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"
2017-11-30 18:17:06 +01:00
#include "../Game.h"
#include "../GameState.h"
2017-12-12 14:52:57 +01:00
#include "../Intro.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"
2018-06-22 22:59:46 +02:00
#include "../audio/audio.h"
#include "../core/Console.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"
2018-01-06 18:32:25 +01:00
#include "../localisation/Localisation.h"
2022-02-18 21:57:00 +01:00
#include "../platform/Platform.h"
2017-12-13 13:02:24 +01:00
#include "../util/Util.h"
#include "../world/Climate.h"
2018-03-19 23:28:40 +01:00
#include "../world/Map.h"
#include "../world/Park.h"
2018-05-01 16:33:16 +02:00
#include "../world/Surface.h"
2018-01-05 23:45:53 +00:00
#include "Viewport.h"
2017-06-08 23:02:39 +01:00
#include <algorithm>
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
2023-04-03 22:07:33 +02:00
static bool WriteDpiToFile(std::string_view path, const DrawPixelInfo& dpi, const GamePalette& palette)
2018-05-08 21:41:41 +01:00
{
2023-04-03 22:07:33 +02:00
auto const pixels8 = dpi.bits;
auto const pixelsLen = (dpi.width + dpi.pitch) * dpi.height;
2018-05-08 21:41:41 +01:00
try
{
Image image;
2023-04-03 22:07:33 +02:00
image.Width = dpi.width;
image.Height = dpi.height;
2018-05-08 21:41:41 +01:00
image.Depth = 8;
2023-04-03 22:07:33 +02:00
image.Stride = dpi.width + dpi.pitch;
2020-05-27 20:12:01 +01:00
image.Palette = std::make_unique<GamePalette>(palette);
image.Pixels = std::vector<uint8_t>(pixels8, pixels8 + pixelsLen);
2018-05-08 21:41:41 +01:00
Imaging::WriteToFile(path, image, IMAGE_FORMAT::PNG);
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);
2018-06-22 22:59:46 +02:00
}
else
{
2022-11-06 20:49:07 +00:00
ContextShowError(STR_SCREENSHOT_FAILED, STR_NONE, {});
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
{
2019-07-21 10:33:15 +01:00
return GetContext()->GetGameState()->GetPark().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::SCREENSHOT);
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(
2019-05-08 22:03:17 +01:00
name + ((tries > 0) ? " ("s + std::to_string(tries) + ")" : ""s) + ".png");
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
2023-04-03 22:07:33 +02:00
std::string ScreenshotDumpPNG(DrawPixelInfo& dpi)
{
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
{
2019-01-31 23:15:05 +01:00
return "";
2017-06-06 23:24:18 +02:00
}
2014-04-26 03:17:32 +01:00
2021-09-13 19:47:13 +03:00
if (WriteDpiToFile(path.value(), dpi, 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 "";
}
2023-01-16 21:14:50 +01:00
std::string ScreenshotDumpPNG32bpp(int32_t width, int32_t height, const void* pixels)
2016-06-11 03:06:30 +01:00
{
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
{
2019-01-31 23:15:05 +01:00
return "";
2017-06-06 23:24:18 +02:00
}
2016-06-11 03:06:30 +01:00
2020-04-22 17:09:29 +02:00
const auto pixels8 = static_cast<const uint8_t*>(pixels);
2018-05-08 21:41:41 +01:00
const auto pixelsLen = width * 4 * height;
try
{
Image image;
image.Width = width;
image.Height = height;
image.Depth = 32;
image.Stride = width * 4;
image.Pixels = std::vector<uint8_t>(pixels8, pixels8 + pixelsLen);
2021-09-13 19:47:13 +03:00
Imaging::WriteToFile(path.value(), image, IMAGE_FORMAT::PNG_32);
return path.value();
2018-05-08 21:41:41 +01:00
}
2018-06-22 22:59:46 +02:00
catch (const std::exception& e)
2018-05-08 21:41:41 +01:00
{
LOG_ERROR("Unable to save screenshot: %s", e.what());
2019-01-31 23:15:05 +01:00
return "";
2017-06-06 23:24:18 +02:00
}
2016-06-11 03:06:30 +01:00
}
static int32_t GetHighestBaseClearanceZ(const CoordsXY& location, const bool useViewClipping)
{
int32_t z = 0;
auto element = MapGetFirstElementAt(location);
if (element != nullptr)
{
do
{
if (useViewClipping && (element->GetBaseZ() > gClipHeight * COORDS_Z_STEP))
{
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 DrawPixelInfo CreateDPI(const Viewport& viewport)
2019-11-03 18:44:58 +01:00
{
DrawPixelInfo dpi;
2019-11-03 18:44:58 +01:00
dpi.width = viewport.width;
dpi.height = viewport.height;
dpi.bits = new (std::nothrow) uint8_t[dpi.width * dpi.height];
if (dpi.bits == nullptr)
{
throw std::runtime_error("Giant screenshot failed, unable to allocate memory for image.");
}
if (viewport.flags & VIEWPORT_FLAG_TRANSPARENT_BACKGROUND)
{
2020-04-22 17:09:29 +02:00
std::memset(dpi.bits, PALETTE_INDEX_0, static_cast<size_t>(dpi.width) * dpi.height);
2019-11-03 18:44:58 +01:00
}
return dpi;
}
static void ReleaseDPI(DrawPixelInfo& dpi)
2019-11-03 18:44:58 +01:00
{
if (dpi.bits != nullptr)
delete[] dpi.bits;
dpi.bits = nullptr;
dpi.width = 0;
dpi.height = 0;
}
static Viewport GetGiantViewport(int32_t rotation, ZoomLevel zoom)
{
// Get the tile coordinates of each corner
const TileCoordsXY cornerCoords[2][4] = {
{
// Map corners
{ 1, 1 },
{ gMapSize.x - 2, gMapSize.y - 2 },
{ 1, gMapSize.y - 2 },
{ gMapSize.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, (gMapSize.x - 2) * 32),
std::min(gClipSelectionB.y, (gMapSize.y - 2) * 32) } },
TileCoordsXY{ CoordsXY{ std::max(gClipSelectionA.x, 32), std::min(gClipSelectionB.y, (gMapSize.y - 2) * 32) } },
TileCoordsXY{ CoordsXY{ std::min(gClipSelectionB.x, (gMapSize.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.view_width = right - left;
viewport.view_height = bottom - top;
viewport.width = zoom.ApplyInversedTo(viewport.view_width);
viewport.height = zoom.ApplyInversedTo(viewport.view_height);
viewport.zoom = zoom;
return viewport;
}
static void RenderViewport(IDrawingEngine* drawingEngine, const Viewport& viewport, DrawPixelInfo& dpi)
{
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();
}
dpi.DrawingEngine = drawingEngine;
2023-04-03 22:07:33 +02:00
ViewportRender(dpi, &viewport, { { 0, 0 }, { viewport.width, viewport.height } });
}
2015-06-02 14:12:36 +01:00
2023-01-16 21:14:50 +01:00
void ScreenshotGiant()
{
DrawPixelInfo dpi{};
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;
}
2022-10-16 17:27:01 +02:00
if (gConfigGeneral.TransparentScreenshot)
{
viewport.flags |= VIEWPORT_FLAG_TRANSPARENT_BACKGROUND;
}
2019-11-03 18:44:58 +01:00
dpi = CreateDPI(viewport);
RenderViewport(nullptr, viewport, dpi);
2023-04-03 22:07:33 +02:00
WriteDpiToFile(path.value(), dpi, 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());
2022-11-06 20:49:07 +00:00
ContextShowError(STR_SCREENSHOT_SAVED_AS, STR_NONE, ft);
}
catch (const std::exception& e)
{
LOG_ERROR("%s", e.what());
2022-11-06 20:49:07 +00:00
ContextShowError(STR_SCREENSHOT_FAILED, STR_NONE, {});
2017-06-06 23:24:18 +02:00
}
2019-11-03 18:44:58 +01:00
ReleaseDPI(dpi);
}
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);
}
2022-10-16 17:27:01 +02:00
if (options->transparent || gConfigGeneral.TransparentScreenshot)
{
viewport.flags |= VIEWPORT_FLAG_TRANSPARENT_BACKGROUND;
}
}
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
2023-07-02 12:59:20 +03:00
bool giantScreenshot = (argc == 5) && String::IEquals(argv[2], "giant");
if (argc != 4 && argc != 8 && !giantScreenshot)
{
std::printf("Usage: openrct2 screenshot <file> <output_image> <width> <height> [<x> <y> <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;
DrawPixelInfo dpi;
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.");
}
gIntroState = IntroState::None;
gScreenFlags = SCREEN_FLAGS_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);
gCurrentRotation = rotation;
}
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)
{
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]);
customRotation = std::atoi(argv[7]) & 3;
}
const auto& mapSize = gMapSize;
if (resolutionWidth == 0 || resolutionHeight == 0)
{
2022-02-03 22:12:00 +00:00
resolutionWidth = (mapSize.x * COORDS_XY_STEP * 2) >> customZoom;
resolutionHeight = (mapSize.y * COORDS_XY_STEP * 1) >> customZoom;
resolutionWidth += 8;
resolutionHeight += 128;
}
viewport.width = resolutionWidth;
viewport.height = resolutionHeight;
viewport.view_width = viewport.width;
viewport.view_height = viewport.height;
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 });
CoordsXYZ coords3d = { customX, customY, z };
auto coords2d = Translate3DTo2DWithZ(customRotation, coords3d);
2020-03-01 09:56:14 -03:00
viewport.viewPos = { coords2d.x - ((viewport.view_width << customZoom) / 2),
coords2d.y - ((viewport.view_height << customZoom) / 2) };
viewport.zoom = ZoomLevel{ static_cast<int8_t>(customZoom) };
gCurrentRotation = customRotation;
}
else
{
2020-03-01 10:49:36 -03:00
viewport.viewPos = { gSavedView - ScreenCoordsXY{ (viewport.view_width / 2), (viewport.view_height / 2) } };
viewport.zoom = gSavedViewZoom;
gCurrentRotation = gSavedViewRotation;
}
}
ApplyOptions(options, viewport);
2019-11-03 18:44:58 +01:00
dpi = CreateDPI(viewport);
RenderViewport(nullptr, viewport, dpi);
2023-04-03 22:07:33 +02:00
WriteDpiToFile(outputPath, dpi, gPalette);
}
catch (const std::exception& e)
{
std::printf("%s\n", e.what());
exitCode = -1;
}
2019-11-03 18:44:58 +01:00
ReleaseDPI(dpi);
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;
viewport.view_width = viewport.width;
viewport.view_height = viewport.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.view_width)) / 2),
coords2d.y - ((options.Zoom.ApplyTo(viewport.view_height)) / 2) };
viewport.zoom = options.Zoom;
}
else
{
viewport = GetGiantViewport(options.Rotation, options.Zoom);
}
auto backupRotation = gCurrentRotation;
gCurrentRotation = options.Rotation;
if (options.Transparent)
{
viewport.flags |= VIEWPORT_FLAG_TRANSPARENT_BACKGROUND;
}
auto outputPath = ResolveFilenameForCapture(options.Filename);
auto dpi = CreateDPI(viewport);
RenderViewport(nullptr, viewport, dpi);
2023-04-03 22:07:33 +02:00
WriteDpiToFile(outputPath, dpi, gPalette);
ReleaseDPI(dpi);
gCurrentRotation = backupRotation;
}