1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-23 15:52:55 +01:00

Create park preview images from scenario meta objects

This commit is contained in:
Aaron van Geffen
2025-05-16 14:33:21 +02:00
parent 66d67e2ee6
commit 7cd59ab75f
3 changed files with 93 additions and 33 deletions

View File

@@ -26,6 +26,9 @@
#include <openrct2/localisation/Formatting.h> #include <openrct2/localisation/Formatting.h>
#include <openrct2/localisation/Localisation.Date.h> #include <openrct2/localisation/Localisation.Date.h>
#include <openrct2/localisation/LocalisationService.h> #include <openrct2/localisation/LocalisationService.h>
#include <openrct2/object/ObjectManager.h>
#include <openrct2/object/ObjectRepository.h>
#include <openrct2/object/ScenarioMetaObject.h>
#include <openrct2/park/ParkPreview.h> #include <openrct2/park/ParkPreview.h>
#include <openrct2/ride/RideData.h> #include <openrct2/ride/RideData.h>
#include <openrct2/scenario/Scenario.h> #include <openrct2/scenario/Scenario.h>
@@ -190,29 +193,24 @@ namespace OpenRCT2::Ui::Windows
auto& bgWorker = GetContext()->GetBackgroundWorker(); auto& bgWorker = GetContext()->GetBackgroundWorker();
auto& path = _highlightedScenario->Path; auto& path = _highlightedScenario->Path;
auto& name = _highlightedScenario->InternalName;
_previewLoadJob = bgWorker.addJob(
[path]() {
try
{
auto fs = FileStream(path, FileMode::open);
ClassifiedFileInfo info; ClassifiedFileInfo info;
if (!TryClassifyFile(&fs, &info)) if (!TryClassifyFile(path, &info))
return ParkPreview{}; return;
if (info.Type == FileType::park) if (info.Type == FileType::park)
{ {
_previewLoadJob = bgWorker.addJob(
[path, name]() {
try
{
auto fs = FileStream(path, FileMode::open);
auto& objectRepository = GetContext()->GetObjectRepository(); auto& objectRepository = GetContext()->GetObjectRepository();
auto parkImporter = ParkImporter::CreateParkFile(objectRepository); auto parkImporter = ParkImporter::CreateParkFile(objectRepository);
parkImporter->LoadFromStream(&fs, false, true, path.c_str()); parkImporter->LoadFromStream(&fs, false, true, path.c_str());
return parkImporter->GetParkPreview(); return parkImporter->GetParkPreview();
} }
else
{
return ParkPreview{};
}
}
catch (const std::exception& e) catch (const std::exception& e)
{ {
LOG_ERROR("Could not get preview for \"%s\" due to %s", path.c_str(), e.what()); LOG_ERROR("Could not get preview for \"%s\" due to %s", path.c_str(), e.what());
@@ -230,6 +228,43 @@ namespace OpenRCT2::Ui::Windows
scenarioSelectWnd->UpdateParkPreview(preview); scenarioSelectWnd->UpdateParkPreview(preview);
}); });
} }
else if (info.Type == FileType::scenario)
{
SourceDescriptor source{};
if (!ScenarioSources::TryGetByName(name, &source))
return;
auto& objManager = GetContext()->GetObjectManager();
// Unload current scenario meta object if it's not the one we need
auto* loadedObject = objManager.GetLoadedObject(ObjectType::scenarioMeta, 0);
if (loadedObject != nullptr && loadedObject->GetIdentifier() != source.textObjectId)
{
objManager.UnloadObjects({ loadedObject->GetDescriptor() });
loadedObject = nullptr;
}
// Load the relevant scenario meta file if it hasn't been loaded yet
if (loadedObject == nullptr)
{
loadedObject = objManager.LoadObject(source.textObjectId);
if (loadedObject == nullptr)
return;
}
auto* scenarioMetaObj = reinterpret_cast<ScenarioMetaObject*>(loadedObject);
ParkPreview preview{};
preview.images.push_back(scenarioMetaObj->GetMiniMapImage());
preview.images.push_back(scenarioMetaObj->GetPreviewImage());
_preview = preview;
}
else
{
return;
}
}
void UpdateParkPreview(const ParkPreview& preview) void UpdateParkPreview(const ParkPreview& preview)
{ {

View File

@@ -51,12 +51,36 @@ std::string ScenarioMetaObject::GetScenarioDetails()
return GetStringTable().GetString(ObjectStringID::SCENARIO_DETAILS); return GetStringTable().GetString(ObjectStringID::SCENARIO_DETAILS);
} }
ImageIndex ScenarioMetaObject::GetMiniMapImageIndex() const PreviewImage ScenarioMetaObject::GetMiniMapImage() const
{ {
return _imageOffsetId; PreviewImage preview{};
preview.type = PreviewImageType::miniMap;
auto* g1 = GfxGetG1Element(_imageOffsetId);
if (g1 == nullptr)
return preview;
preview.width = g1->width;
preview.height = g1->height;
std::copy_n(g1->offset, g1->width * g1->height, preview.pixels);
return preview;
} }
ImageIndex ScenarioMetaObject::GetPreviewImageIndex() const PreviewImage ScenarioMetaObject::GetPreviewImage() const
{ {
return _imageOffsetId + 1; PreviewImage preview{};
preview.type = PreviewImageType::screenshot;
auto* g1 = GfxGetG1Element(_imageOffsetId + 1);
if (g1 == nullptr)
return preview;
preview.width = g1->width;
preview.height = g1->height;
std::copy_n(g1->offset, g1->width * g1->height, preview.pixels);
return preview;
} }

View File

@@ -10,6 +10,7 @@
#pragma once #pragma once
#include "../core/IStream.hpp" #include "../core/IStream.hpp"
#include "../park/ParkPreview.h"
#include "Object.h" #include "Object.h"
#include <string> #include <string>
@@ -30,6 +31,6 @@ public:
std::string GetParkName(); std::string GetParkName();
std::string GetScenarioDetails(); std::string GetScenarioDetails();
ImageIndex GetMiniMapImageIndex() const; OpenRCT2::PreviewImage GetMiniMapImage() const;
ImageIndex GetPreviewImageIndex() const; OpenRCT2::PreviewImage GetPreviewImage() const;
}; };