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