Skip to content

Commit

Permalink
Working on compiled palette input
Browse files Browse the repository at this point in the history
  • Loading branch information
grunt-lucas committed Sep 2, 2023
1 parent 64c34fe commit 09c1406
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ struct GBAPalette {
std::array<BGR15, PAL_SIZE> colors;

GBAPalette() : size{}, colors{} {}

// TODO : should this also look at the size field? we are only really using it for unit testing
auto operator==(const GBAPalette &other) const { return this->colors == other.colors; }
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ static void driveCompile(PtContext &ctx)
fmt::format("{}: could not open for reading", ctx.srcPaths.primaryMetatileBehaviors().string()));
}
auto [map, reverse] = importMetatileBehaviorMaps(ctx, behaviorFile);
behaviorFile.close();
behaviorMap = map;
behaviorReverseMap = reverse;
}
Expand Down
2 changes: 2 additions & 0 deletions src/errors_warnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,13 +973,15 @@ TEST_CASE("fatalerror_invalidBehaviorValue should trigger when the metatile beha
std::ifstream behaviorFile{"res/tests/metatile_behaviors_invalid_1.h"};
CHECK_THROWS_WITH_AS(porytiles::importMetatileBehaviorMaps(ctx, behaviorFile), "invalid behavior value foo",
porytiles::PtException);
behaviorFile.close();
}

SUBCASE("Invalid integer format 2")
{
std::ifstream behaviorFile{"res/tests/metatile_behaviors_invalid_2.h"};
CHECK_THROWS_WITH_AS(porytiles::importMetatileBehaviorMaps(ctx, behaviorFile), "invalid behavior value 6bar",
porytiles::PtException);
behaviorFile.close();
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define FMT_HEADER_ONLY
#include <fmt/color.h>

#include <algorithm>
#include <bitset>
#include <csv.h>
#include <doctest.h>
Expand Down Expand Up @@ -571,6 +572,22 @@ importAttributesFromCsv(PtContext &ctx, const std::unordered_map<std::string, st
return attributeMap;
}

static std::vector<GBAPalette> importCompiledPalettes(PtContext &ctx,
std::vector<std::shared_ptr<std::ifstream>> &paletteFiles)
{
std::vector<GBAPalette> palettes{};

for (std::shared_ptr<std::ifstream> stream : paletteFiles) {
std::string line;
while (std::getline(*stream, line)) {
// TODO : read lines instead of printing
std::cout << line << std::endl;
}
}

return palettes;
}

static std::vector<GBATile> importCompiledTiles(PtContext &ctx, const png::image<png::index_pixel> &tiles)
{
std::vector<GBATile> gbaTiles{};
Expand Down Expand Up @@ -690,10 +707,26 @@ CompiledTileset importCompiledTileset(PtContext &ctx, const std::filesystem::pat
std::ifstream metatiles{tilesetPath / "metatiles.bin", std::ios::binary};
std::ifstream attributes{tilesetPath / "metatile_attributes.bin", std::ios::binary};
png::image<png::index_pixel> tilesheetPng{tilesetPath / "tiles.png"};
std::vector<std::shared_ptr<std::ifstream>> paletteFiles{};

for (std::size_t index = 0; index < ctx.fieldmapConfig.numPalettesTotal; index++) {
std::ostringstream filename;
if (index < 10) {
filename << "0";
}
filename << index << ".pal";
paletteFiles.push_back(std::make_shared<std::ifstream>(tilesetPath / "palettes" / filename.str()));
}

tileset.tiles = importCompiledTiles(ctx, tilesheetPng);
tileset.palettes = importCompiledPalettes(ctx, paletteFiles);
tileset.assignments = importCompiledMetatilesAndAttrs(ctx, metatiles, attributes);

metatiles.close();
attributes.close();
std::for_each(paletteFiles.begin(), paletteFiles.end(),
[](std::shared_ptr<std::ifstream> stream) { stream->close(); });

return tileset;
}

Expand Down Expand Up @@ -1126,6 +1159,7 @@ TEST_CASE("importMetatileBehaviorMaps should parse metatile behaviors as expecte

std::ifstream behaviorFile{"res/tests/metatile_behaviors.h"};
auto [behaviorMap, behaviorReverseMap] = porytiles::importMetatileBehaviorMaps(ctx, behaviorFile);
behaviorFile.close();

CHECK(!behaviorMap.contains("MB_INVALID"));
CHECK(behaviorMap.at("MB_NORMAL") == 0x00);
Expand Down

0 comments on commit 09c1406

Please sign in to comment.