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