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