Debian Codegraph @

src/palette.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 #include "palette.h"
20
21 #include "file.h"
22 #include "nortsong.h"
23 #include "opentyr.h"
24 #include "video.h"
25
26 #include <assert.h>
27
28 static Uint32 rgb_to_yuv(int r, int g, int b);
29
30 #define PALETTE_COUNT 23
31
32 Palette palettes[PALETTE_COUNT];
33 int palette_count;
34
35 static Palette palette;
36 Uint32 rgb_palette[256], yuv_palette[256];
37
38 Palette colors;
39
40 void JE_loadPals(void)
41 {
42 FILE *f = dir_fopen_die(data_dir(), "palette.dat", "rb");
43
44 palette_count = ftell_eof(f) / (256 * 3);
45 assert(palette_count == PALETTE_COUNT);
46
47 for (int p = 0; p < palette_count; ++p)
48 {
49 for (int i = 0; i < 256; ++i)
50 {
51 // The VGA hardware palette used only 6 bits per component, so the values need to be rescaled to
52 // 8 bits. The naive way to do this is to simply do (c << 2), padding it with 0's, however this
53 // makes the maximum value 252 instead of the proper 255. A trick to fix this is to use the upper 2
54 // bits of the original value instead. This ensures that the value goes to 255 as the original goes
55 // to 63.
56
57 Uint8 rgb[3];
58 fread_u8_die(rgb, 3, f);
59 palettes[p][i].r = (rgb[0] << 2) | (rgb[0] >> 4);
60 palettes[p][i].g = (rgb[1] << 2) | (rgb[1] >> 4);
61 palettes[p][i].b = (rgb[2] << 2) | (rgb[2] >> 4);
62 }
63 }
64
65 fclose(f);
66 }
67
68 void set_palette(Palette colors, unsigned int first_color, unsigned int last_color)
69 {
70 for (uint i = first_color; i <= last_color; ++i)
71 {
72 palette[i] = colors[i];
73 rgb_palette[i] = SDL_MapRGB(main_window_tex_format, palette[i].r, palette[i].g, palette[i].b);
74 yuv_palette[i] = rgb_to_yuv(palette[i].r, palette[i].g, palette[i].b);
75 }
76 }
77
78 void set_colors(SDL_Color color, unsigned int first_color, unsigned int last_color)
79 {
80 for (uint i = first_color; i <= last_color; ++i)
81 {
82 palette[i] = color;
83 rgb_palette[i] = SDL_MapRGB(main_window_tex_format, palette[i].r, palette[i].g, palette[i].b);
84 yuv_palette[i] = rgb_to_yuv(palette[i].r, palette[i].g, palette[i].b);
85 }
86 }
87
88 void init_step_fade_palette(int diff[256][3], Palette colors, unsigned int first_color, unsigned int last_color)
89 {
90 for (unsigned int i = first_color; i <= last_color; i++)
91 {
92 diff[i][0] = (int)colors[i].r - palette[i].r;
93 diff[i][1] = (int)colors[i].g - palette[i].g;
94 diff[i][2] = (int)colors[i].b - palette[i].b;
95 }
96 }
97
98 void init_step_fade_solid(int diff[256][3], SDL_Color color, unsigned int first_color, unsigned int last_color)
99 {
100 for (unsigned int i = first_color; i <= last_color; i++)
101 {
102 diff[i][0] = (int)color.r - palette[i].r;
103 diff[i][1] = (int)color.g - palette[i].g;
104 diff[i][2] = (int)color.b - palette[i].b;
105 }
106 }
107
108 void step_fade_palette(int diff[256][3], int steps, unsigned int first_color, unsigned int last_color)
109 {
110 assert(steps > 0);
111
112 for (unsigned int i = first_color; i <= last_color; i++)
113 {
114 const int delta[3] = { diff[i][0] / steps, diff[i][1] / steps, diff[i][2] / steps };
115
116 diff[i][0] -= delta[0];
117 diff[i][1] -= delta[1];
118 diff[i][2] -= delta[2];
119
120 palette[i].r += delta[0];
121 palette[i].g += delta[1];
122 palette[i].b += delta[2];
123
124 rgb_palette[i] = SDL_MapRGB(main_window_tex_format, palette[i].r, palette[i].g, palette[i].b);
125 yuv_palette[i] = rgb_to_yuv(palette[i].r, palette[i].g, palette[i].b);
126 }
127 }
128
129 void fade_palette(Palette colors, int steps, unsigned int first_color, unsigned int last_color)
130 {
131 assert(steps > 0);
132
133 static int diff[256][3];
134 init_step_fade_palette(diff, colors, first_color, last_color);
135
136 for (; steps > 0; steps--)
137 {
138 setDelay(1);
139
140 step_fade_palette(diff, steps, first_color, last_color);
141
142 JE_showVGA();
143
144 service_wait_delay();
145 }
146 }
147
148 void fade_solid(SDL_Color color, int steps, unsigned int first_color, unsigned int last_color)
149 {
150 assert(steps > 0);
151
152 static int diff[256][3];
153 init_step_fade_solid(diff, color, first_color, last_color);
154
155 for (; steps > 0; steps--)
156 {
157 setDelay(1);
158
159 step_fade_palette(diff, steps, first_color, last_color);
160
161 JE_showVGA();
162
163 service_wait_delay();
164 }
165 }
166
167 void fade_black(int steps)
168 {
169 SDL_Color black = { 0, 0, 0 };
170 fade_solid(black, steps, 0, 255);
171 }
172
173 void fade_white(int steps)
174 {
175 SDL_Color white = { 255, 255, 255 };
176 fade_solid(white, steps, 0, 255);
177 }
178
179 static Uint32 rgb_to_yuv(int r, int g, int b)
180 {
181 int y = (r + g + b) >> 2,
182 u = 128 + ((r - b) >> 2),
183 v = 128 + ((-r + 2 * g - b) >> 3);
184 return (y << 16) + (u << 8) + v;
185 }