1 /* Copyright (C) 1995-2002  FSGames. Ported by Sean Ford and Yan Shosh
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17 //
18 // Palette.cpp file -- holds (what else?) palette routines
19 //
20 // Created: 02-05-95
21 //
22 /* ChangeLog
23 	buffers: 8/14/02: *added our_pal_lookup func so we don't need our.pal
24 	Zardus: 8/20/02: added return 0 for save_pallete so VC++ is happy
25 */
26 
27 #include "pal32.h"
28 #include <stdio.h>
29 #include "SDL_types.h"
30 
31 char temppal[768];    // for loading, setting, etc.
32 char gammapal[768];   // for gamma-correction
33 
34 //buffers: PORT: we need a palette to store the current palette
35 char curpal[768];
36 
37 char our_pal_lookup(int index);
38 
39 //
40 // load_and_set_palette
41 // Loads palette from file FILENAME,
42 // stores palette info in NEWPALETTE, and
43 // sets the current palette to this.
44 //
load_and_set_palette(char * filename,char * newpalette)45 short load_and_set_palette(char *filename, char *newpalette)
46 {
47 	short i;
48 
49 	//buffers: don't need this file stuff since we use our_pal_lookup instead
50 	/*	if ( (infile = fopen(filename, "rb")) == NULL ) // open for read
51 		{
52 			printf("Error in reading palette file %s\n", filename);
53 			return 0;
54 		}
55 
56 		if (fread(temppal, 1, 768, infile) != 768 || ferror(infile))
57 		{
58 			printf("Error: Corrupt palette file %s!\n", filename);
59 			return 0;
60 		}
61 
62 		fclose(infile);
63 	*/
64 	// Copy back the palette info ..
65 	for (i=0; i < 768; i++)
66 		//newpalette[i] = temppal[i];
67 		newpalette[i] = our_pal_lookup(i);
68 
69 	//set_palette(temppal);
70 	set_palette(newpalette);
71 
72 	return 1;
73 }
74 
75 //
76 // save_palette
77 // save the dos palette so we can restore it when done
78 //
save_palette(char * whatpalette)79 short save_palette(char * whatpalette)
80 {
81 	//buffers: PORT: we don't have a palette to save :P
82 	return 0;
83 }
84 
85 
86 //
87 // load_palette
88 // Loads palette from file FILENAME shorto NEWPALETTE
89 //
load_palette(char * filename,char * newpalette)90 short load_palette(char *filename, char *newpalette)
91 {
92 	short i;
93 
94 	/* buffers: we don't need this since we use our_pal_lookup() now
95 		if ( (infile = fopen(filename, "rb")) == NULL ) // open for read
96 		{
97 			printf("Error in reading palette file %s\n", filename);
98 			return 0;
99 		}
100 
101 		if (fread(temppal, 1, 768, infile) != 768 || ferror(infile))
102 		{
103 			printf("Error: Corrupt palette file %s!\n", filename);
104 			return 0;
105 		}
106 
107 		fclose(infile);
108 	*/
109 	// Copy back the palette info ..
110 	for (i=0; i < 768; i++)
111 		//newpalette[i] = temppal[i];
112 		newpalette[i] = our_pal_lookup(i);
113 
114 	return 1;
115 }
116 
117 
118 //
119 // set_palette
120 // Sets the current palette to NEWPALETTE.
121 //
set_palette(char * newpalette)122 short set_palette(char *newpalette)
123 {
124 	short i;
125 
126 	// Copy over the palette info ..
127 	for (i=0; i < 768; i++)
128 		curpal[i] = newpalette[i];
129 
130 	return 1;
131 }
132 
133 //
134 // adjust_palette
135 // Performs gamma correction (lightening/darkening)
136 //  on whichpal based on a positive or negative amount;
137 //  displays new palette, but does NOT affect whichpal
138 //
adjust_palette(char * whichpal,short amount)139 void adjust_palette(char *whichpal, short amount)
140 {
141 	short i;
142 	short tempcol;
143 	short multiple = (short) (amount * 10);
144 
145 	// Copy whichpal to temppal for setting ..
146 	for (i=0; i < 768; i++)
147 	{
148 		tempcol = whichpal[i];
149 
150 		// Now modify the current color bit based on 'amount'
151 		// Convert the 'amount' to = x*10% + x; ie, 2=(20% +2) increase
152 		tempcol = (short) ( ( ( tempcol * (100+multiple) ) / 100) + amount);
153 		if (tempcol < 0)
154 			tempcol = 0;
155 		if (tempcol > 63)
156 			tempcol = 63;
157 
158 		// Now set the current palette index to modified bit value
159 		curpal[i] = (char) tempcol;
160 	}
161 }
162 
163 //
164 // cycle_palette
165 // Cycle and display newpalette
166 //
cycle_palette(char * newpalette,short start,short end,short shift)167 void cycle_palette(char *newpalette, short start, short end, short shift)
168 {
169 	short i;
170 	short length = (short) (end-start);
171 	short newval;
172 	short colorspot;
173 
174 	// Copy over the palette info ..
175 	for (i=0; i < 768; i+=3)
176 	{
177 		colorspot = (short) (i/3);
178 		if ( (colorspot>= start) && (colorspot <= end) )
179 		{
180 			newval = (short) (colorspot-shift);
181 			if (newval<start)
182 				newval += length;
183 			newval *= 3;
184 			temppal[i]   = newpalette[newval];
185 			temppal[i+1] = newpalette[newval+1];
186 			temppal[i+2] = newpalette[newval+2];
187 		}
188 		else
189 		{
190 			temppal[i]   = newpalette[i];
191 			temppal[i+1] = newpalette[i+1];
192 			temppal[i+2] = newpalette[i+2];
193 		}
194 	}
195 
196 	// Return the modified palette
197 	for (i=0; i < 768; i++)
198 	{
199 		newpalette[i] = temppal[i];
200 		//buffers: since this is supposed to load the pal too, we
201 		//buffers: copy it over to ourpal.
202 		curpal[i] = temppal[i];
203 	}
204 }
205 
query_palette_reg(unsigned char index,int * red,int * green,int * blue)206 void query_palette_reg(unsigned char index, int *red, int *green, int *blue)
207 {
208 	int tred, tgreen, tblue;
209 
210 	tred = (int)curpal[index*3];
211 	tgreen = (int)curpal[index*3+1];
212 	tblue = (int)curpal[index*3+2];
213 
214 	*red = tred;
215 	*green = tgreen;
216 	*blue = tblue;
217 }
218 
set_palette_reg(unsigned char index,int red,int green,int blue)219 void set_palette_reg(unsigned char index,int red,int green,int blue)
220 {
221 	curpal[index*3] = red;
222 	curpal[index*3+1] = green;
223 	curpal[index*3+2] = blue;
224 }
225 
226 //buffers: this is the our.pal data in a function.
227 //buffers: i thought having a seperate our.pal file was ugly so i just
228 //buffers: put it all in this func
our_pal_lookup(int index)229 char our_pal_lookup(int index)
230 {
231 	char data[] = {
232 	                  0,0,0,8,8,8,16,16,16,24,24,24,32,32,32,40,40,40,48,48,48,56,56,56,1,
233 	                  1,1,9,9,9,17,17,17,25,25,25,33,33,33,41,41,41,49,49,49,57,57,57,0,
234 	                  0,0,15,15,15,18,18,18,21,21,21,24,24,24,27,27,27,30,30,30,33,33,33,36,
235 	                  36,36,39,39,39,42,42,42,45,45,45,48,48,48,51,51,51,54,54,54,57,57,57,57,
236 	                  16,16,54,18,18,51,20,20,48,22,22,45,24,24,42,26,26,39,28,28,36,30,30,57,
237 	                  0,0,52,0,0,47,0,0,42,0,0,37,0,0,32,0,0,27,0,0,22,0,0,16,
238 	                  57,16,18,54,18,20,51,20,22,48,22,24,45,24,26,42,26,28,39,28,30,36,30,0,
239 	                  57,0,0,52,0,0,47,0,0,42,0,0,37,0,0,32,0,0,27,0,0,22,0,16,
240 	                  16,57,18,18,54,20,20,51,22,22,48,24,24,45,26,26,42,28,28,39,30,30,36,0,
241 	                  0,57,0,0,52,0,0,47,0,0,42,0,0,37,0,0,32,0,0,27,0,0,22,57,
242 	                  57,16,54,54,18,51,51,20,48,48,22,45,45,24,42,42,26,39,39,28,36,36,30,57,
243 	                  57,0,52,52,0,47,47,0,42,42,0,37,37,0,32,32,0,27,27,0,22,22,0,57,
244 	                  16,57,54,18,54,51,20,51,48,22,48,45,24,45,42,26,42,39,28,39,36,30,36,57,
245 	                  0,57,52,0,52,47,0,47,42,0,42,37,0,37,32,0,32,27,0,27,22,0,22,16,
246 	                  57,57,18,54,54,20,51,51,22,48,48,24,45,45,26,42,42,28,39,39,30,36,36,0,
247 	                  57,57,0,52,52,0,47,47,0,42,42,0,37,37,0,32,32,0,27,27,0,22,22,57,
248 	                  41,25,52,36,20,47,31,15,42,26,10,37,21,5,32,16,0,27,11,0,22,6,0,50,
249 	                  40,30,45,35,25,40,30,20,35,25,15,30,20,10,25,15,5,20,10,0,15,5,0,57,
250 	                  25,41,52,20,36,47,15,31,42,10,26,37,5,21,32,0,16,27,0,11,22,0,6,50,
251 	                  30,40,45,25,35,40,20,30,35,15,25,30,10,20,25,5,15,20,0,10,15,0,5,0,
252 	                  18,6,0,16,6,0,13,5,0,11,5,0,8,3,0,6,2,0,3,1,0,2,0,17,
253 	                  17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
254 	                  17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
255 	                  17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,41,
256 	                  25,57,36,20,52,31,15,47,26,10,42,21,5,37,16,0,32,11,0,27,6,0,22,40,
257 	                  30,50,35,25,45,30,20,40,25,15,35,20,10,30,15,5,25,10,0,20,5,0,15,25,
258 	                  41,57,23,39,55,21,37,53,19,35,51,17,33,49,15,31,47,13,29,45,11,27,43,9,
259 	                  25,41,7,23,39,5,21,37,3,19,35,1,17,33,0,15,31,0,13,29,0,11,27,57,
260 	                  15,0,57,21,0,57,27,0,57,33,0,57,39,0,57,45,0,57,51,0,57,57,0,57,
261 	                  15,0,57,21,0,57,27,0,57,33,0,57,39,0,57,45,0,57,51,0,57,57,0,57,
262 	                  37,31,51,33,27,47,28,24,43,24,20,56,35,23,52,32,24,48,30,22,44,27,19,28,
263 	                  18,18,30,20,20,32,22,22,34,24,24,36,26,26,38,28,28,40,30,30,42,32,32};
264 
265 	return data[index];
266 }
267