]> icculus.org git repositories - btb/d2x.git/blob - main/gamepal.c
moved bitmap and sound header data structures and i/o routines back to piggy.c
[btb/d2x.git] / main / gamepal.c
1 /* $Id: gamepal.c,v 1.5 2003-10-10 09:36:35 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Functions for loading palettes
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #include <string.h>
26 #include <stdlib.h>
27 #include <math.h>
28
29 #include "pa_enabl.h"                   //$$POLY_ACC
30 #include "fix.h"
31 #include "vecmat.h"
32 #include "gr.h"
33 #include "3d.h"
34 #include "palette.h"
35 #include "rle.h"
36
37 #include "inferno.h"
38 #include "gamepal.h"
39 #include "mission.h"
40 #include "newmenu.h"
41 #include "texmerge.h"
42 #include "piggy.h"
43 #include "strutil.h"
44
45 #if defined(POLY_ACC)
46 #include "poly_acc.h"
47 #endif
48
49 extern void g3_remap_interp_colors();
50
51 char Current_level_palette[FILENAME_LEN];
52
53 extern int Color_0_31_0, HUD_color;
54
55 //give a filename a new extension
56 void change_filename_ext( char *dest, char *src, char *ext )
57 {
58         char *p;
59
60         strcpy (dest, src);
61
62         if (ext[0] == '.')
63                 ext++;
64
65         p = strchr(dest,'.');
66         if (!p) {
67                 p = dest + strlen(dest);
68                 *p = '.';
69         }
70
71         strcpy(p+1,ext);
72 }
73
74 //background for boxed messages
75 typedef struct bkg {
76         short x, y, w, h;                       // The location of the menu.
77         grs_bitmap * bmp;                       // The background under the menu.
78 } bkg;
79
80 extern bkg bg;
81
82 void load_background_bitmap(void);
83
84 char last_palette_loaded[FILENAME_LEN]="";
85 char last_palette_loaded_pig[FILENAME_LEN]="";
86
87 ubyte last_palette_for_color_fonts[768];
88
89 void remap_fonts_and_menus(int do_fadetable_hack)
90 {
91         nm_remap_background();
92         gr_remap_color_fonts();
93
94         if (do_fadetable_hack) {
95                 int i;
96                 float g = 1.0;
97                 double intensity;
98                 ubyte gamma[64];
99
100                 intensity = (double)(14)/(double)(32);
101                 for (i=0;i<64;i++)
102                         gamma[i] = (int)((pow(intensity, 1.0/g)*i) + 0.5);
103                 for (i=0;i<256;i++) {
104                         int c;
105                         c = gr_find_closest_color(gamma[gr_palette[i*3]],gamma[gr_palette[i*3+1]],gamma[gr_palette[i*3+2]]);
106                         gr_fade_table[14*256+i] = c;
107                 }
108         }
109
110         memcpy(last_palette_for_color_fonts,gr_palette,sizeof(last_palette_for_color_fonts));
111 }
112
113 //load a palette by name. returns 1 if new palette loaded, else 0
114 //if used_for_level is set, load pig, etc.
115 //if no_change_screen is set, the current screen does not get remapped,
116 //and the hardware palette does not get changed
117 int load_palette(char *name,int used_for_level,int no_change_screen)
118 {
119         char pigname[FILENAME_LEN];
120         ubyte old_pal[256*3];
121
122         //special hack to tell that palette system about a pig that's been loaded elsewhere
123         if (used_for_level == -2) {
124                 strncpy(last_palette_loaded_pig,name,sizeof(last_palette_loaded_pig));
125                 return 1;
126         }
127
128         if (name==NULL)
129                 name = last_palette_loaded_pig;
130
131         if (used_for_level && stricmp(last_palette_loaded_pig,name) != 0) {
132
133                 _splitpath(name,NULL,NULL,pigname,NULL);
134                 strcat(pigname,".pig");
135                 //if not editor, load pig first so small install message can come
136                 //up in old palette.  If editor version, we must load the pig after
137                 //the palette is loaded so we can remap new textures.
138                 #ifndef EDITOR
139                 piggy_new_pigfile(pigname);
140                 #endif
141         }
142
143         if (stricmp(last_palette_loaded,name) != 0) {
144
145                 memcpy(old_pal,gr_palette,sizeof(old_pal));
146
147                 strncpy(last_palette_loaded,name,sizeof(last_palette_loaded));
148
149                 gr_use_palette_table(name);
150
151                 if (Function_mode == FMODE_GAME && !no_change_screen)
152                         gr_remap_bitmap_good( &grd_curscreen->sc_canvas.cv_bitmap, old_pal, -1, -1 );
153
154 #if defined(POLY_ACC)
155         if (bg.bmp && bg.bmp->bm_type == BM_LINEAR)
156 #else
157         if (bg.bmp)
158 #endif
159             gr_remap_bitmap_good( bg.bmp, old_pal, -1, -1 );
160
161                 if (!gr_palette_faded_out && !no_change_screen)
162                         gr_palette_load(gr_palette);
163
164                 remap_fonts_and_menus(0);
165
166                 Color_0_31_0 = -1;              //for gauges
167                 HUD_color = -1;
168
169                 load_background_bitmap();
170
171                 g3_remap_interp_colors();
172         }
173
174
175         if (used_for_level && stricmp(last_palette_loaded_pig,name) != 0) {
176
177                 strncpy(last_palette_loaded_pig,name,sizeof(last_palette_loaded_pig));
178
179                 #ifdef EDITOR
180                 piggy_new_pigfile(pigname);
181                 #endif
182
183                 texmerge_flush();
184                 rle_cache_flush();
185         }
186
187         return 1;
188 }
189