~ubuntu-branches/debian/stretch/opentyrian/stretch

« back to all changes in this revision

Viewing changes to src/palette.c

  • Committer: Package Import Robot
  • Author(s): Etienne Millon
  • Date: 2015-03-31 08:48:54 UTC
  • Revision ID: package-import@ubuntu.com-20150331084854-f5a4uoz7uv3vopk6
Tags: upstream-2.1.20130907+dfsg
ImportĀ upstreamĀ versionĀ 2.1.20130907+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "file.h"
 
20
#include "nortsong.h"
 
21
#include "opentyr.h"
 
22
#include "palette.h"
 
23
#include "video.h"
 
24
 
 
25
#include <assert.h>
 
26
 
 
27
static Uint32 rgb_to_yuv( int r, int g, int b );
 
28
 
 
29
#ifdef TYRIAN2000
 
30
#define PALETTE_COUNT 24
 
31
#else
 
32
#define PALETTE_COUNT 23
 
33
#endif
 
34
 
 
35
Palette palettes[PALETTE_COUNT];
 
36
int palette_count;
 
37
 
 
38
static Palette palette;
 
39
Uint32 rgb_palette[256], yuv_palette[256];
 
40
 
 
41
Palette colors;
 
42
 
 
43
void JE_loadPals( void )
 
44
{
 
45
        FILE *f = dir_fopen_die(data_dir(), "palette.dat", "rb");
 
46
        
 
47
        palette_count = ftell_eof(f) / (256 * 3);
 
48
        assert(palette_count == PALETTE_COUNT);
 
49
        
 
50
        for (int p = 0; p < palette_count; ++p)
 
51
        {
 
52
                for (int i = 0; i < 256; ++i)
 
53
                {
 
54
                        palettes[p][i].r = getc(f) << 2;
 
55
                        palettes[p][i].g = getc(f) << 2;
 
56
                        palettes[p][i].b = getc(f) << 2;
 
57
                }
 
58
        }
 
59
        
 
60
        fclose(f);
 
61
}
 
62
 
 
63
void set_palette( Palette colors, unsigned int first_color, unsigned int last_color )
 
64
{
 
65
        SDL_Surface *const surface = SDL_GetVideoSurface();
 
66
        const uint bpp = surface->format->BitsPerPixel;
 
67
        
 
68
        for (uint i = first_color; i <= last_color; ++i)
 
69
        {
 
70
                palette[i] = colors[i];
 
71
                
 
72
                if (bpp != 8)
 
73
                {
 
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);
 
76
                }
 
77
        }
 
78
        
 
79
        if (bpp == 8)
 
80
                SDL_SetColors(surface, palette, first_color, last_color - first_color + 1);
 
81
}
 
82
 
 
83
void set_colors( SDL_Color color, unsigned int first_color, unsigned int last_color )
 
84
{
 
85
        SDL_Surface *const surface = SDL_GetVideoSurface();
 
86
        const uint bpp = surface->format->BitsPerPixel;
 
87
        
 
88
        for (uint i = first_color; i <= last_color; ++i)
 
89
        {
 
90
                palette[i] = color;
 
91
                
 
92
                if (bpp != 8)
 
93
                {
 
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);
 
96
                }
 
97
        }
 
98
        
 
99
        if (bpp == 8)
 
100
                SDL_SetColors(surface, palette, first_color, last_color - first_color + 1);
 
101
}
 
102
 
 
103
void init_step_fade_palette( int diff[256][3], Palette colors, unsigned int first_color, unsigned int last_color )
 
104
{
 
105
        for (unsigned int i = first_color; i <= last_color; i++)
 
106
        {
 
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;
 
110
        }
 
111
}
 
112
 
 
113
void init_step_fade_solid( int diff[256][3], SDL_Color color, unsigned int first_color, unsigned int last_color )
 
114
{
 
115
        for (unsigned int i = first_color; i <= last_color; i++)
 
116
        {
 
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;
 
120
        }
 
121
}
 
122
 
 
123
void step_fade_palette( int diff[256][3], int steps, unsigned int first_color, unsigned int last_color )
 
124
{
 
125
        assert(steps > 0);
 
126
        
 
127
        SDL_Surface *const surface = SDL_GetVideoSurface();
 
128
        const uint bpp = surface->format->BitsPerPixel;
 
129
        
 
130
        for (unsigned int i = first_color; i <= last_color; i++)
 
131
        {
 
132
                int delta[3] = { diff[i][0] / steps, diff[i][1] / steps, diff[i][2] / steps };
 
133
                
 
134
                diff[i][0] -= delta[0];
 
135
                diff[i][1] -= delta[1];
 
136
                diff[i][2] -= delta[2];
 
137
                
 
138
                palette[i].r += delta[0];
 
139
                palette[i].g += delta[1];
 
140
                palette[i].b += delta[2];
 
141
                
 
142
                if (bpp != 8)
 
143
                {
 
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);
 
146
                }
 
147
        }
 
148
        
 
149
        if (bpp == 8)
 
150
                SDL_SetColors(surface, palette, 0, 256);
 
151
}
 
152
 
 
153
 
 
154
void fade_palette( Palette colors, int steps, unsigned int first_color, unsigned int last_color )
 
155
{
 
156
        assert(steps > 0);
 
157
        
 
158
        SDL_Surface *const surface = SDL_GetVideoSurface();
 
159
        const uint bpp = surface->format->BitsPerPixel;
 
160
        
 
161
        static int diff[256][3];
 
162
        init_step_fade_palette(diff, colors, first_color, last_color);
 
163
        
 
164
        for (; steps > 0; steps--)
 
165
        {
 
166
                setdelay(1);
 
167
                
 
168
                step_fade_palette(diff, steps, first_color, last_color);
 
169
                
 
170
                if (bpp != 8)
 
171
                        JE_showVGA();
 
172
                
 
173
                wait_delay();
 
174
        }
 
175
}
 
176
 
 
177
void fade_solid( SDL_Color color, int steps, unsigned int first_color, unsigned int last_color )
 
178
{
 
179
        assert(steps > 0);
 
180
        
 
181
        SDL_Surface *const surface = SDL_GetVideoSurface();
 
182
        const uint bpp = surface->format->BitsPerPixel;
 
183
        
 
184
        static int diff[256][3];
 
185
        init_step_fade_solid(diff, color, first_color, last_color);
 
186
        
 
187
        for (; steps > 0; steps--)
 
188
        {
 
189
                setdelay(1);
 
190
                
 
191
                step_fade_palette(diff, steps, first_color, last_color);
 
192
                
 
193
                if (bpp != 8)
 
194
                        JE_showVGA();
 
195
                
 
196
                wait_delay();
 
197
        }
 
198
}
 
199
 
 
200
void fade_black( int steps )
 
201
{
 
202
        SDL_Color black = { 0, 0, 0 };
 
203
        fade_solid(black, steps, 0, 255);
 
204
}
 
205
 
 
206
void fade_white( int steps )
 
207
{
 
208
        SDL_Color white = { 255, 255, 255 };
 
209
        fade_solid(white, steps, 0, 255);
 
210
}
 
211
 
 
212
static Uint32 rgb_to_yuv( int r, int g, int b )
 
213
{
 
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;
 
218
}
 
219