mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 10:42:07 +00:00
Reimplement basic RGBGFX features in C++
Currently missing from the old version:
- `-f` ("fixing" the input image to be indexed)
- `-m` (the code for detecting mirrored tiles is missing, but all of the
"plumbing" is otherwise there)
- `-C`
- `-d`
- `-x` (though I need to check the exact functionality the old one has)
- Also the man page is still a draft and needs to be fleshed out
More planned features are not implemented yet either:
- Explicit palette spec
- Better error messages, also error "images"
- Better 8x16 support, as well as other "dedup unit" sizes
- Support for arbitrary number of palettes & colors per palette
- Other output formats (for example, a "full" palette map for "streaming"
use cases like gb-open-world)
- Quantization?
Some things may also be bugged:
- Transparency support
- Tile offsets (not exposed yet)
- Tile counts per bank (not exposed yet)
...and performance remains to be checked.
We need to set up some tests, honestly.
This commit is contained in:
79
src/gfx/proto_palette.cpp
Normal file
79
src/gfx/proto_palette.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* This file is part of RGBDS.
|
||||
*
|
||||
* Copyright (c) 2022, Eldred Habert and RGBDS contributors.
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include "gfx/proto_palette.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
bool ProtoPalette::add(uint16_t color) {
|
||||
size_t i = 0;
|
||||
|
||||
// Seek the first slot greater than our color
|
||||
// (A linear search is better because we don't store the array size,
|
||||
// and there are very few slots anyway)
|
||||
while (_colorIndices[i] < color) {
|
||||
++i;
|
||||
if (i == _colorIndices.size())
|
||||
return false; // EOF
|
||||
}
|
||||
// If we found ourselves, great!
|
||||
if (_colorIndices[i] == color)
|
||||
return true;
|
||||
|
||||
// Swap entries until the end
|
||||
while (_colorIndices[i] != UINT16_MAX) {
|
||||
std::swap(_colorIndices[i], color);
|
||||
++i;
|
||||
if (i == _colorIndices.size())
|
||||
return false; // Oh well
|
||||
}
|
||||
// Write that last one into the new slot
|
||||
_colorIndices[i] = color;
|
||||
return true;
|
||||
}
|
||||
|
||||
ProtoPalette::ComparisonResult ProtoPalette::compare(ProtoPalette const &other) const {
|
||||
auto ours = _colorIndices.begin(), theirs = other._colorIndices.begin();
|
||||
bool weBigger = true, theyBigger = true;
|
||||
|
||||
while (ours != _colorIndices.end() && theirs != other._colorIndices.end()) {
|
||||
if (*ours == *theirs) {
|
||||
++ours;
|
||||
++theirs;
|
||||
} else if (*ours < *theirs) {
|
||||
++ours;
|
||||
theyBigger = false;
|
||||
} else {
|
||||
++theirs;
|
||||
weBigger = false;
|
||||
}
|
||||
}
|
||||
weBigger &= ours == _colorIndices.end();
|
||||
theyBigger &= theirs == other._colorIndices.end();
|
||||
|
||||
return theyBigger ? THEY_BIGGER : (weBigger ? WE_BIGGER : NEITHER);
|
||||
}
|
||||
|
||||
ProtoPalette &ProtoPalette::operator=(ProtoPalette const &other) {
|
||||
_colorIndices = other._colorIndices;
|
||||
return *this;
|
||||
}
|
||||
|
||||
size_t ProtoPalette::size() const {
|
||||
return std::distance(begin(), end());
|
||||
}
|
||||
|
||||
auto ProtoPalette::begin() const -> decltype(_colorIndices)::const_iterator {
|
||||
return _colorIndices.begin();
|
||||
}
|
||||
auto ProtoPalette::end() const -> decltype(_colorIndices)::const_iterator {
|
||||
return std::find(_colorIndices.begin(), _colorIndices.end(), UINT16_MAX);
|
||||
}
|
||||
Reference in New Issue
Block a user