[Decompilation] Encode the per-component range in the RGB color template

Allowing us to then retrieve it using a function call with no run-time
cost, although we do have to be careful with the types here.
Also, is that another solution to decompilation puzzles that involve
types of number literals?

Part of P0080, funded by Ember2528 and Splashman.
This commit is contained in:
nmlgc 2020-03-01 12:24:18 +01:00
parent f2b454dfc6
commit 3229b2285d
3 changed files with 19 additions and 6 deletions

View File

@ -17,7 +17,7 @@
#ifdef __cplusplus #ifdef __cplusplus
// master.lib palettes use twice the bits per RGB component for more // master.lib palettes use twice the bits per RGB component for more
// toning precision // toning precision
typedef RGB<uint8_t> RGB8; typedef RGB<uint8_t, 256> RGB8;
typedef Palette<RGB8> Palette8; typedef Palette<RGB8> Palette8;
#endif #endif
// --------------------- // ---------------------

17
pc98.h
View File

@ -25,16 +25,29 @@ typedef bool page_t;
#pragma option -a1 #pragma option -a1
#ifdef __cplusplus #ifdef __cplusplus
template <class ComponentType> union RGB { template <class ComponentType, int Range> union RGB {
struct { struct {
ComponentType r, g, b; ComponentType r, g, b;
} c; } c;
ComponentType v[3]; ComponentType v[3];
// Yes, we actually need this function in certain cases where code
// generation calls for a 0 in the ComponentType.
static ComponentType min() {
return 0;
}
static ComponentType max() {
return (Range - 1);
}
}; };
template <class RGBType> struct Palette { template <class RGBType> struct Palette {
RGBType colors[COLOR_COUNT]; RGBType colors[COLOR_COUNT];
static int range() {
return RGBType::Range;
}
RGBType& operator [](int col) { RGBType& operator [](int col) {
return colors[col]; return colors[col];
} }
@ -44,7 +57,7 @@ typedef bool page_t;
// 4,096 colors // 4,096 colors
typedef int8_t uint4_t; typedef int8_t uint4_t;
typedef RGB<uint4_t> RGB4; typedef RGB<uint4_t, 16> RGB4;
typedef Palette<RGB4> Palette4; typedef Palette<RGB4> Palette4;
#endif #endif

View File

@ -547,7 +547,7 @@ void z_palette_fade_from(
unsigned int step_ms unsigned int step_ms
) )
{ {
RGB4 fadepal[COLOR_COUNT]; Palette4 fadepal;
int i; int i;
int col; int col;
int comp; int comp;
@ -564,12 +564,12 @@ void z_palette_fade_from(
fadepal[i].c.b = z_Palettes[i].c.b; fadepal[i].c.b = z_Palettes[i].c.b;
} }
} }
for(i = 0; i < 16; i++) { for(i = 0; i < fadepal.range(); i++) {
z_vsync_wait(); z_vsync_wait();
for(col = 0; col < COLOR_COUNT; col++) { for(col = 0; col < COLOR_COUNT; col++) {
for(comp = 0; comp < sizeof(RGB4); comp++) { for(comp = 0; comp < sizeof(RGB4); comp++) {
if(fadepal[col].v[comp] != z_Palettes[col].v[comp]) { if(fadepal[col].v[comp] != z_Palettes[col].v[comp]) {
fadepal[col].v[comp] += fadepal.colors[col].v[comp] +=
(fadepal[col].v[comp] < z_Palettes[col].v[comp]) (fadepal[col].v[comp] < z_Palettes[col].v[comp])
? 1 ? 1
: -1; : -1;