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