Skip to content

Commit

Permalink
Importer for basic pokeemerald set working
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Sep 1, 2023
1 parent be5fb62 commit 397cd1a
Show file tree
Hide file tree
Showing 22 changed files with 372 additions and 27 deletions.
2 changes: 1 addition & 1 deletion include/importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ 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);
CompiledTileset importCompiledTileset(PtContext &ctx, const std::filesystem::path &tilesetPath);

} // namespace porytiles

Expand Down
6 changes: 4 additions & 2 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ std::string subtileString(Subtile subtile);
enum class LayerType { NORMAL, COVERED, SPLIT, TRIPLE };
std::string layerTypeString(LayerType layerType);
std::uint8_t layerTypeValue(LayerType layerType);
LayerType layerTypeFromInt(std::uint8_t layerInt);

enum class EncounterType { NONE, LAND, WATER };
std::uint8_t encounterTypeValue(EncounterType encounterType);
Expand Down Expand Up @@ -760,10 +761,11 @@ struct CompilerConfig {
};

struct CompilerContext {
std::unique_ptr<CompiledTileset> pairedPrimaryTiles;
std::unique_ptr<CompiledTileset> pairedPrimaryTileset;
std::unique_ptr<CompiledTileset> resultTileset;
std::unordered_map<BGR15, std::tuple<RGBA32, RGBATile, std::size_t, std::size_t>> bgrToRgba;

CompilerContext() : pairedPrimaryTiles{nullptr}, bgrToRgba{} {}
CompilerContext() : pairedPrimaryTileset{nullptr}, resultTileset{nullptr}, bgrToRgba{} {}
};

} // namespace porytiles
Expand Down
Binary file not shown.
Binary file not shown.
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/00.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/01.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
248 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/02.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
96 160 216
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/03.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
64 72 104
200 80 72
248 200 136
216 104 96
232 144 112
168 32 40
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/04.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
112 192 160
56 192 128
56 136 96
112 224 160
56 88 56
48 136 40
64 56 48
88 96 48
136 128 48
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/05.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
112 192 160
160 224 192
56 136 96
224 200 96
112 224 160
240 232 128
240 240 248
64 72 104
120 120 128
144 160 176
200 216 232
88 88 112
168 184 200
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/06.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/07.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/08.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/09.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/10.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/11.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
19 changes: 19 additions & 0 deletions res/tests/compiled/simple_primary_1/palettes/12.pal
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
JASC-PAL
0100
16
248 0 248
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Binary file added res/tests/compiled/simple_primary_1/tiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 13 additions & 13 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ static void assignTilesSecondary(PtContext &ctx, CompiledTileset &compiled,
// Create the GBATile for this tile's key frame
GBATile keyFrameTile = makeTile(normTile, NormalizedTile::keyFrameIndex(), compiled.palettes[paletteIndex]);

if (ctx.compilerContext.pairedPrimaryTiles->tileIndexes.contains(keyFrameTile)) {
if (ctx.compilerContext.pairedPrimaryTiles->tileIndexes.at(keyFrameTile) == 0) {
if (ctx.compilerContext.pairedPrimaryTileset->tileIndexes.contains(keyFrameTile)) {
if (ctx.compilerContext.pairedPrimaryTileset->tileIndexes.at(keyFrameTile) == 0) {
/*
* Fatal error if the user provided a transparent key frame tile. This is not allowed, since there would be no
* way to tell if a transparent user provided tile on the layer sheet referred to the true index 0 transparent
Expand Down Expand Up @@ -703,9 +703,9 @@ static void assignTilesSecondary(PtContext &ctx, CompiledTileset &compiled,
usedKeyFrameTiles.at(gbaTile) = true;
}

if (ctx.compilerContext.pairedPrimaryTiles->tileIndexes.contains(gbaTile)) {
if (ctx.compilerContext.pairedPrimaryTileset->tileIndexes.contains(gbaTile)) {
// Tile was in the primary set
compiled.assignments.at(index.tileIndex) = {ctx.compilerContext.pairedPrimaryTiles->tileIndexes.at(gbaTile),
compiled.assignments.at(index.tileIndex) = {ctx.compilerContext.pairedPrimaryTileset->tileIndexes.at(gbaTile),
paletteIndex, normTile.hFlip, normTile.vFlip};
}
else {
Expand Down Expand Up @@ -749,10 +749,10 @@ static void assignTilesSecondary(PtContext &ctx, CompiledTileset &compiled,
std::unique_ptr<CompiledTileset> compile(PtContext &ctx, const DecompiledTileset &decompiledTileset)
{
if (ctx.compilerConfig.mode == CompilerMode::SECONDARY &&
(ctx.fieldmapConfig.numPalettesInPrimary != ctx.compilerContext.pairedPrimaryTiles->palettes.size())) {
(ctx.fieldmapConfig.numPalettesInPrimary != ctx.compilerContext.pairedPrimaryTileset->palettes.size())) {
internalerror_numPalettesInPrimaryNeqPrimaryPalettesSize("compiler::compile",
ctx.fieldmapConfig.numPalettesInPrimary,
ctx.compilerContext.pairedPrimaryTiles->palettes.size());
ctx.compilerContext.pairedPrimaryTileset->palettes.size());
}

auto compiled = std::make_unique<CompiledTileset>();
Expand Down Expand Up @@ -790,7 +790,7 @@ std::unique_ptr<CompiledTileset> compile(PtContext &ctx, const DecompiledTileset
std::unordered_map<BGR15, std::size_t> emptyPrimaryColorIndexMap;
const std::unordered_map<BGR15, std::size_t> *primaryColorIndexMap = &emptyPrimaryColorIndexMap;
if (ctx.compilerConfig.mode == CompilerMode::SECONDARY) {
primaryColorIndexMap = &(ctx.compilerContext.pairedPrimaryTiles->colorIndexMap);
primaryColorIndexMap = &(ctx.compilerContext.pairedPrimaryTileset->colorIndexMap);
}
auto [colorToIndex, indexToColor] = buildColorIndexMaps(ctx, indexedNormTiles, *primaryColorIndexMap);
compiled->colorIndexMap = colorToIndex;
Expand Down Expand Up @@ -830,9 +830,9 @@ std::unique_ptr<CompiledTileset> compile(PtContext &ctx, const DecompiledTileset
* primary palette and hence does not need to extend the search by assigning its colors to one of the new secondary
* palettes.
*/
primaryPaletteColorSets.reserve(ctx.compilerContext.pairedPrimaryTiles->palettes.size());
for (std::size_t i = 0; i < ctx.compilerContext.pairedPrimaryTiles->palettes.size(); i++) {
const auto &gbaPalette = ctx.compilerContext.pairedPrimaryTiles->palettes.at(i);
primaryPaletteColorSets.reserve(ctx.compilerContext.pairedPrimaryTileset->palettes.size());
for (std::size_t i = 0; i < ctx.compilerContext.pairedPrimaryTileset->palettes.size(); i++) {
const auto &gbaPalette = ctx.compilerContext.pairedPrimaryTileset->palettes.at(i);
primaryPaletteColorSets.emplace_back();
for (std::size_t j = 1; j < gbaPalette.size; j++) {
primaryPaletteColorSets.at(i).set(colorToIndex.at(gbaPalette.colors.at(j)));
Expand Down Expand Up @@ -874,7 +874,7 @@ std::unique_ptr<CompiledTileset> compile(PtContext &ctx, const DecompiledTileset
for (std::size_t i = 0; i < ctx.fieldmapConfig.numPalettesInPrimary; i++) {
// Copy the primary set's palettes into this tileset so tiles can use them
for (std::size_t j = 0; j < PAL_SIZE; j++) {
compiled->palettes.at(i).colors.at(j) = ctx.compilerContext.pairedPrimaryTiles->palettes.at(i).colors.at(j);
compiled->palettes.at(i).colors.at(j) = ctx.compilerContext.pairedPrimaryTileset->palettes.at(i).colors.at(j);
}
}
for (std::size_t i = ctx.fieldmapConfig.numPalettesInPrimary; i < ctx.fieldmapConfig.numPalettesTotal; i++) {
Expand Down Expand Up @@ -1861,7 +1861,7 @@ TEST_CASE("compile function should fill out secondary CompiledTileset struct wit
porytiles::DecompiledTileset decompiledSecondary = porytiles::importLayeredTilesFromPngs(
ctx, std::unordered_map<std::size_t, porytiles::Attributes>{}, bottomSecondary, middleSecondary, topSecondary);
ctx.compilerConfig.mode = porytiles::CompilerMode::SECONDARY;
ctx.compilerContext.pairedPrimaryTiles = std::move(compiledPrimary);
ctx.compilerContext.pairedPrimaryTileset = std::move(compiledPrimary);
auto compiledSecondary = porytiles::compile(ctx, decompiledSecondary);

// Check that tiles are as expected
Expand Down Expand Up @@ -2261,7 +2261,7 @@ TEST_CASE("compile function should correctly compile secondary set with animated
porytiles::importAnimTiles(ctx, anims, decompiledPrimary);

auto compiledPrimary = porytiles::compile(ctx, decompiledPrimary);
ctx.compilerContext.pairedPrimaryTiles = std::move(compiledPrimary);
ctx.compilerContext.pairedPrimaryTileset = std::move(compiledPrimary);
ctx.compilerConfig.mode = porytiles::CompilerMode::SECONDARY;

REQUIRE(std::filesystem::exists("res/tests/anim_metatiles_1/secondary/bottom.png"));
Expand Down
Loading

0 comments on commit 397cd1a

Please sign in to comment.