1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | #include "tupdate.hh"
|
---|
10 | #include "loaders/dir_load.hh"
|
---|
11 | #include "saver_id.hh"
|
---|
12 | #include "loaders/load.hh"
|
---|
13 | #include "error/alert.hh"
|
---|
14 | #include "memory/array.hh"
|
---|
15 | #include "g1_limits.hh"
|
---|
16 | #include "status/status.hh"
|
---|
17 | #include "tex_id.hh"
|
---|
18 | #include "image/image8.hh"
|
---|
19 |
|
---|
20 | #include "file/file.hh"
|
---|
21 | #include "mip.hh"
|
---|
22 | #include "r1_res.hh"
|
---|
23 | #include "palette/pal.hh"
|
---|
24 |
|
---|
25 | extern int m1_max_mip_level;
|
---|
26 |
|
---|
27 | static i4_str *m1_locate_texture(const i4_const_str &texture_filename)
|
---|
28 | {
|
---|
29 | i4_file_status_struct s;
|
---|
30 | i4_str *path, *fname, *ext;
|
---|
31 |
|
---|
32 | if (texture_filename.null())
|
---|
33 | return 0;
|
---|
34 |
|
---|
35 | if (i4_get_status(texture_filename, s))
|
---|
36 | return new i4_str(texture_filename);
|
---|
37 |
|
---|
38 | i4_filename_struct fn;
|
---|
39 | i4_split_path(texture_filename, fn);
|
---|
40 |
|
---|
41 | char s_dir[128];
|
---|
42 | i4_os_string(r1_gets("s_dir"), s_dir, 128);
|
---|
43 |
|
---|
44 | char def_name[256];
|
---|
45 | if (fn.extension)
|
---|
46 | {
|
---|
47 | sprintf(def_name, "%s/%s.%s", s_dir, fn.filename, fn.extension);
|
---|
48 | if (i4_get_status(def_name, s))
|
---|
49 | return new i4_str(def_name);
|
---|
50 | }
|
---|
51 |
|
---|
52 | sprintf(def_name, "%s/%s.jpg", s_dir, fn.filename);
|
---|
53 | if (i4_get_status(def_name, s))
|
---|
54 | return new i4_str(def_name);
|
---|
55 |
|
---|
56 | return 0;
|
---|
57 | }
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | static i4_image_class *m1_convert_8_to_32(i4_image_class *image8)
|
---|
62 | {
|
---|
63 | i4_pixel_format fmt;
|
---|
64 | fmt.default_format();
|
---|
65 |
|
---|
66 | const i4_pal *pal=i4_pal_man.register_pal(&fmt);
|
---|
67 |
|
---|
68 | i4_image_class *im8=image8;
|
---|
69 |
|
---|
70 | i4_image_class *new32=i4_create_image(im8->width(),
|
---|
71 | im8->height(),
|
---|
72 | pal);
|
---|
73 |
|
---|
74 | w32 *i32=(w32 *)new32->data;
|
---|
75 | w8 *i8=(w8 *)im8->data;
|
---|
76 |
|
---|
77 | w32 *pal_data=image8->get_pal()->source.lookup;
|
---|
78 |
|
---|
79 | w32 i,t=im8->width()*im8->height();
|
---|
80 | for (i=0; i<t; i++)
|
---|
81 | {
|
---|
82 | *i32=pal_data[*i8];
|
---|
83 | ++i32;
|
---|
84 | ++i8;
|
---|
85 | }
|
---|
86 |
|
---|
87 | return new32;
|
---|
88 | }
|
---|
89 |
|
---|
90 | i4_bool r1_write_tga_mips(i4_image_class *src_texture,
|
---|
91 | char *dst_file,
|
---|
92 | char *texture_name, w32 chroma_color);
|
---|
93 |
|
---|
94 | i4_bool m1_pack_mip_texture(const i4_const_str &src_tga,
|
---|
95 | const i4_const_str &out_ctext)
|
---|
96 | {
|
---|
97 | i4_file_class *fp = i4_open(src_tga);
|
---|
98 |
|
---|
99 | i4_bool result = i4_F;
|
---|
100 |
|
---|
101 | if (!fp)
|
---|
102 | return i4_F;
|
---|
103 |
|
---|
104 | i4_str *stat_str=r1_gets("loading_texture_x").sprintf(200,&src_tga);
|
---|
105 | i4_status_class *status=i4_create_status(*stat_str);
|
---|
106 | delete stat_str;
|
---|
107 | i4_image_class *src_texture = i4_load_image(fp, status);
|
---|
108 | if (status)
|
---|
109 | delete status;
|
---|
110 |
|
---|
111 | delete fp;
|
---|
112 |
|
---|
113 | if (!src_texture)
|
---|
114 | return i4_F;
|
---|
115 |
|
---|
116 | if (src_texture->get_pal()->source.pixel_depth==I4_8BIT)
|
---|
117 | {
|
---|
118 | i4_image_class *old = src_texture;
|
---|
119 | src_texture = m1_convert_8_to_32(src_texture);
|
---|
120 | delete old;
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (src_texture->get_pal()->source.pixel_depth==I4_32BIT)
|
---|
124 | {
|
---|
125 | /*
|
---|
126 | //create a texture update window
|
---|
127 | i4_str *update_texture_message = i4gets("updating_texture").sprintf(100,&src_tga);
|
---|
128 |
|
---|
129 | i4_status_class *status_window = i4_create_status(*update_texture_message);
|
---|
130 |
|
---|
131 | delete info; //stupid i4 str delete
|
---|
132 | */
|
---|
133 |
|
---|
134 | char dst_ctext[256];
|
---|
135 | i4_os_string(out_ctext,dst_ctext,256);
|
---|
136 |
|
---|
137 | char texname[256];
|
---|
138 | i4_os_string(src_tga,texname,256);
|
---|
139 |
|
---|
140 | //generate the mips
|
---|
141 | result = r1_write_tga_mips(src_texture,dst_ctext,texname,G1_CHROMA_COLOR);
|
---|
142 |
|
---|
143 | //delete stat; //done making the mips, delete the update window
|
---|
144 | }
|
---|
145 |
|
---|
146 | delete src_texture;
|
---|
147 |
|
---|
148 | return result;
|
---|
149 | }
|
---|
150 |
|
---|
151 | i4_bool is_newer(const i4_const_str &f1,
|
---|
152 | const i4_const_str &f2)
|
---|
153 | {
|
---|
154 | i4_file_status_struct f1_time, f2_time;
|
---|
155 |
|
---|
156 | if (i4_get_status(f1, f1_time))
|
---|
157 | {
|
---|
158 | if (i4_get_status(f2, f2_time))
|
---|
159 | return f1_time.last_modified>f2_time.last_modified;
|
---|
160 | else return i4_F;
|
---|
161 | }
|
---|
162 | else return i4_F;
|
---|
163 | }
|
---|
164 |
|
---|
165 | // this function checks the times of the textures with the
|
---|
166 | // compressed mip version and updates it if it is newer
|
---|
167 | i4_bool m1_update_texture(const i4_const_str &texture_filename,
|
---|
168 | i4_bool force_all,
|
---|
169 | i4_status_class *stat,
|
---|
170 | float start, float end)
|
---|
171 | {
|
---|
172 | i4_bool ret=i4_F;
|
---|
173 |
|
---|
174 |
|
---|
175 | i4_file_status_struct s1,s2;
|
---|
176 | i4_str *tname=m1_locate_texture(texture_filename);
|
---|
177 | if (tname)
|
---|
178 | {
|
---|
179 | if (!force_all)
|
---|
180 | i4_get_status(*tname, s1);
|
---|
181 |
|
---|
182 | w32 id = r1_get_texture_id(*tname);
|
---|
183 |
|
---|
184 | i4_str *texture_file = r1_texture_id_to_filename(id, r1_get_compressed_dir());
|
---|
185 |
|
---|
186 | ret=i4_T;
|
---|
187 |
|
---|
188 | if (force_all)
|
---|
189 | {
|
---|
190 | m1_pack_mip_texture(*tname,*texture_file);
|
---|
191 | }
|
---|
192 | else
|
---|
193 | {
|
---|
194 | memset(&s1,0,sizeof(i4_file_status_struct));
|
---|
195 | memset(&s2,0,sizeof(i4_file_status_struct));
|
---|
196 | i4_get_status(*tname, s1);
|
---|
197 | i4_get_status(*texture_file, s2);
|
---|
198 |
|
---|
199 | if (s1.last_modified>s2.last_modified)
|
---|
200 | {
|
---|
201 | m1_pack_mip_texture(*tname,*texture_file);
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | delete texture_file;
|
---|
207 | delete tname;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 |
|
---|
213 | return ret;
|
---|
214 | }
|
---|
215 |
|
---|
216 | // this calls m1_update_texture on all the textures in an object model file
|
---|
217 | i4_bool m1_update_object_textures(const i4_const_str &object_filename,
|
---|
218 | i4_bool force_all,
|
---|
219 | const i4_const_str *out_dir)
|
---|
220 | {
|
---|
221 |
|
---|
222 | i4_str *info=i4gets("updating_obj").sprintf(100, &object_filename);
|
---|
223 | i4_status_class *stat=i4_create_status(*info);
|
---|
224 | delete info;
|
---|
225 |
|
---|
226 |
|
---|
227 | i4_bool ret=i4_F;
|
---|
228 | i4_file_class *fp=i4_open(object_filename);
|
---|
229 | if (fp)
|
---|
230 | {
|
---|
231 | i4_loader_class *gfp=i4_open_save_file(fp);
|
---|
232 | if (gfp)
|
---|
233 | {
|
---|
234 | if (gfp->goto_section(G1_SECTION_MODEL_TEXTURE_NAMES))
|
---|
235 | {
|
---|
236 | w16 num_quads = gfp->read_16();
|
---|
237 |
|
---|
238 | for (int i=0; i<num_quads; i++)
|
---|
239 | {
|
---|
240 | i4_str *texture_name = gfp->read_counted_str();
|
---|
241 |
|
---|
242 | if (m1_update_texture(*texture_name, force_all, stat,
|
---|
243 | i/(float)num_quads, (i+1)/(float)num_quads))
|
---|
244 | {
|
---|
245 | ret = i4_T;
|
---|
246 | }
|
---|
247 |
|
---|
248 | delete texture_name;
|
---|
249 |
|
---|
250 | if (stat)
|
---|
251 | stat->update((float)i / (float)num_quads);
|
---|
252 | }
|
---|
253 | }
|
---|
254 | delete gfp;
|
---|
255 | }
|
---|
256 | }
|
---|
257 | else i4_alert(i4gets("obj_missing"),100,&object_filename);
|
---|
258 |
|
---|
259 | if (stat)
|
---|
260 | delete stat;
|
---|
261 |
|
---|
262 | return ret;
|
---|
263 | }
|
---|
264 |
|
---|
265 | struct m1_tentry
|
---|
266 | {
|
---|
267 | w32 id;
|
---|
268 | w16 first_size;
|
---|
269 | w16 w, h;
|
---|
270 | w32 ns;
|
---|
271 | w8 *data;
|
---|
272 | w8 flags;
|
---|
273 | };
|
---|
274 |
|
---|
275 | struct m1_tarray
|
---|
276 | {
|
---|
277 | w32 id;
|
---|
278 | w16 total;
|
---|
279 | w32 *ids;
|
---|
280 | };
|
---|
281 |
|
---|
282 | static int id_compare(const m1_tentry *a, const m1_tentry *b)
|
---|
283 | {
|
---|
284 | if (a->id < b->id)
|
---|
285 | return -1;
|
---|
286 | else if (a->id > b->id)
|
---|
287 | return 1;
|
---|
288 | else return 0;
|
---|
289 | }
|
---|
290 |
|
---|
291 |
|
---|
292 |
|
---|
293 | void m1_convert_all_in_dir(const i4_const_str &dir)
|
---|
294 | {
|
---|
295 | i4_str *info=i4gets("converting_directory").sprintf(100,&dir);
|
---|
296 | i4_status_class *stat=i4_create_status(*info);
|
---|
297 | delete info;
|
---|
298 |
|
---|
299 |
|
---|
300 | i4_directory_struct ds;
|
---|
301 | if (i4_get_directory(dir, ds))
|
---|
302 | {
|
---|
303 | for (int i=0; i<ds.tfiles; i++)
|
---|
304 | {
|
---|
305 | i4_str *n=i4_const_str("%S/%S").sprintf(200, &dir, ds.files[i]);
|
---|
306 |
|
---|
307 | m1_update_texture(*n, i4_F, stat, i/(float)ds.tfiles, (i+1)/(float)ds.tfiles);
|
---|
308 | delete n;
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | if (stat)
|
---|
313 | delete stat;
|
---|
314 | }
|
---|