1 | /* |
1 | /* |
2 | * static char *rcsid_image_c = |
2 | * static char *rcsid_image_c = |
3 | * "$Id: image.C,v 1.2 2006/08/29 08:01:35 root Exp $"; |
3 | * "$Id: image.C,v 1.5 2006/09/08 16:51:42 root Exp $"; |
4 | */ |
4 | */ |
5 | |
5 | |
6 | /* |
6 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
7 | CrossFire, A Multiplayer game for X-windows |
8 | |
8 | |
… | |
… | |
42 | * that some number corresponds to the face at that number - for |
42 | * that some number corresponds to the face at that number - for |
43 | * xbm, it may not. At current time, these do in fact match because |
43 | * xbm, it may not. At current time, these do in fact match because |
44 | * the bmaps file is created in a sorted order. |
44 | * the bmaps file is created in a sorted order. |
45 | */ |
45 | */ |
46 | |
46 | |
47 | struct bmappair { |
47 | struct bmappair |
|
|
48 | { |
48 | char *name; |
49 | char *name; |
49 | unsigned int number; |
50 | unsigned int number; |
50 | }; |
51 | }; |
|
|
52 | |
|
|
53 | void free (bmappair *); // guard to catch free when delete should be used |
51 | |
54 | |
52 | static struct bmappair *xbm=NULL; |
55 | static struct bmappair *xbm=NULL; |
53 | |
56 | |
54 | /* Following can just as easily be pointers, but |
57 | /* Following can just as easily be pointers, but |
55 | * it is easier to keep them like this. |
58 | * it is easier to keep them like this. |
… | |
… | |
71 | int nrofpixmaps = 0; |
74 | int nrofpixmaps = 0; |
72 | |
75 | |
73 | /** |
76 | /** |
74 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
77 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
75 | */ |
78 | */ |
76 | struct smoothing { |
79 | struct smoothing : zero_initialised |
|
|
80 | { |
77 | uint16 id; |
81 | uint16 id; |
78 | uint16 smooth; |
82 | uint16 smooth; |
79 | }; |
83 | }; |
|
|
84 | |
|
|
85 | void free (smoothing *); // guard to catch free when delete should be used |
80 | |
86 | |
81 | /** |
87 | /** |
82 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
88 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
83 | * entries. It is sorted by smooth[].id. |
89 | * entries. It is sorted by smooth[].id. |
84 | */ |
90 | */ |
… | |
… | |
145 | FILE *fp; |
151 | FILE *fp; |
146 | |
152 | |
147 | sprintf(buf,"%s/faces", settings.datadir); |
153 | sprintf(buf,"%s/faces", settings.datadir); |
148 | LOG(llevDebug,"Reading faces from %s...",buf); |
154 | LOG(llevDebug,"Reading faces from %s...",buf); |
149 | if ((fp=fopen(buf,"r"))==NULL) { |
155 | if ((fp=fopen(buf,"r"))==NULL) { |
150 | LOG(llevError, "Cannot open faces file %s: %s\n", buf, strerror_local(errno)); |
156 | LOG(llevError, "Cannot open faces file %s: %s\n", buf, strerror(errno)); |
151 | exit(-1); |
157 | exit(-1); |
152 | } |
158 | } |
153 | while (fgets(buf, MAX_BUF, fp)!=NULL) { |
159 | while (fgets(buf, MAX_BUF, fp)!=NULL) { |
154 | if (*buf=='#') continue; |
160 | if (*buf=='#') continue; |
155 | if (!strncmp(buf,"end",3)) { |
161 | if (!strncmp(buf,"end",3)) { |
156 | on_face = NULL; |
162 | on_face = NULL; |
157 | } |
163 | } |
158 | else if (!strncmp(buf,"face",4)) { |
164 | else if (!strncmp(buf,"face",4)) { |
159 | int tmp; |
165 | int tmp; |
160 | |
166 | |
161 | cp = buf + 5; |
167 | cp = buf + 5; |
162 | cp[strlen(cp)-1] = '\0'; /* remove newline */ |
168 | cp[strlen(cp)-1] = '\0'; /* remove newline */ |
163 | |
169 | |
164 | if ((tmp=FindFace(cp,-1))==-1) { |
170 | if ((tmp=FindFace(cp,-1))==-1) { |
165 | LOG(llevError,"Could not find face %s\n", cp); |
171 | LOG(llevError,"Could not find face %s\n", cp); |
166 | continue; |
172 | continue; |
167 | } |
173 | } |
168 | on_face = &new_faces[tmp]; |
174 | on_face = &new_faces[tmp]; |
169 | on_face->visibility=0; |
175 | on_face->visibility=0; |
170 | } |
176 | } |
171 | else if (on_face==NULL) { |
177 | else if (on_face==NULL) { |
172 | LOG(llevError,"Got line with no face set: %s\n", buf); |
178 | LOG(llevError,"Got line with no face set: %s\n", buf); |
173 | } |
179 | } |
… | |
… | |
211 | |
217 | |
212 | bmaps_checksum=0; |
218 | bmaps_checksum=0; |
213 | sprintf (buf,"%s/bmaps", settings.datadir); |
219 | sprintf (buf,"%s/bmaps", settings.datadir); |
214 | LOG(llevDebug,"Reading bmaps from %s...",buf); |
220 | LOG(llevDebug,"Reading bmaps from %s...",buf); |
215 | if ((fp=fopen(buf,"r"))==NULL) { |
221 | if ((fp=fopen(buf,"r"))==NULL) { |
216 | LOG(llevError, "Cannot open bmaps file %s: %s\n", buf, strerror_local(errno)); |
222 | LOG(llevError, "Cannot open bmaps file %s: %s\n", buf, strerror(errno)); |
217 | exit(-1); |
223 | exit(-1); |
218 | } |
224 | } |
219 | |
225 | |
220 | /* First count how many bitmaps we have, so we can allocate correctly */ |
226 | /* First count how many bitmaps we have, so we can allocate correctly */ |
221 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
227 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
222 | if(buf[0] != '#' && buf[0] != '\n' ) |
228 | if(buf[0] != '#' && buf[0] != '\n' ) |
223 | nrofbmaps++; |
229 | nrofbmaps++; |
224 | rewind(fp); |
230 | rewind(fp); |
225 | |
231 | |
226 | xbm = (struct bmappair *) malloc(sizeof(struct bmappair) * nrofbmaps); |
232 | xbm = new bmappair [nrofbmaps]; |
227 | memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); |
233 | memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); |
228 | |
234 | |
229 | while(nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) { |
235 | while(nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) { |
230 | if (*buf == '#') |
236 | if (*buf == '#') |
231 | continue; |
237 | continue; |
… | |
… | |
265 | } |
271 | } |
266 | fclose(fp); |
272 | fclose(fp); |
267 | |
273 | |
268 | LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles); |
274 | LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles); |
269 | |
275 | |
270 | new_faces = (New_Face *)malloc(sizeof(New_Face) * nrofpixmaps); |
276 | new_faces = new New_Face [nrofpixmaps]; |
271 | for (i = 0; i < nrofpixmaps; i++) { |
277 | for (i = 0; i < nrofpixmaps; i++) { |
272 | new_faces[i].name = ""; |
278 | new_faces[i].name = ""; |
273 | new_faces[i].number = i; |
279 | new_faces[i].number = i; |
274 | new_faces[i].visibility=0; |
280 | new_faces[i].visibility=0; |
275 | new_faces[i].magicmap=255; |
281 | new_faces[i].magicmap=255; |
276 | } |
282 | } |
277 | for (i = 0; i < nroffiles; i++) { |
283 | for (i = 0; i < nroffiles; i++) { |
278 | new_faces[xbm[i].number].name = xbm[i].name; |
284 | new_faces[xbm[i].number].name = xbm[i].name; |
279 | } |
285 | } |
280 | |
286 | |
|
|
287 | // non-pod datatype, likely not allowed |
281 | qsort (xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
288 | qsort (xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
282 | |
289 | |
283 | ReadFaceData(); |
290 | ReadFaceData(); |
284 | |
291 | |
285 | for (i = 0; i < nrofpixmaps; i++) { |
292 | for (i = 0; i < nrofpixmaps; i++) { |
… | |
… | |
321 | */ |
328 | */ |
322 | int FindFace (const char *name, int error) { |
329 | int FindFace (const char *name, int error) { |
323 | struct bmappair *bp, tmp; |
330 | struct bmappair *bp, tmp; |
324 | char *p; |
331 | char *p; |
325 | |
332 | |
|
|
333 | if (!name) |
|
|
334 | return error; |
|
|
335 | |
326 | if ((p = strchr (name, '\n'))) |
336 | if ((p = strchr (name, '\n'))) |
327 | *p = '\0'; |
337 | *p = '\0'; |
328 | |
338 | |
329 | tmp.name = (char *)name; |
339 | tmp.name = (char *)name; |
330 | bp = (struct bmappair *)bsearch |
340 | bp = (struct bmappair *)bsearch |
… | |
… | |
347 | |
357 | |
348 | bmaps_checksum=0; |
358 | bmaps_checksum=0; |
349 | sprintf (buf,"%s/smooth", settings.datadir); |
359 | sprintf (buf,"%s/smooth", settings.datadir); |
350 | LOG(llevDebug,"Reading smooth from %s...",buf); |
360 | LOG(llevDebug,"Reading smooth from %s...",buf); |
351 | if ((fp=fopen(buf,"r"))==NULL) { |
361 | if ((fp=fopen(buf,"r"))==NULL) { |
352 | LOG(llevError, "Cannot open smooth file %s: %s\n", strerror_local(errno)); |
362 | LOG(llevError, "Cannot open smooth file %s: %s\n", strerror(errno)); |
353 | exit(-1); |
363 | exit(-1); |
354 | } |
364 | } |
355 | |
365 | |
356 | /* First count how many smooth we have, so we can allocate correctly */ |
366 | /* First count how many smooth we have, so we can allocate correctly */ |
357 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
367 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
358 | if(buf[0] != '#' && buf[0] != '\n' ) |
368 | if(buf[0] != '#' && buf[0] != '\n' ) |
359 | smoothcount++; |
369 | smoothcount++; |
360 | rewind(fp); |
370 | rewind(fp); |
361 | |
371 | |
362 | smooth = (struct smoothing *) malloc(sizeof(struct smoothing) * (smoothcount)); |
372 | smooth = new smoothing [smoothcount]; |
363 | memset (smooth, 0, sizeof (struct smoothing) * (smoothcount)); |
|
|
364 | |
373 | |
365 | while(nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp)!=NULL) { |
374 | while(nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp)!=NULL) { |
366 | if (*buf == '#') |
375 | if (*buf == '#') |
367 | continue; |
376 | continue; |
368 | p=strchr(buf,' '); |
377 | p=strchr(buf,' '); |
… | |
… | |
410 | { |
419 | { |
411 | int i; |
420 | int i; |
412 | |
421 | |
413 | for (i=0; i<nroffiles; i++) |
422 | for (i=0; i<nroffiles; i++) |
414 | free(xbm[i].name); |
423 | free(xbm[i].name); |
415 | free(xbm); |
424 | |
416 | free(new_faces); |
425 | delete [] xbm; |
417 | free(smooth); |
426 | delete [] new_faces; |
|
|
427 | delete [] smooth; |
418 | } |
428 | } |