MCPcopy Create free account
hub / github.com/OpenRCT2/OpenRCT2 / exportObject

Function exportObject

src/openrct2/command_line/sprite/SpriteExportObject.cpp:24–80  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22namespace OpenRCT2::CommandLine::Sprite
23{
24 ExitCode exportObject(const char** argv, int32_t argc)
25 {
26 if (argc < 3)
27 {
28 fprintf(stdout, "usage: sprite exportobject <path to object> <output directory>\n");
29 return ExitCode::fail;
30 }
31
32 const char* objectPath = argv[1];
33 const utf8* outputPath = argv[2];
34 auto context = CreateContext();
35 context->Initialise();
36
37 std::unique_ptr<Object> metaObject = ObjectFactory::CreateObjectFromFile(objectPath, true);
38 if (metaObject == nullptr)
39 {
40 fprintf(stderr, "Could not load the object.\n");
41 return ExitCode::fail;
42 }
43
44 if (!Path::CreateDirectory(outputPath))
45 {
46 fprintf(stderr, "Unable to create output directory.\n");
47 return ExitCode::fail;
48 }
49
50 const auto* imageTableStart = metaObject->GetImageTable().GetImages();
51 const uint32_t maxIndex = metaObject->GetNumImages();
52 const int32_t numbers = static_cast<int32_t>(std::floor(std::log10(maxIndex) + 1));
53
54 std::ostringstream oss; // TODO: Remove when C++20 is enabled and std::format can be used
55 for (uint32_t spriteIndex = 0; spriteIndex < maxIndex; spriteIndex++)
56 {
57 oss << std::setw(numbers) << std::setfill('0') << spriteIndex << ".png";
58 auto path = Path::Combine(outputPath, PopStr(oss));
59
60 const auto& g1 = imageTableStart[spriteIndex];
61 if (g1.width == 0 || g1.height == 0)
62 {
63 fprintf(stdout, "\"\"");
64 }
65 else
66 {
67 if (!SpriteImageExport(g1, path))
68 {
69 fprintf(stderr, "Could not export\n");
70 return ExitCode::fail;
71 }
72
73 path = fs::u8path(path).generic_u8string();
74 fprintf(stdout, "{ \"path\": \"%s\", \"x\": %d, \"y\": %d }", path.c_str(), g1.xOffset, g1.yOffset);
75 }
76
77 fprintf(stdout, (spriteIndex + 1 != maxIndex) ? ",\n" : "\n");
78 }
79 return ExitCode::ok;
80 }
81

Callers 1

HandleSpriteFunction · 0.85

Calls 9

CreateContextFunction · 0.85
CreateObjectFromFileFunction · 0.85
CreateDirectoryFunction · 0.85
PopStrFunction · 0.85
SpriteImageExportFunction · 0.85
GetImagesMethod · 0.80
GetNumImagesMethod · 0.80
CombineFunction · 0.50
InitialiseMethod · 0.45

Tested by

no test coverage detected