]> icculus.org git repositories - btb/d2x.git/blob - main/editor/texpage.c
include conf.h in new editor files
[btb/d2x.git] / main / editor / texpage.c
1 /* $Id: texpage.c,v 1.3 2004-12-19 15:21:11 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 #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.3 2004-12-19 15:21:11 btb 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
46 #include "texpage.h"
47 #include "piggy.h"
48
49 #define TMAPS_PER_PAGE 12
50
51 static UI_GADGET_USERBOX * TmapBox[TMAPS_PER_PAGE];
52 static UI_GADGET_USERBOX * TmapCurrent;
53
54 int CurrentTmap = 0;            // Used globally
55 int CurrentTexture = 0;         // Used globally
56
57 int TextureLights;
58 int TextureEffects;
59 int TextureMetals;
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 < Num_tmaps )  {
100                         PIGGY_PAGE_IN( Textures[TmapList[i+TexturePage*TMAPS_PER_PAGE]]);
101                         gr_ubitmap(0,0, &GameBitmaps[Textures[TmapList[i+TexturePage*TMAPS_PER_PAGE]].index]);
102                 } else 
103                         gr_clear_canvas( CGREY );
104                 }
105 }
106
107 //shows the current texture, updating the window and printing the name, base
108 //on CurrentTexture
109 void texpage_show_current()
110 {
111         gr_set_current_canvas(TmapCurrent->canvas);
112         PIGGY_PAGE_IN(Textures[CurrentTexture]);
113         gr_ubitmap(0,0, &GameBitmaps[Textures[CurrentTexture].index]);
114         texpage_display_name( TmapInfo[CurrentTexture].filename );
115 }
116
117 int texpage_goto_first()
118 {
119         TexturePage=0;
120         texpage_redraw();
121         return 1;
122 }
123
124 int texpage_goto_metals()
125 {
126
127         TexturePage=TextureMetals/TMAPS_PER_PAGE;
128         texpage_redraw();
129         return 1;
130 }
131
132
133 // Goto lights (paste ons)
134 int texpage_goto_lights()
135 {
136         TexturePage=TextureLights/TMAPS_PER_PAGE;
137         texpage_redraw();
138         return 1;
139 }
140
141 int texpage_goto_effects()
142 {
143         TexturePage=TextureEffects/TMAPS_PER_PAGE;
144         texpage_redraw();
145         return 1;
146 }
147
148 static int texpage_goto_prev()
149 {
150         if (TexturePage > 0) {
151                 TexturePage--;
152                 texpage_redraw();
153         }
154         return 1;
155 }
156
157 static int texpage_goto_next()
158 {
159         if ((TexturePage+1)*TMAPS_PER_PAGE < Num_tmaps ) {
160                 TexturePage++;
161                 texpage_redraw();
162         }
163         return 1;
164 }
165
166 //NOTE:  this code takes the texture map number, not this index in the
167 //list of available textures.  There are different if there are holes in
168 //the list
169 int texpage_grab_current(int n)
170 {
171         int i;
172
173         if ( (n<0) || ( n>= Num_tmaps) ) return 0;
174
175         CurrentTexture = n;
176
177         for (i=0;i<Num_tmaps;i++)
178                 if (TmapList[i] == n) {
179                         CurrentTmap = i;
180                         break;
181                 }
182         Assert(i!=Num_tmaps);
183         
184         TexturePage = CurrentTmap / TMAPS_PER_PAGE;
185         
186         if (TexturePage*TMAPS_PER_PAGE < Num_tmaps )
187                 texpage_redraw();
188
189         texpage_show_current();
190         
191         return 1;
192 }
193
194
195 // INIT TEXTURE STUFF
196
197 void texpage_init( UI_WINDOW * win )
198 {
199         int i;
200
201         ui_add_gadget_button( win, TMAPCURBOX_X + 00, TMAPCURBOX_Y - 24, 30, 20, "<<", texpage_goto_prev );
202         ui_add_gadget_button( win, TMAPCURBOX_X + 32, TMAPCURBOX_Y - 24, 30, 20, ">>", texpage_goto_next );
203
204         ui_add_gadget_button( win, TMAPCURBOX_X + 00, TMAPCURBOX_Y - 48, 15, 20, "T", texpage_goto_first );
205         ui_add_gadget_button( win, TMAPCURBOX_X + 17, TMAPCURBOX_Y - 48, 15, 20, "M", texpage_goto_metals );
206         ui_add_gadget_button( win, TMAPCURBOX_X + 34, TMAPCURBOX_Y - 48, 15, 20, "L", texpage_goto_lights );
207         ui_add_gadget_button( win, TMAPCURBOX_X + 51, TMAPCURBOX_Y - 48, 15, 20, "E", texpage_goto_effects );
208         
209
210         for (i=0;i<TMAPS_PER_PAGE;i++)
211                 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);
212
213         TmapCurrent = ui_add_gadget_userbox( win, TMAPCURBOX_X, TMAPCURBOX_Y, 64, 64 );
214
215         TmapnameCanvas = gr_create_sub_canvas(&grd_curscreen->sc_canvas, TMAPCURBOX_X , TMAPCURBOX_Y + TMAPBOX_H + 10, 100, 20);
216         gr_set_current_canvas( TmapnameCanvas );
217         gr_set_curfont( ui_small_font ); 
218    gr_set_fontcolor( CBLACK, CWHITE );
219
220         texpage_redraw();
221
222 // Don't reset the current tmap every time we go back to the editor.
223 //      CurrentTmap = TexturePage*TMAPS_PER_PAGE;
224 //      CurrentTexture = TmapList[CurrentTmap];
225         texpage_show_current();
226
227 }
228
229 void texpage_close()
230 {
231         gr_free_sub_canvas(TmapnameCanvas);
232 }
233
234
235 // DO TEXTURE STUFF
236
237 #define MAX_REPLACEMENTS        32
238
239 typedef struct replacement {
240         int     new, old;
241 } replacement;
242
243 replacement Replacement_list[MAX_REPLACEMENTS];
244 int     Num_replacements=0;
245
246 void texpage_do()
247 {
248         int i;
249
250         for (i=0; i<TMAPS_PER_PAGE; i++ ) {
251                 if (TmapBox[i]->b1_clicked && (i+TexturePage*TMAPS_PER_PAGE < Num_tmaps)) {
252                         CurrentTmap = i+TexturePage*TMAPS_PER_PAGE;
253                         CurrentTexture = TmapList[CurrentTmap];
254                         texpage_show_current();
255
256                         if (keyd_pressed[KEY_LSHIFT]) {
257                                 mprintf((0, "Will replace CurrentTexture (%i) with...(select by pressing Ctrl)\n", CurrentTexture));
258                                 Replacement_list[Num_replacements].old = CurrentTexture;
259                         }
260
261                         if (keyd_pressed[KEY_LCTRL]) {
262                                 mprintf((0, "...Replacement texture for %i is %i\n", Replacement_list[Num_replacements].old, CurrentTexture));
263                                 Replacement_list[Num_replacements].new = CurrentTexture;
264                                 Num_replacements++;
265                         }
266                 }
267         }
268 }
269
270 void init_replacements(void)
271 {
272         Num_replacements = 0;
273 }
274
275 void do_replacements(void)
276 {
277         int     replnum, segnum, sidenum;
278
279         med_compress_mine();
280
281         for (replnum=0; replnum<Num_replacements; replnum++) {
282                 int     old_tmap_num, new_tmap_num;
283
284                 old_tmap_num = Replacement_list[replnum].old;
285                 new_tmap_num = Replacement_list[replnum].new;
286                 Assert(old_tmap_num >= 0);
287                 Assert(new_tmap_num >= 0);
288
289                 for (segnum=0; segnum <= Highest_segment_index; segnum++) {
290                         segment *segp=&Segments[segnum];
291                         for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
292                                 side    *sidep=&segp->sides[sidenum];
293                                 if (sidep->tmap_num == old_tmap_num) {
294                                         sidep->tmap_num = new_tmap_num;
295                                         // mprintf((0, "Replacing tmap_num on segment:side = %i:%i\n", segnum, sidenum));
296                                 }
297                                 if ((sidep->tmap_num2 != 0) && ((sidep->tmap_num2 & 0x3fff) == old_tmap_num)) {
298                                         if (new_tmap_num == 0) {
299                                                 Int3(); //      Error.  You have tried to replace a tmap_num2 with 
300                                                                         //      the 0th tmap_num2 which is ILLEGAL!
301                                         } else {
302                                                 sidep->tmap_num2 = new_tmap_num | (sidep->tmap_num2 & 0xc000);
303                                                 // mprintf((0, "Replacing tmap_num2 on segment:side = %i:%i\n", segnum, sidenum));
304                                         }
305                                 }
306                         }
307                 }
308         }
309
310 }
311
312 void do_replacements_all(void)
313 {
314         int     i;
315
316         for (i=0; i<NUM_SHAREWARE_LEVELS; i++) {
317                 load_level(Shareware_level_names[i]);
318                 do_replacements();
319                 save_level(Shareware_level_names[i]);
320         }
321
322         for (i=0; i<NUM_REGISTERED_LEVELS; i++) {
323                 load_level(Registered_level_names[i]);
324                 do_replacements();
325                 save_level(Registered_level_names[i]);
326         }
327
328 }
329