Skip to content

Commit

Permalink
WIP Emit decompiled tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Aug 30, 2023
1 parent 024adf4 commit 8d7c844
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/emitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ void emitAnim(PtContext &ctx, std::vector<png::image<png::index_pixel>> &outFram
void emitAttributes(PtContext &ctx, std::ostream &out, std::unordered_map<std::uint8_t, std::string> behaviorReverseMap,
const CompiledTileset &tileset);

/**
* TODO : fill in doc comment
*/
void emitDecompiled(PtContext &ctx, png::image<png::rgba_pixel> &bottom, png::image<png::rgba_pixel> &middle,
png::image<png::rgba_pixel> &top, const DecompiledTileset &tileset);

} // namespace porytiles

#endif // PORYTILES_EMITTER_H
10 changes: 10 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ struct RGBATile {
pixels.at(row * TILE_SIDE_LENGTH + col) = value;
}

bool equalsAfterBgrConversion(const RGBATile &other)
{
for (std::size_t i = 0; i < TILE_NUM_PIX; i++) {
if (rgbaToBgr(this->pixels.at(i)) != rgbaToBgr(other.pixels.at(i))) {
return false;
}
}
return true;
}

auto operator==(const RGBATile &other) const { return this->pixels == other.pixels; }

// Ignore the other fields for purposes of ordering the tiles
Expand Down
28 changes: 28 additions & 0 deletions src/decompiler.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "decompiler.h"

#include <cstdint>
#include <doctest.h>
#include <memory>

#include "compiler.h"
#include "importer.h"
#include "ptcontext.h"
#include "types.h"

Expand Down Expand Up @@ -37,3 +40,28 @@ std::unique_ptr<DecompiledTileset> decompile(PtContext &ctx, const CompiledTiles
}

} // namespace porytiles

TEST_CASE("decompile should decompile a basic tileset")
{
porytiles::PtContext ctx{};
ctx.fieldmapConfig.numPalettesInPrimary = 6;
ctx.fieldmapConfig.numPalettesTotal = 13;
ctx.compilerConfig.mode = porytiles::CompilerMode::PRIMARY;

REQUIRE(std::filesystem::exists("res/tests/simple_metatiles_2/primary/bottom.png"));
REQUIRE(std::filesystem::exists("res/tests/simple_metatiles_2/primary/middle.png"));
REQUIRE(std::filesystem::exists("res/tests/simple_metatiles_2/primary/top.png"));
png::image<png::rgba_pixel> bottomPrimary{"res/tests/simple_metatiles_2/primary/bottom.png"};
png::image<png::rgba_pixel> middlePrimary{"res/tests/simple_metatiles_2/primary/middle.png"};
png::image<png::rgba_pixel> topPrimary{"res/tests/simple_metatiles_2/primary/top.png"};
porytiles::DecompiledTileset decompiledPrimary = porytiles::importLayeredTilesFromPngs(
ctx, std::unordered_map<std::size_t, porytiles::Attributes>{}, bottomPrimary, middlePrimary, topPrimary);
auto compiledPrimary = porytiles::compile(ctx, decompiledPrimary);

auto decompiledViaAlgorithm = porytiles::decompile(ctx, *compiledPrimary);

CHECK(decompiledViaAlgorithm->tiles.size() == decompiledPrimary.tiles.size());
for (std::size_t i = 0; i < decompiledViaAlgorithm->tiles.size(); i++) {
CHECK(decompiledViaAlgorithm->tiles.at(i).equalsAfterBgrConversion(decompiledPrimary.tiles.at(i)));
}
}
10 changes: 10 additions & 0 deletions src/emitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ void emitAttributes(PtContext &ctx, std::ostream &out, std::unordered_map<std::u
out.flush();
}

void emitDecompiled(PtContext &ctx, png::image<png::rgba_pixel> &bottom, png::image<png::rgba_pixel> &middle,
png::image<png::rgba_pixel> &top, const DecompiledTileset &tileset) {
// TODO : this function needs to receive the attributes map so it knows the number of metatiles

// For now just assume triple layer
for(std::size_t metatileIndex = 0; metatileIndex < tileset.tiles.size() / 12; metatileIndex++) {

}
}

} // namespace porytiles

// --------------------
Expand Down

0 comments on commit 8d7c844

Please sign in to comment.