-
Notifications
You must be signed in to change notification settings - Fork 0
/
corona_palette.cpp
182 lines (154 loc) · 4.84 KB
/
corona_palette.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* This file is released under the GNU General Public Licence
*
* authors:
* Richard Ashbury <[email protected]>
* Jean-Christophe Hoelt <[email protected]>
*/
#include "corona_palette.h"
#include <cstdlib>
#include <cstring>
///////////////////////////////////////////////////////////////////////////////
// Palette stored in compressed format (just checkpoints)
class CompressedPalette
{
public:
ColorRGB m_col[16];
int m_ind[16];
int m_nb;
CompressedPalette() : m_nb(0) {}
void push_color(int i, ColorRGB col) { m_col[m_nb] = col; m_ind[m_nb++] = i; }
void expand(Palette pal) const;
};
///////////////////////////////////////////////////////////////////////////////
PaletteCycler::PaletteCycler(const int palettes[][NB_PALETTES], int nbPalettes)
: m_palettes(palettes, nbPalettes)
{
for (int i=0;i<256;++i) {
m_curpal[i].rgbBlue = 0;
m_curpal[i].rgbGreen = 0;
m_curpal[i].rgbRed = 0;
}
startPaletteTransition();
affectPaletteTransition(1);
m_transferring = false;
m_srcnum = m_destnum;
}
void PaletteCycler::startPaletteTransition()
{
if (m_palettes.size() > 0)
{
// Copy the current palette to the "source palette"
memcpy(m_srcpal, m_curpal, sizeof(m_srcpal));
// Create a new palette as the "destination palette"
m_srcnum = m_destnum;
m_destnum = rand() % (int) m_palettes.size();
m_palettes.expandPalette(m_destnum, m_destpal);
// Begin the transition
m_transferring = true;
m_progress = 0;
}
}
void PaletteCycler::update(TimedLevel *pLevels)
{
// Randomly change the destination palette
if (pLevels->timeStamp - pLevels->lastbeat > 10000000) {
if (rand() % 100 == 0) startPaletteTransition();
}
else {
if (rand() % 400 == 0) startPaletteTransition();
}
// Continue any current palette transtion
if (m_transferring) {
if (pLevels->timeStamp - pLevels->lastbeat > 10000000) m_progress += 0.01;
else m_progress += 0.005;
if (m_progress >= 1) {
m_transferring = false;
m_progress = 1;
m_srcnum = m_destnum;
}
// Use an inverse sigmoid transition to emphasise the midway palette
double x;
if (m_progress < 0.5) x = 2 * m_progress * (1 - m_progress);
else x = 2 * m_progress * (m_progress - 1) + 1;
affectPaletteTransition(x);
}
}
void PaletteCycler::affectPaletteTransition(double p)
{
for (int i = 0; i < 256; ++i)
{
ColorRGB c1 = m_srcpal[i];
ColorRGB c2 = m_destpal[i];
m_curpal[i].rgbRed = (unsigned char) ((1 - p) * c1.rgbRed + p * c2.rgbRed);
m_curpal[i].rgbGreen = (unsigned char) ((1 - p) * c1.rgbGreen + p * c2.rgbGreen);
m_curpal[i].rgbBlue = (unsigned char) ((1 - p) * c1.rgbBlue + p * c2.rgbBlue);
}
}
///////////////////////////////////////////////////////////////////////////////
void CompressedPalette::expand(Palette dest) const
{
ColorRGB col;
int entry = 0;
col.rgbBlue = col.rgbGreen = col.rgbRed = 0;
int i = 0;
for (; i < m_nb; ++i)
{
int j = entry;
for (; j < m_ind[i]; ++j)
{
double t = (double) (j - entry) / (m_ind[i] - entry);
dest[j].rgbRed = (unsigned char) ((1 - t) * col.rgbRed + t * m_col[i].rgbRed);
dest[j].rgbGreen = (unsigned char) ((1 - t) * col.rgbGreen + t * m_col[i].rgbGreen);
dest[j].rgbBlue = (unsigned char) ((1 - t) * col.rgbBlue + t * m_col[i].rgbBlue);
}
entry = j;
col = m_col[i];
}
for (; entry < 256; ++entry) dest[entry] = col;
}
///////////////////////////////////////////////////////////////////////////////
PaletteCollection::PaletteCollection(const int palettes[][NB_PALETTES], int nbPalettes)
{
m_cpal = new CompressedPalette[nbPalettes];
m_nbPalettes = nbPalettes;
// Set up the palettes from the array
for (int i = 0; i < nbPalettes; ++i)
{
CompressedPalette newpal;
const int* pal = palettes[i];
for (int j = 1; j < pal[0] * 2; j += 2)
{
ColorRGB rgb;
rgb.rgbRed = (pal[j + 1] & 0xff0000) >> 16;
rgb.rgbGreen = (pal[j + 1] & 0xff00) >> 8;
rgb.rgbBlue = pal[j + 1] & 0xff;
newpal.push_color(pal[j], rgb);
}
m_cpal[i] = newpal;
}
}
PaletteCollection::~PaletteCollection()
{
delete[] m_cpal;
}
void PaletteCollection::expandPalette(int i, Palette dest) const
{
m_cpal[i].expand(dest);
}
///////////////////////////////////////////////////////////////////////////////
void blitSurface8To32(unsigned char *byteSurf, int *colorSurf, int palette[256], int size)
{
int i = 0;
while(size--) {
colorSurf[i++] = palette[byteSurf[size]];
}
}
void paletteToRGBA(int dest[256], const Palette src)
{
for (int i=0; i<256; ++i) {
int alpha = (i<128?i*2:255);
dest[i] = (alpha << 24) | ((int)src[i].rgbRed << 16) | ((int)src[i].rgbGreen << 8) | (int)src[i].rgbBlue;
/* dest[i] = ((int)i << 24) | ((int)src[i].rgbRed << 0) | ((int)src[i].rgbGreen << 8) | ((int)src[i].rgbBlue << 16); */
}
}