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