[Decompilation] Move palette loops to common macros

You'd really want to overload `operator =` for those.

Part of P0165, funded by Ember2528.
This commit is contained in:
nmlgc 2021-10-31 01:11:05 +02:00
parent 629bf7b08e
commit 7cc0189439
6 changed files with 46 additions and 37 deletions

19
pc98.h
View File

@ -117,6 +117,25 @@ typedef int8_t uint4_t;
typedef RGB<uint4_t, 16> RGB4; typedef RGB<uint4_t, 16> RGB4;
typedef Palette<RGB4> Palette4; typedef Palette<RGB4> Palette4;
#define palette_foreach(tmp_col, tmp_comp, func) { \
for(tmp_col = 0; tmp_col < COLOR_COUNT; tmp_col++) { \
for(tmp_comp = 0; tmp_comp < COMPONENT_COUNT; tmp_comp++) { \
func \
} \
} \
}
// Sets all components of all colors to the given grayscale [value].
#define palette_set_grayscale(dst, value, tmp_col, tmp_comp) \
palette_foreach(tmp_col, tmp_comp, { \
dst[tmp_col].v[tmp_comp] = value; \
})
#define palette_copy(dst, src, tmp_col, tmp_comp) \
palette_foreach(tmp_col, tmp_comp, { \
dst[col].v[comp] = src[col].v[comp]; \
})
#endif #endif
/// -------- /// --------

View File

@ -58,11 +58,9 @@ int getkanji(FILE *fp)
void grp_palette_set_all(const Palette4& pal) void grp_palette_set_all(const Palette4& pal)
{ {
for(int col = 0; col < COLOR_COUNT; col++) { int col;
for(int comp = 0; comp < COMPONENT_COUNT; comp++) { int comp;
grp_palette[col].v[comp] = pal[col].v[comp]; palette_copy(grp_palette, pal, col, comp);
}
}
} }
int grp_put_palette_show(const char *fn) int grp_put_palette_show(const char *fn)

View File

@ -44,19 +44,16 @@ void z_palette_white_in(void);
void z_palette_black_out(void); void z_palette_black_out(void);
void z_palette_white_out(void); void z_palette_white_out(void);
#define z_Palettes_set_func_and_show(tmp_col, tmp_comp, func) \ #define z_Palettes_set_func_and_show(tmp_col, tmp_comp, func) { \
for(tmp_col = 0; tmp_col < COLOR_COUNT; tmp_col++) { \ palette_foreach(tmp_col, tmp_comp, func); \
for(tmp_comp = 0; tmp_comp < COMPONENT_COUNT; tmp_comp++) { \ z_palette_set_all_show(z_Palettes); \
func \ }
} \
} \
z_palette_set_all_show(z_Palettes);
// Sets all colors in both the hardware palette and z_Palettes to #000. // Sets all colors in both the hardware palette and z_Palettes to #000.
#define z_palette_set_black(tmp_col, tmp_comp) \ #define z_palette_set_black(tmp_col, tmp_comp) { \
z_Palettes_set_func_and_show(tmp_col, tmp_comp, { \ palette_set_grayscale(z_Palettes, 0, tmp_col, tmp_comp); \
z_Palettes[tmp_col].v[tmp_comp] = 0; \ z_palette_set_all_show(z_Palettes); \
}) }
// Performs a single black-out step. // Performs a single black-out step.
#define z_palette_black_out_step(tmp_col, tmp_comp) \ #define z_palette_black_out_step(tmp_col, tmp_comp) \

View File

@ -75,17 +75,15 @@ void pagetrans_diagonal_8x8_with_palette(
: (tip_top + INTERLEAVE_H); : (tip_top + INTERLEAVE_H);
if((frame % (FRAME_COUNT / COLOR_COUNT)) == 0) { if((frame % (FRAME_COUNT / COLOR_COUNT)) == 0) {
for(col = 0; col < COLOR_COUNT; col++) { // Not identical to the ramping loop in z_palette_fade_from()!
// Not identical to the ramping loop in z_palette_fade_from()! palette_foreach(col, comp, {
for(comp = 0; comp < COMPONENT_COUNT; comp++) { if(pal_cur[col].v[comp] != pal_target[col].v[comp]) {
if(pal_cur[col].v[comp] != pal_target[col].v[comp]) { pal_cur.colors[col].v[comp] +=
pal_cur.colors[col].v[comp] += (pal_cur[col].v[comp] > pal_target[col].v[comp])
(pal_cur[col].v[comp] > pal_target[col].v[comp]) ? -1
? -1 : 1;
: 1;
}
} }
} });
z_palette_set_all_show(pal_cur); z_palette_set_all_show(pal_cur);
} }

View File

@ -1,12 +1,11 @@
// The intended palette for a boss, with no effects applied. // The intended palette for a boss, with no effects applied.
extern Palette4 boss_palette; extern Palette4 boss_palette;
#define boss_palette_snap_inlined() \ #define boss_palette_snap_inlined() { \
for(int col = 0; col < COLOR_COUNT; col++) { \ int col; \
for(int comp = 0; comp < COMPONENT_COUNT; comp++) { \ int comp; \
boss_palette[col].v[comp] = z_Palettes[col].v[comp]; \ palette_copy(boss_palette, z_Palettes, col, comp); \
} \ }
}
// Overwrites [boss_palette] with [z_Palettes]. // Overwrites [boss_palette] with [z_Palettes].
void boss_palette_snap(void); void boss_palette_snap(void);

View File

@ -2,9 +2,7 @@
void stage_palette_set(const Palette4 &pal) void stage_palette_set(const Palette4 &pal)
{ {
for(int col = 0; col < COLOR_COUNT; col++) { int col;
for(int comp = 0; comp < COMPONENT_COUNT; comp++) { int comp;
stage_palette[col].v[comp] = pal[col].v[comp]; palette_copy(stage_palette, pal, col, comp);
}
}
} }