Skip to content

Commit 8ec4589

Browse files
authored
Merge pull request #42 from TomHarte/PaletteRotation
Fix palettisation, add manual hack for picking a good colour 0.
2 parents 3ce984b + 49aad93 commit 8ec4589

File tree

5 files changed

+10444
-10287
lines changed

5 files changed

+10444
-10287
lines changed

preprocessor/Map Preprocessor/AppDelegate.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ - (void)encode:(NSString *)directory {
377377
NSArray<NSString *> *sprite_files = [self spriteFiles:directory];
378378

379379
// Build palette based on tiles and sprites.
380-
Palettiser palettiser;
380+
Palettiser palettiser(4); // TODO: super-hack here; I'm supplying a rotation I picked to make sure that
381+
// colour 0 is the background one. That needs to be automated.
381382
for(NSString *file in [tile_files arrayByAddingObjectsFromArray:sprite_files]) {
382383
NSData *fileData = [NSData dataWithContentsOfFile:file];
383384
const PixelAccessor accessor([[NSImage alloc] initWithData:fileData]);

preprocessor/Map Preprocessor/Serialisers/Palettiser.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct Palette {
1818
template <size_t TargetCount = 16, size_t TargetOffset = 0>
1919
class Palettiser {
2020
public:
21+
Palettiser(size_t rotation) : rotation_(rotation) {}
22+
2123
void add_colour(uint32_t colour) {
2224
colours_.insert(colour);
2325
}
@@ -60,6 +62,10 @@ class Palettiser {
6062
Bucket *selected_bucket = nullptr;
6163
int max_range = 0;
6264
for(auto &bucket: buckets) {
65+
if(bucket.colours.size() == 1) {
66+
continue;
67+
}
68+
6369
uint32_t max = 0, min = 255;
6470
for(int index = 0; index < 24; index += 8) {
6571
for(const auto colour: bucket.colours) {
@@ -101,10 +107,15 @@ class Palettiser {
101107

102108
// Build final palette.
103109
Palette result;
110+
result.sam_palette.resize(TargetCount);
111+
uint8_t palette_index = rotation_;
104112
for(const auto &bucket: buckets) {
113+
const uint8_t index = palette_index + TargetOffset;
114+
palette_index = (palette_index + 1) % TargetCount;
115+
105116
uint32_t sum[3]{};
106117
for(const auto colour: bucket.colours) {
107-
result.source_mapping[colour] = static_cast<uint8_t>(result.sam_palette.size());
118+
result.source_mapping[colour] = index;
108119
sum[0] += (colour >> 0) & 0xff;
109120
sum[1] += (colour >> 8) & 0xff;
110121
sum[2] += (colour >> 16) & 0xff;
@@ -137,12 +148,13 @@ class Palettiser {
137148
// when there is meant to be none than it does to have the whole
138149
// colour be slightly darker.
139150
((bright >= 2 && red && green && blue) ? 0x01 : 0x00);
140-
result.sam_palette.push_back(sam_colour);
151+
result.sam_palette[index] = sam_colour;
141152
}
142153

143154
return result;
144155
}
145156

146157
private:
147158
std::unordered_set<uint32_t> colours_;
159+
size_t rotation_;
148160
};

src/generated/palette.z80s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
palette:
2-
db 0x00, 0x20, 0x40, 0x08, 0x00, 0x28, 0x58, 0x52, 0x12, 0x7e, 0x6c, 0x64, 0x00, 0x00, 0x00, 0x00
2+
db 0x5a, 0x1a, 0x63, 0x2c, 0x00, 0x20, 0x40, 0x08, 0x64, 0x24, 0x48, 0x52, 0x12, 0x7e, 0x6c, 0x0c

0 commit comments

Comments
 (0)