Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rubberduckycooly
GitHub Repository: rubberduckycooly/Sonic-1-2-2013-Decompilation
Path: blob/main/RSDKv4/Palette.hpp
817 views
1
#ifndef PALETTE_H
2
#define PALETTE_H
3
4
#define PALETTE_COUNT (0x8)
5
#define PALETTE_COLOR_COUNT (0x100)
6
7
struct Color {
8
byte r;
9
byte g;
10
byte b;
11
byte a;
12
};
13
14
struct PaletteEntry {
15
byte r;
16
byte g;
17
byte b;
18
};
19
20
// Palettes (as RGB565 Colors)
21
extern PaletteEntry fullPalette32[PALETTE_COUNT][PALETTE_COLOR_COUNT];
22
extern ushort fullPalette[PALETTE_COUNT][PALETTE_COLOR_COUNT];
23
extern ushort *activePalette; // Pointers to the 256 color set thats active
24
extern PaletteEntry *activePalette32;
25
26
extern byte gfxLineBuffer[SCREEN_YSIZE]; // Pointers to active palette
27
extern int GFX_LINESIZE;
28
extern int GFX_LINESIZE_MINUSONE;
29
extern int GFX_LINESIZE_DOUBLE;
30
extern int GFX_FRAMEBUFFERSIZE;
31
extern int GFX_FBUFFERMINUSONE;
32
33
extern uint gfxPalette16to32[0x10000];
34
35
extern int fadeMode;
36
extern byte fadeA;
37
extern byte fadeR;
38
extern byte fadeG;
39
extern byte fadeB;
40
41
extern int paletteMode;
42
43
#define RGB888_TO_RGB5551(r, g, b) (2 * ((b) >> 3) | ((g) >> 3 << 6) | ((r) >> 3 << 11) | 0) // used in mobile vers
44
#define RGB888_TO_RGB565(r, g, b) ((b) >> 3) | (((g) >> 2) << 5) | (((r) >> 3) << 11) // used in pc vers
45
46
#if RETRO_SOFTWARE_RENDER
47
#define PACK_RGB888(r, g, b) RGB888_TO_RGB565(r, g, b)
48
#elif RETRO_USING_OPENGL
49
#define PACK_RGB888(r, g, b) RGB888_TO_RGB5551(r, g, b)
50
#endif
51
52
void LoadPalette(const char *filePath, int paletteID, int startPaletteIndex, int startIndex, int endIndex);
53
54
inline void SetActivePalette(byte newActivePal, int startLine, int endLine)
55
{
56
#if RETRO_SOFTWARE_RENDER
57
if (newActivePal < PALETTE_COUNT)
58
for (int l = startLine; l < endLine && l < SCREEN_YSIZE; l++) gfxLineBuffer[l] = newActivePal;
59
60
activePalette = fullPalette[gfxLineBuffer[0]];
61
activePalette32 = fullPalette32[gfxLineBuffer[0]];
62
#endif
63
}
64
65
inline void SetPaletteEntry(byte paletteIndex, byte index, byte r, byte g, byte b)
66
{
67
if (paletteIndex != 0xFF) {
68
fullPalette[paletteIndex][index] = PACK_RGB888(r, g, b);
69
fullPalette32[paletteIndex][index].r = r;
70
fullPalette32[paletteIndex][index].g = g;
71
fullPalette32[paletteIndex][index].b = b;
72
}
73
else {
74
activePalette[index] = PACK_RGB888(r, g, b);
75
activePalette32[index].r = r;
76
activePalette32[index].g = g;
77
activePalette32[index].b = b;
78
}
79
}
80
81
inline void SetPaletteEntryPacked(byte paletteIndex, byte index, uint color)
82
{
83
fullPalette[paletteIndex][index] = PACK_RGB888((byte)(color >> 16), (byte)(color >> 8), (byte)(color >> 0));
84
85
fullPalette32[paletteIndex][index].r = (byte)(color >> 16);
86
fullPalette32[paletteIndex][index].g = (byte)(color >> 8);
87
fullPalette32[paletteIndex][index].b = (byte)(color >> 0);
88
}
89
90
inline uint GetPaletteEntryPacked(byte paletteIndex, byte index)
91
{
92
PaletteEntry clr = fullPalette32[paletteIndex][index];
93
return (clr.r << 16) | (clr.g << 8) | (clr.b);
94
}
95
96
inline void CopyPalette(byte sourcePalette, byte srcPaletteStart, byte destinationPalette, byte destPaletteStart, byte count)
97
{
98
if (sourcePalette < PALETTE_COUNT && destinationPalette < PALETTE_COUNT) {
99
for (int i = 0; i < count; ++i) {
100
fullPalette[destinationPalette][destPaletteStart + i] = fullPalette[sourcePalette][srcPaletteStart + i];
101
fullPalette32[destinationPalette][destPaletteStart + i] = fullPalette32[sourcePalette][srcPaletteStart + i];
102
}
103
}
104
}
105
106
inline void RotatePalette(int palID, byte startIndex, byte endIndex, bool right)
107
{
108
if (right) {
109
ushort startClr = fullPalette[palID][endIndex];
110
PaletteEntry startClr32 = fullPalette32[palID][endIndex];
111
for (int i = endIndex; i > startIndex; --i) {
112
fullPalette[palID][i] = fullPalette[palID][i - 1];
113
fullPalette32[palID][i] = fullPalette32[palID][i - 1];
114
}
115
fullPalette[palID][startIndex] = startClr;
116
fullPalette32[palID][startIndex] = startClr32;
117
}
118
else {
119
ushort startClr = fullPalette[palID][startIndex];
120
PaletteEntry startClr32 = fullPalette32[palID][startIndex];
121
for (int i = startIndex; i < endIndex; ++i) {
122
fullPalette[palID][i] = fullPalette[palID][i + 1];
123
fullPalette32[palID][i] = fullPalette32[palID][i + 1];
124
}
125
fullPalette[palID][endIndex] = startClr;
126
fullPalette32[palID][endIndex] = startClr32;
127
}
128
}
129
130
inline void SetFade(byte R, byte G, byte B, ushort A)
131
{
132
fadeMode = 1;
133
fadeR = R;
134
fadeG = G;
135
fadeB = B;
136
fadeA = A > 0xFF ? 0xFF : A;
137
}
138
139
#if RETRO_REV00
140
void SetLimitedFade(byte paletteID, byte R, byte G, byte B, ushort blendAmount, int startIndex, int endIndex);
141
#else
142
void SetPaletteFade(byte destPaletteID, byte srcPaletteA, byte srcPaletteB, ushort blendAmount, int startIndex, int endIndex);
143
#endif
144
145
#endif // !PALETTE_H
146