mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-29 22:37:50 +00:00
Use // line comments not /* block comments
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "gfx/main.hpp"
|
||||
|
||||
@@ -110,16 +110,13 @@ void Options::verbosePrint(uint8_t level, char const *fmt, ...) const {
|
||||
// Short options
|
||||
static char const *optstring = "-Aa:b:Cc:d:hi:L:mN:n:Oo:Pp:Qq:r:s:Tt:U:uVvXx:YZ";
|
||||
|
||||
/*
|
||||
* Equivalent long options
|
||||
* Please keep in the same order as short opts
|
||||
*
|
||||
* Also, make sure long opts don't create ambiguity:
|
||||
* A long opt's name should start with the same letter as its short opt,
|
||||
* except if it doesn't create any ambiguity (`verbose` versus `version`).
|
||||
* This is because long opt matching, even to a single char, is prioritized
|
||||
* over short opt matching
|
||||
*/
|
||||
// Equivalent long options
|
||||
// Please keep in the same order as short opts.
|
||||
// Also, make sure long opts don't create ambiguity:
|
||||
// A long opt's name should start with the same letter as its short opt,
|
||||
// except if it doesn't create any ambiguity (`verbose` versus `version`).
|
||||
// This is because long opt matching, even to a single char, is prioritized
|
||||
// over short opt matching.
|
||||
static option const longopts[] = {
|
||||
{"auto-attr-map", no_argument, nullptr, 'A'},
|
||||
{"attr-map", required_argument, nullptr, 'a'},
|
||||
@@ -173,10 +170,8 @@ static void printUsage() {
|
||||
);
|
||||
}
|
||||
|
||||
/*
|
||||
* Parses a number at the beginning of a string, moving the pointer to skip the parsed characters
|
||||
* Returns the provided errVal on error
|
||||
*/
|
||||
// Parses a number at the beginning of a string, moving the pointer to skip the parsed characters.
|
||||
// Returns the provided errVal on error.
|
||||
static uint16_t parseNumber(char *&string, char const *errPrefix, uint16_t errVal = UINT16_MAX) {
|
||||
uint8_t base = 10;
|
||||
if (*string == '\0') {
|
||||
@@ -199,12 +194,10 @@ static uint16_t parseNumber(char *&string, char const *errPrefix, uint16_t errVa
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Turns a digit into its numeric value in the current base, if it has one.
|
||||
* Maximum is inclusive. The string_view is modified to "consume" all digits.
|
||||
* Returns 255 on parse failure (including wrong char for base), in which case
|
||||
* the string_view may be pointing on garbage.
|
||||
*/
|
||||
// Turns a digit into its numeric value in the current base, if it has one.
|
||||
// Maximum is inclusive. The string_view is modified to "consume" all digits.
|
||||
// Returns 255 on parse failure (including wrong char for base), in which case
|
||||
// the string_view may be pointing on garbage.
|
||||
auto charIndex = [&base](unsigned char c) -> uint8_t {
|
||||
unsigned char index = c - '0'; // Use wrapping semantics
|
||||
if (base == 2 && index >= 2) {
|
||||
@@ -272,10 +265,8 @@ static void registerInput(char const *arg) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Turn an "at-file"'s contents into an argv that `getopt` can handle
|
||||
* @param argPool Argument characters will be appended to this vector, for storage purposes.
|
||||
*/
|
||||
// Turn an "at-file"'s contents into an argv that `getopt` can handle
|
||||
// @param argPool Argument characters will be appended to this vector, for storage purposes.
|
||||
static std::vector<size_t> readAtFile(std::string const &path, std::vector<char> &argPool) {
|
||||
File file;
|
||||
if (!file.open(path, std::ios_base::in)) {
|
||||
@@ -347,13 +338,10 @@ static std::vector<size_t> readAtFile(std::string const &path, std::vector<char>
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Parses an arg vector, modifying `options` and `localOptions` as options are read.
|
||||
* The `localOptions` struct is for flags which must be processed after the option parsing finishes.
|
||||
*
|
||||
* Returns `nullptr` if the vector was fully parsed, or a pointer (which is part of the arg vector)
|
||||
* to an "at-file" path if one is encountered.
|
||||
*/
|
||||
// Parses an arg vector, modifying `options` and `localOptions` as options are read.
|
||||
// The `localOptions` struct is for flags which must be processed after the option parsing finishes.
|
||||
// Returns `nullptr` if the vector was fully parsed, or a pointer (which is part of the arg vector)
|
||||
// to an "at-file" path if one is encountered.
|
||||
static char *parseArgv(int argc, char *argv[]) {
|
||||
for (int ch; (ch = musl_getopt_long_only(argc, argv, optstring, longopts, nullptr)) != -1;) {
|
||||
char *arg = musl_optarg; // Make a copy for scanning
|
||||
@@ -887,9 +875,7 @@ void Palette::addColor(uint16_t color) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the ID of the color in the palette, or `size()` if the color is not in
|
||||
*/
|
||||
// Returns the ID of the color in the palette, or `size()` if the color is not in
|
||||
uint8_t Palette::indexOf(uint16_t color) const {
|
||||
return color == Rgba::transparent
|
||||
? 0
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "gfx/pal_packing.hpp"
|
||||
|
||||
@@ -27,15 +27,11 @@
|
||||
// Tile | Proto-palette
|
||||
// Page | Palette
|
||||
|
||||
/*
|
||||
* A reference to a proto-palette, and attached attributes for sorting purposes
|
||||
*/
|
||||
// A reference to a proto-palette, and attached attributes for sorting purposes
|
||||
struct ProtoPalAttrs {
|
||||
size_t protoPalIndex;
|
||||
/*
|
||||
* Pages from which we are banned (to prevent infinite loops)
|
||||
* This is dynamic because we wish not to hard-cap the amount of palettes
|
||||
*/
|
||||
// Pages from which we are banned (to prevent infinite loops)
|
||||
// This is dynamic because we wish not to hard-cap the amount of palettes
|
||||
std::vector<bool> bannedPages;
|
||||
|
||||
explicit ProtoPalAttrs(size_t index) : protoPalIndex(index) {}
|
||||
@@ -50,10 +46,8 @@ struct ProtoPalAttrs {
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
* A collection of proto-palettes assigned to a palette
|
||||
* Does not contain the actual color indices because we need to be able to remove elements
|
||||
*/
|
||||
// A collection of proto-palettes assigned to a palette
|
||||
// Does not contain the actual color indices because we need to be able to remove elements
|
||||
class AssignedProtos {
|
||||
// We leave room for emptied slots to avoid copying the structs around on removal
|
||||
std::vector<std::optional<ProtoPalAttrs>> _assigned;
|
||||
@@ -127,10 +121,8 @@ public:
|
||||
}
|
||||
const_iterator end() const { return const_iterator{&_assigned, _assigned.end()}; }
|
||||
|
||||
/*
|
||||
* Assigns a new ProtoPalAttrs in a free slot, assuming there is one
|
||||
* Args are passed to the `ProtoPalAttrs`'s constructor
|
||||
*/
|
||||
// Assigns a new ProtoPalAttrs in a free slot, assuming there is one
|
||||
// Args are passed to the `ProtoPalAttrs`'s constructor
|
||||
template<typename... Ts>
|
||||
void assign(Ts &&...args) {
|
||||
auto freeSlot =
|
||||
@@ -192,9 +184,7 @@ private:
|
||||
return colors;
|
||||
}
|
||||
public:
|
||||
/*
|
||||
* Returns the number of distinct colors
|
||||
*/
|
||||
// Returns the number of distinct colors
|
||||
size_t volume() const { return uniqueColors().size(); }
|
||||
bool canFit(ProtoPalette const &protoPal) const {
|
||||
auto &colors = uniqueColors();
|
||||
@@ -218,10 +208,8 @@ public:
|
||||
return factor;
|
||||
}();
|
||||
|
||||
/*
|
||||
* Computes the "relative size" of a proto-palette on this palette;
|
||||
* it's a measure of how much this proto-palette would "cost" to introduce.
|
||||
*/
|
||||
// Computes the "relative size" of a proto-palette on this palette;
|
||||
// it's a measure of how much this proto-palette would "cost" to introduce.
|
||||
uint32_t relSizeOf(ProtoPalette const &protoPal) const {
|
||||
// NOTE: this function must not call `uniqueColors`, or one of its callers will break!
|
||||
|
||||
@@ -244,9 +232,7 @@ public:
|
||||
return relSize;
|
||||
}
|
||||
|
||||
/*
|
||||
* Computes the "relative size" of a set of proto-palettes on this palette
|
||||
*/
|
||||
// Computes the "relative size" of a set of proto-palettes on this palette
|
||||
template<typename Iter>
|
||||
auto combinedVolume(Iter &&begin, Iter const &end, std::vector<ProtoPalette> const &protoPals)
|
||||
const {
|
||||
@@ -254,9 +240,7 @@ public:
|
||||
addUniqueColors(colors, std::forward<Iter>(begin), end, protoPals);
|
||||
return colors.size();
|
||||
}
|
||||
/*
|
||||
* Computes the "relative size" of a set of colors on this palette
|
||||
*/
|
||||
// Computes the "relative size" of a set of colors on this palette
|
||||
template<typename Iter>
|
||||
auto combinedVolume(Iter &&begin, Iter &&end) const {
|
||||
auto &colors = uniqueColors();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "gfx/pal_sorting.hpp"
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "gfx/pal_spec.hpp"
|
||||
|
||||
@@ -189,11 +189,8 @@ static T readLE(U const *bytes) {
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* **Appends** the first line read from `file` to the end of the provided `buffer`.
|
||||
*
|
||||
* @return true if a line was read.
|
||||
*/
|
||||
// **Appends** the first line read from `file` to the end of the provided `buffer`.
|
||||
// @return true if a line was read.
|
||||
[[gnu::warn_unused_result]] static bool readLine(std::filebuf &file, std::string &buffer) {
|
||||
assume(buffer.empty());
|
||||
// TODO: maybe this can be optimized to bulk reads?
|
||||
@@ -222,9 +219,7 @@ static T readLE(U const *bytes) {
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Parses the initial part of a string_view, advancing the "read index" as it does
|
||||
*/
|
||||
// Parses the initial part of a string_view, advancing the "read index" as it does
|
||||
template<typename U> // Should be uint*_t
|
||||
static std::optional<U> parseDec(std::string const &str, std::string::size_type &n) {
|
||||
uintmax_t value = 0;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "gfx/process.hpp"
|
||||
|
||||
@@ -31,11 +31,9 @@ class ImagePalette {
|
||||
public:
|
||||
ImagePalette() = default;
|
||||
|
||||
/*
|
||||
* Registers a color in the palette.
|
||||
* If the newly inserted color "conflicts" with another one (different color, but same CGB
|
||||
* color), then the other color is returned. Otherwise, `nullptr` is returned.
|
||||
*/
|
||||
// Registers a color in the palette.
|
||||
// If the newly inserted color "conflicts" with another one (different color, but same CGB
|
||||
// color), then the other color is returned. Otherwise, `nullptr` is returned.
|
||||
[[nodiscard]] Rgba const *registerColor(Rgba const &rgba) {
|
||||
decltype(_colors)::value_type &slot = _colors[rgba.cgbColor()];
|
||||
|
||||
@@ -165,15 +163,13 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Reads a PNG and notes all of its colors
|
||||
*
|
||||
* This code is more complicated than strictly necessary, but that's because of the API
|
||||
* being used: the "high-level" interface doesn't provide all the transformations we need,
|
||||
* so we use the "lower-level" one instead.
|
||||
* We also use that occasion to only read the PNG one line at a time, since we store all of
|
||||
* the pixel data in `pixels`, which saves on memory allocations.
|
||||
*/
|
||||
// Reads a PNG and notes all of its colors
|
||||
//
|
||||
// This code is more complicated than strictly necessary, but that's because of the API
|
||||
// being used: the "high-level" interface doesn't provide all the transformations we need,
|
||||
// so we use the "lower-level" one instead.
|
||||
// We also use that occasion to only read the PNG one line at a time, since we store all of
|
||||
// the pixel data in `pixels`, which saves on memory allocations.
|
||||
explicit Png(std::string const &filePath) : path(filePath), colors() {
|
||||
if (file.open(path, std::ios_base::in | std::ios_base::binary) == nullptr) {
|
||||
fatal("Failed to open input image (\"%s\"): %s", file.c_str(path), strerror(errno));
|
||||
@@ -490,9 +486,7 @@ public:
|
||||
};
|
||||
|
||||
class RawTiles {
|
||||
/*
|
||||
* A tile which only contains indices into the image's global palette
|
||||
*/
|
||||
// A tile which only contains indices into the image's global palette
|
||||
class RawTile {
|
||||
std::array<std::array<size_t, 8>, 8> _pixelIndices{};
|
||||
|
||||
@@ -505,9 +499,7 @@ private:
|
||||
std::vector<RawTile> _tiles;
|
||||
|
||||
public:
|
||||
/*
|
||||
* Creates a new raw tile, and returns a reference to it so it can be filled in
|
||||
*/
|
||||
// Creates a new raw tile, and returns a reference to it so it can be filled in
|
||||
RawTile &newTile() {
|
||||
_tiles.emplace_back();
|
||||
return _tiles.back();
|
||||
@@ -515,11 +507,9 @@ public:
|
||||
};
|
||||
|
||||
struct AttrmapEntry {
|
||||
/*
|
||||
* This field can either be a proto-palette ID, or `transparent` to indicate that the
|
||||
* corresponding tile is fully transparent. If you are looking to get the palette ID for this
|
||||
* attrmap entry while correctly handling the above, use `getPalID`.
|
||||
*/
|
||||
// This field can either be a proto-palette ID, or `transparent` to indicate that the
|
||||
// corresponding tile is fully transparent. If you are looking to get the palette ID for this
|
||||
// attrmap entry while correctly handling the above, use `getPalID`.
|
||||
size_t protoPaletteID; // Only this field is used when outputting "unoptimized" data
|
||||
uint8_t tileID; // This is the ID as it will be output to the tilemap
|
||||
bool bank;
|
||||
@@ -918,9 +908,7 @@ struct UniqueTiles {
|
||||
UniqueTiles(UniqueTiles const &) = delete;
|
||||
UniqueTiles(UniqueTiles &&) = default;
|
||||
|
||||
/*
|
||||
* Adds a tile to the collection, and returns its ID
|
||||
*/
|
||||
// Adds a tile to the collection, and returns its ID
|
||||
std::tuple<uint16_t, TileData::MatchType> addTile(TileData newTile) {
|
||||
auto [tileData, inserted] = tileset.insert(newTile);
|
||||
|
||||
@@ -942,12 +930,10 @@ struct UniqueTiles {
|
||||
auto end() const { return tiles.end(); }
|
||||
};
|
||||
|
||||
/*
|
||||
* Generate tile data while deduplicating unique tiles (via mirroring if enabled)
|
||||
* Additionally, while we have the info handy, convert from the 16-bit "global" tile IDs to
|
||||
* 8-bit tile IDs + the bank bit; this will save the work when we output the data later (potentially
|
||||
* twice)
|
||||
*/
|
||||
// Generate tile data while deduplicating unique tiles (via mirroring if enabled)
|
||||
// Additionally, while we have the info handy, convert from the 16-bit "global" tile IDs to
|
||||
// 8-bit tile IDs + the bank bit; this will save the work when we output the data later (potentially
|
||||
// twice)
|
||||
static UniqueTiles dedupTiles(
|
||||
Png const &png,
|
||||
DefaultInitVec<AttrmapEntry> &attrmap,
|
||||
@@ -1164,18 +1150,17 @@ void process() {
|
||||
protoPalettes[n] = protoPalette; // Override them
|
||||
// Remove any other proto-palettes that we encompass
|
||||
// (Example [(0, 1), (0, 2)], inserting (0, 1, 2))
|
||||
/*
|
||||
* The following code does its job, except that references to the removed
|
||||
* proto-palettes are not updated, causing issues.
|
||||
* TODO: overlap might not be detrimental to the packing algorithm.
|
||||
* Investigation is necessary, especially if pathological cases are found.
|
||||
*
|
||||
* for (size_t i = protoPalettes.size(); --i != n;) {
|
||||
* if (protoPalette.compare(protoPalettes[i]) == ProtoPalette::WE_BIGGER) {
|
||||
* protoPalettes.erase(protoPalettes.begin() + i);
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
//
|
||||
// The following code does its job, except that references to the removed
|
||||
// proto-palettes are not updated, causing issues.
|
||||
// TODO: overlap might not be detrimental to the packing algorithm.
|
||||
// Investigation is necessary, especially if pathological cases are found.
|
||||
//
|
||||
// for (size_t i = protoPalettes.size(); --i != n;) {
|
||||
// if (protoPalette.compare(protoPalettes[i]) == ProtoPalette::WE_BIGGER) {
|
||||
// protoPalettes.erase(protoPalettes.begin() + i);
|
||||
// }
|
||||
// }
|
||||
[[fallthrough]];
|
||||
|
||||
case ProtoPalette::THEY_BIGGER:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "gfx/proto_palette.hpp"
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "gfx/reverse.hpp"
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* SPDX-License-Identifier: MIT */
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "gfx/rgba.hpp"
|
||||
|
||||
@@ -10,11 +10,9 @@
|
||||
|
||||
#include "gfx/main.hpp" // options
|
||||
|
||||
/*
|
||||
* Based on inverting the "Modern - Accurate" formula used by SameBoy
|
||||
* since commit b5a611c5db46d6a0649d04d24d8d6339200f9ca1 (Dec 2020),
|
||||
* with gaps in the scale curve filled by polynomial interpolation.
|
||||
*/
|
||||
// Based on inverting the "Modern - Accurate" formula used by SameBoy
|
||||
// since commit b5a611c5db46d6a0649d04d24d8d6339200f9ca1 (Dec 2020),
|
||||
// with gaps in the scale curve filled by polynomial interpolation.
|
||||
// clang-format off: vertically align columns of values
|
||||
static std::array<uint8_t, 256> reverse_curve{
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
|
||||
|
||||
Reference in New Issue
Block a user