Implement enough functionality to compile & match pokecrystal

This commit is contained in:
ISSOtm
2022-03-04 23:49:55 +01:00
committed by Eldred Habert
parent 6e406b22bb
commit 3fa1854332
12 changed files with 460 additions and 183 deletions

View File

@@ -9,59 +9,6 @@
#ifndef RGBDS_GFX_CONVERT_HPP
#define RGBDS_GFX_CONVERT_HPP
#include <assert.h>
#include <stdint.h>
#include "gfx/main.hpp"
struct Rgba {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
Rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : red(r), green(g), blue(b), alpha(a) {}
Rgba(uint32_t rgba) : red(rgba), green(rgba >> 8), blue(rgba >> 16), alpha(rgba >> 24) {}
operator uint32_t() const { return toCSS(); }
/**
* Returns this RGBA as a 32-bit number that can be printed in hex (`%08x`) to yield its CSS
* representation
*/
uint32_t toCSS() const {
auto shl = [](uint8_t val, unsigned shift) { return static_cast<uint32_t>(val) << shift; };
return shl(red, 24) | shl(green, 16) | shl(blue, 8) | shl(alpha, 0);
}
bool operator!=(Rgba const &other) const {
return static_cast<uint32_t>(*this) != static_cast<uint32_t>(other);
}
bool isGray() const { return red == green && green == blue; }
/**
* CGB colors are RGB555, so we use bit 15 to signify that the color is transparent instead
* Since the rest of the bits don't matter then, we return 0x8000 exactly.
*/
static constexpr uint16_t transparent = 0b1'00000'00000'00000;
/**
* All alpha values strictly below this will be considered transparent
*/
static constexpr uint8_t opacity_threshold = 0xF0; // TODO: adjust this
/**
* Computes the equivalent CGB color, respects the color curve depending on options
*/
uint16_t cgbColor() const {
if (alpha < opacity_threshold)
return transparent;
if (options.useColorCurve) {
assert(!"TODO");
} else {
return (red >> 3) | (green >> 3) << 5 | (blue >> 3) << 10;
}
}
};
void process();
#endif /* RGBDS_GFX_CONVERT_HPP */

View File

@@ -13,22 +13,33 @@
#include <filesystem>
#include <limits.h>
#include <stdint.h>
#include <vector>
#include "helpers.h"
#include "gfx/rgba.hpp"
struct Options {
bool beVerbose = false; // -v
bool fixInput = false; // -f
bool columnMajor = false; // -h; whether to output the tilemap in columns instead of rows
bool columnMajor = false; // -Z, previously -h
bool allowMirroring = false; // -m
bool allowDedup = false; // -u
bool useColorCurve = false; // -C
uint8_t bitDepth = 2; // -d
uint64_t trim = 0; // -x
uint8_t nbPalettes = 8; // TODO
uint8_t nbColorsPerPal = 0; // TODO; 0 means "auto" = 1 << bitDepth;
std::array<uint8_t, 2> baseTileIDs{0, 0}; // TODO
std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // TODO
uint8_t nbPalettes = 8; // -n
uint8_t nbColorsPerPal = 0; // -s; 0 means "auto" = 1 << bitDepth;
enum {
NO_SPEC,
EXPLICIT,
EMBEDDED,
} palSpecType = NO_SPEC; // -c
std::vector<std::array<Rgba, 4>> palSpec{};
std::array<uint16_t, 2> unitSize{1, 1}; // -u (in tiles)
std::array<uint32_t, 4> inputSlice; // -L
std::array<uint8_t, 2> baseTileIDs{0, 0}; // -b
std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // -N
std::filesystem::path tilemap{}; // -t, -T
std::filesystem::path attrmap{}; // -a, -A
std::filesystem::path palettes{}; // -p, -P
@@ -37,8 +48,8 @@ struct Options {
format_(printf, 2, 3) void verbosePrint(char const *fmt, ...) const;
uint8_t maxPalSize() const {
return nbColorsPerPal;
} // TODO: minus 1 when transparency is active
return nbColorsPerPal; // TODO: minus 1 when transparency is active
}
};
extern Options options;
@@ -53,6 +64,8 @@ struct Palette {
void addColor(uint16_t color);
uint8_t indexOf(uint16_t color) const;
uint16_t &operator[](size_t index) { return colors[index]; }
uint16_t const &operator[](size_t index) const { return colors[index]; }
decltype(colors)::iterator begin();
decltype(colors)::iterator end();

View File

@@ -9,16 +9,22 @@
#ifndef RGBDS_GFX_PAL_SORTING_HPP
#define RGBDS_GFX_PAL_SORTING_HPP
#include <array>
#include <assert.h>
#include <optional>
#include <png.h>
#include <vector>
#include "gfx/rgba.hpp"
class Palette;
namespace sorting {
void indexed(std::vector<Palette> &palettes, int palSize, png_color const *palRGB,
png_byte *palAlpha);
void grayscale(std::vector<Palette> &palettes);
void grayscale(std::vector<Palette> &palettes,
std::array<std::optional<Rgba>, 0x8001> const &colors);
void rgb(std::vector<Palette> &palettes);
}

58
include/gfx/rgba.hpp Normal file
View File

@@ -0,0 +1,58 @@
/*
* This file is part of RGBDS.
*
* Copyright (c) 2022, Eldred Habert and RGBDS contributors.
*
* SPDX-License-Identifier: MIT
*/
#ifndef RGBDS_GFX_RGBA_HPP
#define RGBDS_GFX_RGBA_HPP
#include <stdint.h>
struct Rgba {
uint8_t red;
uint8_t green;
uint8_t blue;
uint8_t alpha;
Rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : red(r), green(g), blue(b), alpha(a) {}
/**
* Constructs the color from a "packed" RGBA representation (0xRRGGBBAA)
*/
explicit Rgba(uint32_t rgba = 0)
: red(rgba >> 24), green(rgba >> 16), blue(rgba >> 8), alpha(rgba) {}
/**
* Returns this RGBA as a 32-bit number that can be printed in hex (`%08x`) to yield its CSS
* representation
*/
uint32_t toCSS() const {
auto shl = [](uint8_t val, unsigned shift) { return static_cast<uint32_t>(val) << shift; };
return shl(red, 24) | shl(green, 16) | shl(blue, 8) | shl(alpha, 0);
}
friend bool operator!=(Rgba const &lhs, Rgba const &rhs) { return lhs.toCSS() != rhs.toCSS(); }
/**
* CGB colors are RGB555, so we use bit 15 to signify that the color is transparent instead
* Since the rest of the bits don't matter then, we return 0x8000 exactly.
*/
static constexpr uint16_t transparent = 0b1'00000'00000'00000;
/**
* All alpha values strictly below this will be considered transparent
*/
static constexpr uint8_t opacity_threshold = 0xF0; // TODO: adjust this
// TODO: also a transparency threshold, and error out on "middle" values
bool isTransparent() const { return alpha < opacity_threshold; }
/**
* Computes the equivalent CGB color, respects the color curve depending on options
*/
uint16_t cgbColor() const;
bool isGray() const { return red == green && green == blue; }
uint8_t grayIndex() const;
};
#endif /* RGBDS_GFX_RGBA_HPP */