]> icculus.org git repositories - btb/d2x.git/blob - main/editor/texpage.c
imported missing editor files from d1x
[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  * $Source: /cvs/cvsroot/d2x/main/editor/texpage.c,v $
15  * $Revision: 1.1 $
16  * $Author: btb $
17  * $Date: 2004-12-19 13:54:27 $
18  * 
19  * Routines for displaying texture pages
20  * 
21  * $Log: not supported by cvs2svn $
22  * Revision 1.1.1.1  1999/06/14 22:04:32  donut
23  * Import of d1x 1.37 source.
24  *
25  * Revision 2.0  1995/02/27  11:34:51  john
26  * Version 2.0! No anonymous unions, Watcom 10.0, with no need
27  * for bitmaps.tbl.
28  * 
29  * Revision 1.14  1995/01/14  19:18:02  john
30  * First version of object paging.
31  * 
32  * Revision 1.13  1994/11/27  23:17:15  matt
33  * Made changes for new mprintf calling convention
34  * 
35  * Revision 1.12  1994/11/23  15:49:00  mike
36  * fix bug in tmapnum == 0 always getting reassigned.
37  * 
38  * Revision 1.11  1994/11/23  12:17:34  mike
39  * changing texture maps in all mines.
40  * 
41  * Revision 1.10  1994/11/19  00:04:42  john
42  * Changed some shorts to ints.
43  * 
44  * Revision 1.9  1994/11/16  17:59:36  john
45  * Fixed bug with writing to canvas before initing it.
46  * 
47  * Revision 1.8  1994/11/16  13:15:21  matt
48  * Fixed grab bug, and cleaned up code
49  * 
50  * Revision 1.7  1994/08/05  12:26:37  matt
51  * Fixed overplot problem with texture names
52  * 
53  * Revision 1.6  1994/04/11  12:01:58  yuan
54  * Fixed resetting to first texture on page annoyance.
55  * 
56  * Revision 1.5  1994/04/01  11:15:53  yuan
57  * Added objects to objpage. Added buttons for easier tmap scrolling.
58  * Objects are selected fully from objpage and add object menu or pad.
59  * 
60  * Revision 1.4  1994/03/15  16:33:37  yuan
61  * Fixed bm loader (might have some changes in walls and switches)
62  * 
63  * Revision 1.3  1993/12/16  17:25:46  john
64  * Moved texture and object selection to texpage and objpage
65  * 
66  * Revision 1.2  1993/12/16  15:57:39  john
67  * moved texture selection stuff to texpage.c
68  * 
69  * Revision 1.1  1993/12/16  15:06:56  john
70  * Initial revision
71  * 
72  * 
73  */
74
75 #include <stdlib.h>
76 #include <string.h>
77 #include <stdio.h>
78 #include <stdarg.h>
79
80 #ifdef RCS
81 static char rcsid[] = "$Id: texpage.c,v 1.1 2004-12-19 13:54:27 btb Exp $";
82 #endif
83
84 #include "inferno.h"
85 #include "gameseg.h"
86 #include "screens.h"                    // For GAME_SCREEN?????
87 #include "editor.h"                     // For TMAP_CURBOX??????
88 #include "gr.h"                         // For canves, font stuff
89 #include "ui.h"                         // For UI_GADGET stuff
90 #include "textures.h"           // For NumTextures
91 #include "error.h"
92 #include "key.h"
93 #include "mono.h"
94 #include "gamesave.h"
95
96 #include "texpage.h"
97 #include "piggy.h"
98
99 #define TMAPS_PER_PAGE 12
100
101 static UI_GADGET_USERBOX * TmapBox[TMAPS_PER_PAGE];
102 static UI_GADGET_USERBOX * TmapCurrent;
103
104 int CurrentTmap = 0;            // Used globally
105 int CurrentTexture = 0;         // Used globally
106
107 int TextureLights;
108 int TextureEffects;
109 int TextureMetals;
110
111 static int TexturePage = 0;
112
113 static grs_canvas * TmapnameCanvas;
114 static char tmap_filename[13];
115
116 static void texpage_print_name( char name[13] ) 
117 {
118          int w,h,aw;
119         int i;
120
121         for (i=strlen(name);i<12;i++)
122                 name[i]=' ';
123         name[i]=0;
124         
125     gr_set_current_canvas( TmapnameCanvas );
126     gr_get_string_size( name, &w, &h, &aw );
127     gr_string( 0, 0, name );                      
128 }
129
130 static void texpage_display_name( char *format, ... ) 
131 {
132         va_list ap;
133
134         va_start(ap, format);
135    vsprintf(tmap_filename, format, ap);
136         va_end(ap);
137
138    texpage_print_name(tmap_filename);
139 }
140
141 //Redraw the list of textures, based on TexturePage
142 void texpage_redraw()
143 {
144         int i;
145
146         for (i=0;  i<TMAPS_PER_PAGE; i++ )
147                 {
148                 gr_set_current_canvas(TmapBox[i]->canvas);
149                 if (i+TexturePage*TMAPS_PER_PAGE < Num_tmaps )  {
150                         PIGGY_PAGE_IN( Textures[TmapList[i+TexturePage*TMAPS_PER_PAGE]]);
151                         gr_ubitmap(0,0, &GameBitmaps[Textures[TmapList[i+TexturePage*TMAPS_PER_PAGE]].index]);
152                 } else 
153                         gr_clear_canvas( CGREY );
154                 }
155 }
156
157 //shows the current texture, updating the window and printing the name, base
158 //on CurrentTexture
159 void texpage_show_current()
160 {
161         gr_set_current_canvas(TmapCurrent->canvas);
162         PIGGY_PAGE_IN(Textures[CurrentTexture]);
163         gr_ubitmap(0,0, &GameBitmaps[Textures[CurrentTexture].index]);
164         texpage_display_name( TmapInfo[CurrentTexture].filename );
165 }
166
167 int texpage_goto_first()
168 {
169         TexturePage=0;
170         texpage_redraw();
171         return 1;
172 }
173
174 int texpage_goto_metals()
175 {
176
177         TexturePage=TextureMetals/TMAPS_PER_PAGE;
178         texpage_redraw();
179         return 1;
180 }
181
182
183 // Goto lights (paste ons)
184 int texpage_goto_lights()
185 {
186         TexturePage=TextureLights/TMAPS_PER_PAGE;
187         texpage_redraw();
188         return 1;
189 }
190
191 int texpage_goto_effects()
192 {
193         TexturePage=TextureEffects/TMAPS_PER_PAGE;
194         texpage_redraw();
195         return 1;
196 }
197
198 static int texpage_goto_prev()
199 {
200         if (TexturePage > 0) {
201                 TexturePage--;
202                 texpage_redraw();
203         }
204         return 1;
205 }
206
207 static int texpage_goto_next()
208 {
209         if ((TexturePage+1)*TMAPS_PER_PAGE < Num_tmaps ) {
210                 TexturePage++;
211                 texpage_redraw();
212         }
213         return 1;
214 }
215
216 //NOTE:  this code takes the texture map number, not this index in the
217 //list of available textures.  There are different if there are holes in
218 //the list
219 int texpage_grab_current(int n)
220 {
221         int i;
222
223         if ( (n<0) || ( n>= Num_tmaps) ) return 0;
224
225         CurrentTexture = n;
226
227         for (i=0;i<Num_tmaps;i++)
228                 if (TmapList[i] == n) {
229                         CurrentTmap = i;
230                         break;
231                 }
232         Assert(i!=Num_tmaps);
233         
234         TexturePage = CurrentTmap / TMAPS_PER_PAGE;
235         
236         if (TexturePage*TMAPS_PER_PAGE < Num_tmaps )
237                 texpage_redraw();
238
239         texpage_show_current();
240         
241         return 1;
242 }
243
244
245 // INIT TEXTURE STUFF
246
247 void texpage_init( UI_WINDOW * win )
248 {
249         int i;
250
251         ui_add_gadget_button( win, TMAPCURBOX_X + 00, TMAPCURBOX_Y - 24, 30, 20, "<<", texpage_goto_prev );
252         ui_add_gadget_button( win, TMAPCURBOX_X + 32, TMAPCURBOX_Y - 24, 30, 20, ">>", texpage_goto_next );
253
254         ui_add_gadget_button( win, TMAPCURBOX_X + 00, TMAPCURBOX_Y - 48, 15, 20, "T", texpage_goto_first );
255         ui_add_gadget_button( win, TMAPCURBOX_X + 17, TMAPCURBOX_Y - 48, 15, 20, "M", texpage_goto_metals );
256         ui_add_gadget_button( win, TMAPCURBOX_X + 34, TMAPCURBOX_Y - 48, 15, 20, "L", texpage_goto_lights );
257         ui_add_gadget_button( win, TMAPCURBOX_X + 51, TMAPCURBOX_Y - 48, 15, 20, "E", texpage_goto_effects );
258         
259
260         for (i=0;i<TMAPS_PER_PAGE;i++)
261                 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);
262
263         TmapCurrent = ui_add_gadget_userbox( win, TMAPCURBOX_X, TMAPCURBOX_Y, 64, 64 );
264
265         TmapnameCanvas = gr_create_sub_canvas(&grd_curscreen->sc_canvas, TMAPCURBOX_X , TMAPCURBOX_Y + TMAPBOX_H + 10, 100, 20);
266         gr_set_current_canvas( TmapnameCanvas );
267         gr_set_curfont( ui_small_font ); 
268    gr_set_fontcolor( CBLACK, CWHITE );
269
270         texpage_redraw();
271
272 // Don't reset the current tmap every time we go back to the editor.
273 //      CurrentTmap = TexturePage*TMAPS_PER_PAGE;
274 //      CurrentTexture = TmapList[CurrentTmap];
275         texpage_show_current();
276
277 }
278
279 void texpage_close()
280 {
281         gr_free_sub_canvas(TmapnameCanvas);
282 }
283
284
285 // DO TEXTURE STUFF
286
287 #define MAX_REPLACEMENTS        32
288
289 typedef struct replacement {
290         int     new, old;
291 } replacement;
292
293 replacement Replacement_list[MAX_REPLACEMENTS];
294 int     Num_replacements=0;
295
296 void texpage_do()
297 {
298         int i;
299
300         for (i=0; i<TMAPS_PER_PAGE; i++ ) {
301                 if (TmapBox[i]->b1_clicked && (i+TexturePage*TMAPS_PER_PAGE < Num_tmaps)) {
302                         CurrentTmap = i+TexturePage*TMAPS_PER_PAGE;
303                         CurrentTexture = TmapList[CurrentTmap];
304                         texpage_show_current();
305
306                         if (keyd_pressed[KEY_LSHIFT]) {
307                                 mprintf((0, "Will replace CurrentTexture (%i) with...(select by pressing Ctrl)\n", CurrentTexture));
308                                 Replacement_list[Num_replacements].old = CurrentTexture;
309                         }
310
311                         if (keyd_pressed[KEY_LCTRL]) {
312                                 mprintf((0, "...Replacement texture for %i is %i\n", Replacement_list[Num_replacements].old, CurrentTexture));
313                                 Replacement_list[Num_replacements].new = CurrentTexture;
314                                 Num_replacements++;
315                         }
316                 }
317         }
318 }
319
320 void init_replacements(void)
321 {
322         Num_replacements = 0;
323 }
324
325 void do_replacements(void)
326 {
327         int     replnum, segnum, sidenum;
328
329         med_compress_mine();
330
331         for (replnum=0; replnum<Num_replacements; replnum++) {
332                 int     old_tmap_num, new_tmap_num;
333
334                 old_tmap_num = Replacement_list[replnum].old;
335                 new_tmap_num = Replacement_list[replnum].new;
336                 Assert(old_tmap_num >= 0);
337                 Assert(new_tmap_num >= 0);
338
339                 for (segnum=0; segnum <= Highest_segment_index; segnum++) {
340                         segment *segp=&Segments[segnum];
341                         for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
342                                 side    *sidep=&segp->sides[sidenum];
343                                 if (sidep->tmap_num == old_tmap_num) {
344                                         sidep->tmap_num = new_tmap_num;
345                                         // mprintf((0, "Replacing tmap_num on segment:side = %i:%i\n", segnum, sidenum));
346                                 }
347                                 if ((sidep->tmap_num2 != 0) && ((sidep->tmap_num2 & 0x3fff) == old_tmap_num)) {
348                                         if (new_tmap_num == 0) {
349                                                 Int3(); //      Error.  You have tried to replace a tmap_num2 with 
350                                                                         //      the 0th tmap_num2 which is ILLEGAL!
351                                         } else {
352                                                 sidep->tmap_num2 = new_tmap_num | (sidep->tmap_num2 & 0xc000);
353                                                 // mprintf((0, "Replacing tmap_num2 on segment:side = %i:%i\n", segnum, sidenum));
354                                         }
355                                 }
356                         }
357                 }
358         }
359
360 }
361
362 void do_replacements_all(void)
363 {
364         int     i;
365
366         for (i=0; i<NUM_SHAREWARE_LEVELS; i++) {
367                 load_level(Shareware_level_names[i]);
368                 do_replacements();
369                 save_level(Shareware_level_names[i]);
370         }
371
372         for (i=0; i<NUM_REGISTERED_LEVELS; i++) {
373                 load_level(Registered_level_names[i]);
374                 do_replacements();
375                 save_level(Registered_level_names[i]);
376         }
377
378 }
379