]> icculus.org git repositories - btb/d2x.git/blob - main/gamepal.c
Import of d2x-0.0.8
[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
15 #include <conf.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <math.h>
19
20 #include "pa_enabl.h"                   //$$POLY_ACC
21 #include "fix.h"
22 #include "vecmat.h"
23 #include "gr.h"
24 #include "3d.h"
25 #include "palette.h"
26 #include "rle.h"
27
28 #include "inferno.h"
29 #include "gamepal.h"
30 #include "mission.h"
31 #include "newmenu.h"
32 #include "texmerge.h"
33 #include "piggy.h"
34 #include "strutil.h"
35
36 #if defined(POLY_ACC)
37 #include "poly_acc.h"
38 #endif
39
40 char Current_level_palette[FILENAME_LEN];
41
42 extern int Color_0_31_0, HUD_color;
43
44 //give a filename a new extension
45 void change_filename_ext( char *dest, char *src, char *ext )
46 {
47         char *p;
48
49         strcpy (dest, src);
50
51         if (ext[0] == '.')
52                 ext++;
53
54         p = strchr(dest,'.');
55         if (!p) {
56                 p = dest + strlen(dest);
57                 *p = '.';
58         }
59
60         strcpy(p+1,ext);
61 }
62
63 //background for boxed messages
64 typedef struct bkg {
65         short x, y, w, h;                       // The location of the menu.
66         grs_bitmap * bmp;                       // The background under the menu.
67 } bkg;
68
69 extern bkg bg;
70
71 void load_background_bitmap(void);
72
73 char last_palette_loaded[FILENAME_LEN]="";
74 char last_palette_loaded_pig[FILENAME_LEN]="";
75
76 ubyte last_palette_for_color_fonts[768];
77
78 void remap_fonts_and_menus(int do_fadetable_hack)
79 {
80         nm_remap_background();
81         gr_remap_color_fonts();
82
83         if (do_fadetable_hack) {
84                 int i;
85                 float g = 1.0;
86                 double intensity;
87                 ubyte gamma[64];
88
89                 intensity = (double)(14)/(double)(32);
90                 for (i=0;i<64;i++)
91                         gamma[i] = (int)((pow(intensity, 1.0/g)*i) + 0.5);
92                 for (i=0;i<256;i++) {
93                         int c;
94                         c = gr_find_closest_color(gamma[gr_palette[i*3]],gamma[gr_palette[i*3+1]],gamma[gr_palette[i*3+2]]);
95                         gr_fade_table[14*256+i] = c;
96                 }
97         }
98
99         memcpy(last_palette_for_color_fonts,gr_palette,sizeof(last_palette_for_color_fonts));
100 }
101
102 //load a palette by name. returns 1 if new palette loaded, else 0
103 //if used_for_level is set, load pig, etc.
104 //if no_change_screen is set, the current screen does not get remapped,
105 //and the hardware palette does not get changed
106 int load_palette(char *name,int used_for_level,int no_change_screen)
107 {
108         char pigname[FILENAME_LEN];
109         ubyte old_pal[256*3];
110
111         //special hack to tell that palette system about a pig that's been loaded elsewhere
112         if (used_for_level == -2) {
113                 strncpy(last_palette_loaded_pig,name,sizeof(last_palette_loaded_pig));
114                 return 1;
115         }
116
117         if (name==NULL)
118                 name = last_palette_loaded_pig;
119
120         if (used_for_level && stricmp(last_palette_loaded_pig,name) != 0) {
121
122                 _splitpath(name,NULL,NULL,pigname,NULL);
123                 strcat(pigname,".PIG");
124
125                 //if not editor, load pig first so small install message can come
126                 //up in old palette.  If editor version, we must load the pig after
127                 //the palette is loaded so we can remap new textures.
128                 #ifndef EDITOR
129                 piggy_new_pigfile(pigname);
130                 #endif
131         }
132
133         if (stricmp(last_palette_loaded,name) != 0) {
134
135                 memcpy(old_pal,gr_palette,sizeof(old_pal));
136
137                 strncpy(last_palette_loaded,name,sizeof(last_palette_loaded));
138
139                 gr_use_palette_table(name);
140
141                 if (Function_mode == FMODE_GAME && !no_change_screen)
142                         gr_remap_bitmap_good( &grd_curscreen->sc_canvas.cv_bitmap, old_pal, -1, -1 );
143
144 #if defined(POLY_ACC)
145         if (bg.bmp && bg.bmp->bm_type == BM_LINEAR)
146 #else
147         if (bg.bmp)
148 #endif
149             gr_remap_bitmap_good( bg.bmp, old_pal, -1, -1 );
150
151                 if (!gr_palette_faded_out && !no_change_screen)
152                         gr_palette_load(gr_palette);
153
154                 remap_fonts_and_menus(0);
155
156                 Color_0_31_0 = -1;              //for gauges
157                 HUD_color = -1;
158
159                 load_background_bitmap();
160
161                 g3_remap_interp_colors();
162         }
163
164
165         if (used_for_level && stricmp(last_palette_loaded_pig,name) != 0) {
166
167                 strncpy(last_palette_loaded_pig,name,sizeof(last_palette_loaded_pig));
168
169                 #ifdef EDITOR
170                 piggy_new_pigfile(pigname);
171                 #endif
172
173                 texmerge_flush();
174                 rle_cache_flush();
175         }
176
177         return 1;
178 }
179