]> icculus.org git repositories - btb/d2x.git/blob - main/editor/texpage.c
remove rcs tags
[btb/d2x.git] / main / editor / texpage.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-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Routines for displaying texture pages
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "conf.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <stdarg.h>
28
29 #include "inferno.h"
30 #include "gameseg.h"
31 #include "screens.h"                    // For GAME_SCREEN?????
32 #include "editor.h"                     // For TMAP_CURBOX??????
33 #include "gr.h"                         // For canves, font stuff
34 #include "ui.h"                         // For UI_GADGET stuff
35 #include "textures.h"           // For NumTextures
36 #include "error.h"
37 #include "key.h"
38 #include "mono.h"
39 #include "gamesave.h"
40 #include "mission.h"
41
42 #include "texpage.h"
43 #include "piggy.h"
44
45 #define TMAPS_PER_PAGE 12
46
47 static UI_GADGET_USERBOX * TmapBox[TMAPS_PER_PAGE];
48 static UI_GADGET_USERBOX * TmapCurrent;
49
50 int CurrentTexture = 0;         // Used globally
51
52 int TextureLights = 275;
53 int TextureEffects = 308;
54 int TextureMetals = 202;
55
56 static int TexturePage = 0;
57
58 static grs_canvas * TmapnameCanvas;
59 static char tmap_filename[13];
60
61 static void texpage_print_name( char name[13] ) 
62 {
63          int w,h,aw;
64         int i;
65
66         for (i=strlen(name);i<12;i++)
67                 name[i]=' ';
68         name[i]=0;
69         
70     gr_set_current_canvas( TmapnameCanvas );
71     gr_get_string_size( name, &w, &h, &aw );
72     gr_string( 0, 0, name );                      
73 }
74
75 static void texpage_display_name( char *format, ... ) 
76 {
77         va_list ap;
78
79         va_start(ap, format);
80    vsprintf(tmap_filename, format, ap);
81         va_end(ap);
82
83    texpage_print_name(tmap_filename);
84 }
85
86 //Redraw the list of textures, based on TexturePage
87 void texpage_redraw()
88 {
89         int i;
90
91         for (i = 0;  i < TMAPS_PER_PAGE; i++)
92         {
93                 gr_set_current_canvas(TmapBox[i]->canvas);
94                 if (i + TexturePage*TMAPS_PER_PAGE < NumTextures)
95                 {
96                         PIGGY_PAGE_IN(Textures[i + TexturePage*TMAPS_PER_PAGE]);
97                         gr_ubitmap(0, 0, &GameBitmaps[Textures[i + TexturePage*TMAPS_PER_PAGE].index]);
98                 } else 
99                         gr_clear_canvas( CGREY );
100         }
101 }
102
103 //shows the current texture, updating the window and printing the name, base
104 //on CurrentTexture
105 void texpage_show_current()
106 {
107         gr_set_current_canvas(TmapCurrent->canvas);
108         PIGGY_PAGE_IN(Textures[CurrentTexture]);
109         gr_ubitmap(0,0, &GameBitmaps[Textures[CurrentTexture].index]);
110         texpage_display_name( TmapInfo[CurrentTexture].filename );
111 }
112
113 int texpage_goto_first()
114 {
115         TexturePage=0;
116         texpage_redraw();
117         return 1;
118 }
119
120 int texpage_goto_metals()
121 {
122
123         TexturePage=TextureMetals/TMAPS_PER_PAGE;
124         texpage_redraw();
125         return 1;
126 }
127
128
129 // Goto lights (paste ons)
130 int texpage_goto_lights()
131 {
132         TexturePage=TextureLights/TMAPS_PER_PAGE;
133         texpage_redraw();
134         return 1;
135 }
136
137 int texpage_goto_effects()
138 {
139         TexturePage=TextureEffects/TMAPS_PER_PAGE;
140         texpage_redraw();
141         return 1;
142 }
143
144 static int texpage_goto_prev()
145 {
146         if (TexturePage > 0) {
147                 TexturePage--;
148                 texpage_redraw();
149         }
150         return 1;
151 }
152
153 static int texpage_goto_next()
154 {
155         if ((TexturePage + 1)*TMAPS_PER_PAGE < NumTextures)
156         {
157                 TexturePage++;
158                 texpage_redraw();
159         }
160         return 1;
161 }
162
163 //NOTE:  this code takes the texture map number, not this index in the
164 //list of available textures.  There are different if there are holes in
165 //the list
166 int texpage_grab_current(int n)
167 {
168         if ((n < 0) || (n >= NumTextures)) return 0;
169
170         CurrentTexture = n;
171
172         TexturePage = CurrentTexture / TMAPS_PER_PAGE;
173         
174         if (TexturePage*TMAPS_PER_PAGE < NumTextures)
175                 texpage_redraw();
176
177         texpage_show_current();
178         
179         return 1;
180 }
181
182
183 // INIT TEXTURE STUFF
184
185 void texpage_init( UI_WINDOW * win )
186 {
187         int i;
188
189         ui_add_gadget_button( win, TMAPCURBOX_X + 00, TMAPCURBOX_Y - 24, 30, 20, "<<", texpage_goto_prev );
190         ui_add_gadget_button( win, TMAPCURBOX_X + 32, TMAPCURBOX_Y - 24, 30, 20, ">>", texpage_goto_next );
191
192         ui_add_gadget_button( win, TMAPCURBOX_X + 00, TMAPCURBOX_Y - 48, 15, 20, "T", texpage_goto_first );
193         ui_add_gadget_button( win, TMAPCURBOX_X + 17, TMAPCURBOX_Y - 48, 15, 20, "M", texpage_goto_metals );
194         ui_add_gadget_button( win, TMAPCURBOX_X + 34, TMAPCURBOX_Y - 48, 15, 20, "L", texpage_goto_lights );
195         ui_add_gadget_button( win, TMAPCURBOX_X + 51, TMAPCURBOX_Y - 48, 15, 20, "E", texpage_goto_effects );
196         
197
198         for (i=0;i<TMAPS_PER_PAGE;i++)
199                 TmapBox[i] = ui_add_gadget_userbox( win, TMAPBOX_X + (i/3)*(2+TMAPBOX_W), TMAPBOX_Y + (i%3)*(2+TMAPBOX_H), TMAPBOX_W, TMAPBOX_H);
200
201         TmapCurrent = ui_add_gadget_userbox( win, TMAPCURBOX_X, TMAPCURBOX_Y, 64, 64 );
202
203         TmapnameCanvas = gr_create_sub_canvas(&grd_curscreen->sc_canvas, TMAPCURBOX_X , TMAPCURBOX_Y + TMAPBOX_H + 10, 100, 20);
204         gr_set_current_canvas( TmapnameCanvas );
205         gr_set_curfont( ui_small_font ); 
206    gr_set_fontcolor( CBLACK, CWHITE );
207
208         texpage_redraw();
209
210 // Don't reset the current tmap every time we go back to the editor.
211 //      CurrentTexture = TexturePage*TMAPS_PER_PAGE;
212         texpage_show_current();
213
214 }
215
216 void texpage_close()
217 {
218         gr_free_sub_canvas(TmapnameCanvas);
219 }
220
221
222 // DO TEXTURE STUFF
223
224 #define MAX_REPLACEMENTS        32
225
226 typedef struct replacement {
227         int     new, old;
228 } replacement;
229
230 replacement Replacement_list[MAX_REPLACEMENTS];
231 int     Num_replacements=0;
232
233 void texpage_do()
234 {
235         int i;
236
237         for (i=0; i<TMAPS_PER_PAGE; i++ ) {
238                 if (TmapBox[i]->b1_clicked && (i + TexturePage*TMAPS_PER_PAGE < NumTextures))
239                 {
240                         CurrentTexture = i + TexturePage*TMAPS_PER_PAGE;
241                         texpage_show_current();
242
243                         if (keyd_pressed[KEY_LSHIFT]) {
244                                 mprintf((0, "Will replace CurrentTexture (%i) with...(select by pressing Ctrl)\n", CurrentTexture));
245                                 Replacement_list[Num_replacements].old = CurrentTexture;
246                         }
247
248                         if (keyd_pressed[KEY_LCTRL]) {
249                                 mprintf((0, "...Replacement texture for %i is %i\n", Replacement_list[Num_replacements].old, CurrentTexture));
250                                 Replacement_list[Num_replacements].new = CurrentTexture;
251                                 Num_replacements++;
252                         }
253                 }
254         }
255 }
256
257 void init_replacements(void)
258 {
259         Num_replacements = 0;
260 }
261
262 void do_replacements(void)
263 {
264         int     replnum, segnum, sidenum;
265
266         med_compress_mine();
267
268         for (replnum=0; replnum<Num_replacements; replnum++) {
269                 int     old_tmap_num, new_tmap_num;
270
271                 old_tmap_num = Replacement_list[replnum].old;
272                 new_tmap_num = Replacement_list[replnum].new;
273                 Assert(old_tmap_num >= 0);
274                 Assert(new_tmap_num >= 0);
275
276                 for (segnum=0; segnum <= Highest_segment_index; segnum++) {
277                         segment *segp=&Segments[segnum];
278                         for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
279                                 side    *sidep=&segp->sides[sidenum];
280                                 if (sidep->tmap_num == old_tmap_num) {
281                                         sidep->tmap_num = new_tmap_num;
282                                         // mprintf((0, "Replacing tmap_num on segment:side = %i:%i\n", segnum, sidenum));
283                                 }
284                                 if ((sidep->tmap_num2 != 0) && ((sidep->tmap_num2 & 0x3fff) == old_tmap_num)) {
285                                         if (new_tmap_num == 0) {
286                                                 Int3(); //      Error.  You have tried to replace a tmap_num2 with 
287                                                                         //      the 0th tmap_num2 which is ILLEGAL!
288                                         } else {
289                                                 sidep->tmap_num2 = new_tmap_num | (sidep->tmap_num2 & 0xc000);
290                                                 // mprintf((0, "Replacing tmap_num2 on segment:side = %i:%i\n", segnum, sidenum));
291                                         }
292                                 }
293                         }
294                 }
295         }
296
297 }
298
299 void do_replacements_all(void)
300 {
301         int     i;
302
303         for (i = 0; i < Last_level; i++)
304         {
305                 load_level(Level_names[i]);
306                 do_replacements();
307                 save_level(Level_names[i]);
308         }
309
310         for (i = 0; i < -Last_secret_level; i++)
311         {
312                 load_level(Secret_level_names[i]);
313                 do_replacements();
314                 save_level(Secret_level_names[i]);
315         }
316
317 }
318