Skip to content

Commit

Permalink
Working on compiled tileset import
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Aug 31, 2023
1 parent 43a3fd2 commit f054c15
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ std::unordered_map<std::size_t, Attributes>
importAttributesFromCsv(PtContext &ctx, const std::unordered_map<std::string, std::uint8_t> &behaviorMap,
const std::string &filePath);

CompiledTileset importCompiledTileset(PtContext &ctx, const std::string &tilesetPath);

} // namespace porytiles

#endif // PORYTILES_IMPORTER_H
33 changes: 25 additions & 8 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace porytiles {
static void drivePaletteEmit(PtContext &ctx, const CompiledTileset &compiledTiles,
const std::filesystem::path &palettesPath)
{
// TODO : move this function's functionality into emitter
for (std::size_t i = 0; i < ctx.fieldmapConfig.numPalettesTotal; i++) {
std::string fileName = i < 10 ? "0" + std::to_string(i) : std::to_string(i);
fileName += ".pal";
Expand All @@ -41,6 +42,7 @@ static void drivePaletteEmit(PtContext &ctx, const CompiledTileset &compiledTile
static void driveTilesEmit(PtContext &ctx, const CompiledTileset &compiledTiles,
const std::filesystem::path &tilesetPath)
{
// TODO : move this function's functionality into emitter
const std::size_t imageWidth = porytiles::TILE_SIDE_LENGTH * porytiles::TILES_PNG_WIDTH_IN_TILES;
const std::size_t imageHeight =
porytiles::TILE_SIDE_LENGTH * ((compiledTiles.tiles.size() / porytiles::TILES_PNG_WIDTH_IN_TILES) + 1);
Expand All @@ -53,6 +55,7 @@ static void driveTilesEmit(PtContext &ctx, const CompiledTileset &compiledTiles,
static void driveAnimEmit(PtContext &ctx, const std::vector<CompiledAnimation> &compiledAnims,
const std::vector<GBAPalette> &palettes, const std::filesystem::path &animsPath)
{
// TODO : move this function's functionality into emitter
for (const auto &compiledAnim : compiledAnims) {
std::filesystem::path animPath = animsPath / compiledAnim.animName;
std::filesystem::create_directories(animPath);
Expand All @@ -74,6 +77,7 @@ static void driveAnimEmit(PtContext &ctx, const std::vector<CompiledAnimation> &

static void driveAnimsImport(PtContext &ctx, DecompiledTileset &decompTiles, std::filesystem::path animationPath)
{
// TODO : move this function's functionality into importer
pt_logln(ctx, stderr, "importing animations from {}", animationPath.string());
if (!std::filesystem::exists(animationPath) || !std::filesystem::is_directory(animationPath)) {
pt_logln(ctx, stderr, "path `{}' does not exist, skipping anims import", animationPath.string());
Expand Down Expand Up @@ -149,6 +153,7 @@ static std::unordered_map<std::size_t, Attributes>
buildAttributesMap(PtContext &ctx, const std::unordered_map<std::string, std::uint8_t> &behaviorMap,
std::filesystem::path attributesCsvPath)
{
// TODO : move this function's functionality into importer
pt_logln(ctx, stderr, "importing attributes from {}", attributesCsvPath.string());
if (!std::filesystem::exists(attributesCsvPath) || !std::filesystem::is_regular_file(attributesCsvPath)) {
pt_logln(ctx, stderr, "path `{}' does not exist, skipping attributes import", attributesCsvPath.string());
Expand All @@ -159,7 +164,10 @@ buildAttributesMap(PtContext &ctx, const std::unordered_map<std::string, std::ui
return importAttributesFromCsv(ctx, behaviorMap, attributesCsvPath.string());
}

// static void driveCompileFreestanding(PtContext &ctx) {}
static void driveDecompile(PtContext &ctx)
{
// TODO : impl
}

static void driveCompile(PtContext &ctx)
{
Expand Down Expand Up @@ -386,12 +394,21 @@ static void driveCompile(PtContext &ctx)
fatalerror(ctx.err, ctx.srcPaths, ctx.compilerConfig.mode,
fmt::format("'{}' exists in output directory but is not a file", attribtuesPath.string()));
}
/*
* TODO : these creates will throw an internalerror if user does not have permissions to make path, this should be a
* fatal error, not an internalerror
*/
std::filesystem::create_directories(palettesPath);
std::filesystem::create_directories(animsPath);

try {
std::filesystem::create_directories(palettesPath);
}
catch (const std::exception &e) {
fatalerror(ctx.err, ctx.srcPaths, ctx.compilerConfig.mode,
fmt::format("could not create '{}': {}", palettesPath.string(), e.what()));
}
try {
std::filesystem::create_directories(animsPath);
}
catch (const std::exception &e) {
fatalerror(ctx.err, ctx.srcPaths, ctx.compilerConfig.mode,
fmt::format("could not create '{}': {}", animsPath.string(), e.what()));
}

drivePaletteEmit(ctx, *compiledTiles, palettesPath);
driveTilesEmit(ctx, *compiledTiles, tilesetPath);
Expand All @@ -410,7 +427,7 @@ void drive(PtContext &ctx)
{
switch (ctx.subcommand) {
case Subcommand::DECOMPILE:
throw std::runtime_error{"TODO : support decompile command"};
driveDecompile(ctx);
break;
case Subcommand::COMPILE_PRIMARY:
case Subcommand::COMPILE_SECONDARY:
Expand Down
11 changes: 10 additions & 1 deletion src/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ DecompiledTileset importLayeredTilesFromPngs(PtContext &ctx,
"source layer png dimensions invalid");
}

DecompiledTileset decompiledTiles;
DecompiledTileset decompiledTiles{};

// Since all widths and heights are the same, we can just read the bottom layer's width and height
std::size_t widthInMetatiles = bottom.get_width() / METATILE_SIDE_LENGTH;
Expand Down Expand Up @@ -572,6 +572,15 @@ importAttributesFromCsv(PtContext &ctx, const std::unordered_map<std::string, st
return attributeMap;
}

CompiledTileset importCompiledTileset(PtContext &ctx, const std::filesystem::path &tilesetPath)
{
CompiledTileset tileset{};
//std::ifstream metatiles{tilesetPath / "metatiles.bin"};


return tileset;
}

} // namespace porytiles

TEST_CASE("importTilesFromPng should read an RGBA PNG into a DecompiledTileset in tile-wise left-to-right, "
Expand Down

0 comments on commit f054c15

Please sign in to comment.