1 /*
2 
3 	Copyright (C) 1991-2001 and beyond by Bungie Studios, Inc.
4 	and the "Aleph One" developers.
5 
6 	This program is free software; you can redistribute it and/or modify
7 	it under the terms of the GNU General Public License as published by
8 	the Free Software Foundation; either version 3 of the License, or
9 	(at your option) any later version.
10 
11 	This program is distributed in the hope that it will be useful,
12 	but WITHOUT ANY WARRANTY; without even the implied warranty of
13 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 	GNU General Public License for more details.
15 
16 	This license is contained in the file "COPYING",
17 	which is included with this source code; it is available online at
18 	http://www.gnu.org/licenses/gpl.html
19 
20 */
21 /*
22  *  cscluts_sdl.cpp - CLUT handling, SDL implementation
23  *
24  *  Written in 2000 by Christian Bauer
25  */
26 
27 #include "cseries.h"
28 #include "FileHandler.h"
29 #include "SDL_endian.h"
30 
31 
32 // Global variables
33 RGBColor rgb_black = {0x0000, 0x0000, 0x0000};
34 RGBColor rgb_white = {0xffff, 0xffff, 0xffff};
35 
36 RGBColor system_colors[NUM_SYSTEM_COLORS] =
37 {
38 	{0x2666, 0x2666, 0x2666},
39 	{0xd999, 0xd999, 0xd999}
40 };
41 
42 
43 /*
44  *  Convert Mac CLUT resource to color_table
45  */
46 
build_color_table(color_table * table,LoadedResource & clut)47 void build_color_table(color_table *table, LoadedResource &clut)
48 {
49 	// Open stream to CLUT resource
50 	SDL_RWops *p = SDL_RWFromMem(clut.GetPointer(), (int)clut.GetLength());
51 	assert(p);
52 
53 	// Check number of colors
54 	SDL_RWseek(p, 6, SEEK_CUR);
55 	int n = SDL_ReadBE16(p) + 1;
56 	// SDL_ReadBE16 returns a Uint16 and thus can never be negative. At least,
57 	// that is what it seems.
58 	// TODO Eliminate the n < 0 check as it will always evaluate to false
59 	if (n < 0)
60 		n = 0;
61 	else if (n > 256)
62 		n = 256;
63 	table->color_count = n;
64 
65 	// Convert color data
66 	rgb_color *dst = table->colors;
67 	for (int i=0; i<n; i++) {
68 		SDL_RWseek(p, 2, SEEK_CUR);
69 		dst->red = SDL_ReadBE16(p);
70 		dst->green = SDL_ReadBE16(p);
71 		dst->blue = SDL_ReadBE16(p);
72 		dst++;
73 	}
74 
75 	// Close stream
76 	SDL_RWclose(p);
77 }
78