2
* OpenTyrian: A modern cross-platform port of Tyrian
3
* Copyright (C) 2007-2009 The OpenTyrian Development Team
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27
static Uint32 rgb_to_yuv( int r, int g, int b );
30
#define PALETTE_COUNT 24
32
#define PALETTE_COUNT 23
35
Palette palettes[PALETTE_COUNT];
38
static Palette palette;
39
Uint32 rgb_palette[256], yuv_palette[256];
43
void JE_loadPals( void )
45
FILE *f = dir_fopen_die(data_dir(), "palette.dat", "rb");
47
palette_count = ftell_eof(f) / (256 * 3);
48
assert(palette_count == PALETTE_COUNT);
50
for (int p = 0; p < palette_count; ++p)
52
for (int i = 0; i < 256; ++i)
54
palettes[p][i].r = getc(f) << 2;
55
palettes[p][i].g = getc(f) << 2;
56
palettes[p][i].b = getc(f) << 2;
63
void set_palette( Palette colors, unsigned int first_color, unsigned int last_color )
65
SDL_Surface *const surface = SDL_GetVideoSurface();
66
const uint bpp = surface->format->BitsPerPixel;
68
for (uint i = first_color; i <= last_color; ++i)
70
palette[i] = colors[i];
74
rgb_palette[i] = SDL_MapRGB(surface->format, palette[i].r, palette[i].g, palette[i].b);
75
yuv_palette[i] = rgb_to_yuv(palette[i].r, palette[i].g, palette[i].b);
80
SDL_SetColors(surface, palette, first_color, last_color - first_color + 1);
83
void set_colors( SDL_Color color, unsigned int first_color, unsigned int last_color )
85
SDL_Surface *const surface = SDL_GetVideoSurface();
86
const uint bpp = surface->format->BitsPerPixel;
88
for (uint i = first_color; i <= last_color; ++i)
94
rgb_palette[i] = SDL_MapRGB(surface->format, palette[i].r, palette[i].g, palette[i].b);
95
yuv_palette[i] = rgb_to_yuv(palette[i].r, palette[i].g, palette[i].b);
100
SDL_SetColors(surface, palette, first_color, last_color - first_color + 1);
103
void init_step_fade_palette( int diff[256][3], Palette colors, unsigned int first_color, unsigned int last_color )
105
for (unsigned int i = first_color; i <= last_color; i++)
107
diff[i][0] = (int)colors[i].r - palette[i].r;
108
diff[i][1] = (int)colors[i].g - palette[i].g;
109
diff[i][2] = (int)colors[i].b - palette[i].b;
113
void init_step_fade_solid( int diff[256][3], SDL_Color color, unsigned int first_color, unsigned int last_color )
115
for (unsigned int i = first_color; i <= last_color; i++)
117
diff[i][0] = (int)color.r - palette[i].r;
118
diff[i][1] = (int)color.g - palette[i].g;
119
diff[i][2] = (int)color.b - palette[i].b;
123
void step_fade_palette( int diff[256][3], int steps, unsigned int first_color, unsigned int last_color )
127
SDL_Surface *const surface = SDL_GetVideoSurface();
128
const uint bpp = surface->format->BitsPerPixel;
130
for (unsigned int i = first_color; i <= last_color; i++)
132
int delta[3] = { diff[i][0] / steps, diff[i][1] / steps, diff[i][2] / steps };
134
diff[i][0] -= delta[0];
135
diff[i][1] -= delta[1];
136
diff[i][2] -= delta[2];
138
palette[i].r += delta[0];
139
palette[i].g += delta[1];
140
palette[i].b += delta[2];
144
rgb_palette[i] = SDL_MapRGB(surface->format, palette[i].r, palette[i].g, palette[i].b);
145
yuv_palette[i] = rgb_to_yuv(palette[i].r, palette[i].g, palette[i].b);
150
SDL_SetColors(surface, palette, 0, 256);
154
void fade_palette( Palette colors, int steps, unsigned int first_color, unsigned int last_color )
158
SDL_Surface *const surface = SDL_GetVideoSurface();
159
const uint bpp = surface->format->BitsPerPixel;
161
static int diff[256][3];
162
init_step_fade_palette(diff, colors, first_color, last_color);
164
for (; steps > 0; steps--)
168
step_fade_palette(diff, steps, first_color, last_color);
177
void fade_solid( SDL_Color color, int steps, unsigned int first_color, unsigned int last_color )
181
SDL_Surface *const surface = SDL_GetVideoSurface();
182
const uint bpp = surface->format->BitsPerPixel;
184
static int diff[256][3];
185
init_step_fade_solid(diff, color, first_color, last_color);
187
for (; steps > 0; steps--)
191
step_fade_palette(diff, steps, first_color, last_color);
200
void fade_black( int steps )
202
SDL_Color black = { 0, 0, 0 };
203
fade_solid(black, steps, 0, 255);
206
void fade_white( int steps )
208
SDL_Color white = { 255, 255, 255 };
209
fade_solid(white, steps, 0, 255);
212
static Uint32 rgb_to_yuv( int r, int g, int b )
214
int y = (r + g + b) >> 2,
215
u = 128 + ((r - b) >> 2),
216
v = 128 + ((-r + 2 * g - b) >> 3);
217
return (y << 16) + (u << 8) + v;