|
|
1 | |
1 | /* |
2 | /* |
2 | * static char *rcsid_image_c = |
3 | * static char *rcsid_image_c = |
3 | * "$Id: image.C,v 1.4 2006/09/04 11:07:59 root Exp $"; |
4 | * "$Id: image.C,v 1.6 2006/09/10 16:00:23 root Exp $"; |
4 | */ |
5 | */ |
5 | |
6 | |
6 | /* |
7 | /* |
7 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
8 | |
9 | |
… | |
… | |
48 | { |
49 | { |
49 | char *name; |
50 | char *name; |
50 | unsigned int number; |
51 | unsigned int number; |
51 | }; |
52 | }; |
52 | |
53 | |
53 | void free (bmappair *); // guard to catch free when delete should be used |
54 | void free (bmappair *); // guard to catch free when delete should be used |
54 | |
55 | |
55 | static struct bmappair *xbm=NULL; |
56 | static struct bmappair *xbm = NULL; |
56 | |
57 | |
57 | /* Following can just as easily be pointers, but |
58 | /* Following can just as easily be pointers, but |
58 | * it is easier to keep them like this. |
59 | * it is easier to keep them like this. |
59 | */ |
60 | */ |
60 | New_Face *blank_face, *dark_faces[3], *empty_face, *smooth_face; |
61 | New_Face *blank_face, *dark_faces[3], *empty_face, *smooth_face; |
… | |
… | |
74 | int nrofpixmaps = 0; |
75 | int nrofpixmaps = 0; |
75 | |
76 | |
76 | /** |
77 | /** |
77 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
78 | * id is the face to smooth, smooth is the 16x2 face used to smooth id. |
78 | */ |
79 | */ |
79 | struct smoothing : zero_initialised |
80 | struct smoothing:zero_initialised |
80 | { |
81 | { |
81 | uint16 id; |
82 | uint16 id; |
82 | uint16 smooth; |
83 | uint16 smooth; |
83 | }; |
84 | }; |
84 | |
85 | |
85 | void free (smoothing *); // guard to catch free when delete should be used |
86 | void free (smoothing *); // guard to catch free when delete should be used |
86 | |
87 | |
87 | /** |
88 | /** |
88 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
89 | * Contains all defined smoothing entries. smooth is an array of nrofsmooth |
89 | * entries. It is sorted by smooth[].id. |
90 | * entries. It is sorted by smooth[].id. |
90 | */ |
91 | */ |
91 | static struct smoothing *smooth=NULL; |
92 | static struct smoothing *smooth = NULL; |
92 | int nrofsmooth=0; |
93 | int nrofsmooth = 0; |
93 | |
94 | |
94 | /* the only thing this table is used for now is to |
95 | /* the only thing this table is used for now is to |
95 | * translate the colorname in the magicmap field of the |
96 | * translate the colorname in the magicmap field of the |
96 | * face into a numeric index that is then sent to the |
97 | * face into a numeric index that is then sent to the |
97 | * client for magic map commands. The order of this table |
98 | * client for magic map commands. The order of this table |
98 | * must match that of the NDI colors in include/newclient.h. |
99 | * must match that of the NDI colors in include/newclient.h. |
99 | */ |
100 | */ |
100 | static const char *const colorname[] = { |
101 | static const char *const colorname[] = { |
101 | "black", /* 0 */ |
102 | "black", /* 0 */ |
102 | "white", /* 1 */ |
103 | "white", /* 1 */ |
103 | "blue", /* 2 */ |
104 | "blue", /* 2 */ |
104 | "red", /* 3 */ |
105 | "red", /* 3 */ |
105 | "orange", /* 4 */ |
106 | "orange", /* 4 */ |
106 | "light_blue", /* 5 */ |
107 | "light_blue", /* 5 */ |
107 | "dark_orange", /* 6 */ |
108 | "dark_orange", /* 6 */ |
108 | "green", /* 7 */ |
109 | "green", /* 7 */ |
109 | "light_green", /* 8 */ |
110 | "light_green", /* 8 */ |
110 | "grey", /* 9 */ |
111 | "grey", /* 9 */ |
111 | "brown", /* 10 */ |
112 | "brown", /* 10 */ |
112 | "yellow", /* 11 */ |
113 | "yellow", /* 11 */ |
113 | "khaki" /* 12 */ |
114 | "khaki" /* 12 */ |
114 | }; |
115 | }; |
115 | |
116 | |
|
|
117 | static int |
116 | static int compar (const struct bmappair *a, const struct bmappair *b) { |
118 | compar (const struct bmappair *a, const struct bmappair *b) |
|
|
119 | { |
117 | return strcmp (a->name, b->name); |
120 | return strcmp (a->name, b->name); |
118 | } |
121 | } |
|
|
122 | static int |
119 | static int compar_smooth (const struct smoothing *a, const struct smoothing *b) { |
123 | compar_smooth (const struct smoothing *a, const struct smoothing *b) |
|
|
124 | { |
120 | if (a->id<b->id) |
125 | if (a->id < b->id) |
121 | return -1; |
126 | return -1; |
122 | if (b->id<a->id) |
127 | if (b->id < a->id) |
123 | return 1; |
|
|
124 | return 0; |
128 | return 1; |
|
|
129 | return 0; |
125 | } |
130 | } |
126 | |
131 | |
127 | |
132 | |
128 | /* |
133 | /* |
129 | * Returns the matching color in the coloralias if found, |
134 | * Returns the matching color in the coloralias if found, |
130 | * 0 otherwise. Note that 0 will actually be black, so there is no |
135 | * 0 otherwise. Note that 0 will actually be black, so there is no |
131 | * way the calling function can tell if an error occurred or not |
136 | * way the calling function can tell if an error occurred or not |
132 | */ |
137 | */ |
133 | |
138 | |
|
|
139 | static uint8 |
134 | static uint8 find_color(const char *name) { |
140 | find_color (const char *name) |
|
|
141 | { |
135 | uint8 i; |
142 | uint8 i; |
|
|
143 | |
136 | for(i=0;i<sizeof(colorname)/sizeof(*colorname);i++) |
144 | for (i = 0; i < sizeof (colorname) / sizeof (*colorname); i++) |
137 | if(!strcmp(name,colorname[i])) |
145 | if (!strcmp (name, colorname[i])) |
138 | return i; |
146 | return i; |
139 | LOG(llevError,"Unknown color: %s\n",name); |
147 | LOG (llevError, "Unknown color: %s\n", name); |
140 | return 0; |
148 | return 0; |
141 | } |
149 | } |
142 | |
150 | |
143 | /* This reads the lib/faces file, getting color and visibility information. |
151 | /* This reads the lib/faces file, getting color and visibility information. |
144 | * it is called by ReadBmapNames. |
152 | * it is called by ReadBmapNames. |
145 | */ |
153 | */ |
146 | |
154 | |
147 | static void ReadFaceData(void) |
155 | static void |
|
|
156 | ReadFaceData (void) |
148 | { |
157 | { |
149 | char buf[MAX_BUF], *cp; |
158 | char buf[MAX_BUF], *cp; |
150 | New_Face *on_face=NULL; |
159 | New_Face *on_face = NULL; |
151 | FILE *fp; |
160 | FILE *fp; |
152 | |
161 | |
153 | sprintf(buf,"%s/faces", settings.datadir); |
162 | sprintf (buf, "%s/faces", settings.datadir); |
154 | LOG(llevDebug,"Reading faces from %s...",buf); |
163 | LOG (llevDebug, "Reading faces from %s...", buf); |
155 | if ((fp=fopen(buf,"r"))==NULL) { |
164 | if ((fp = fopen (buf, "r")) == NULL) |
|
|
165 | { |
156 | LOG(llevError, "Cannot open faces file %s: %s\n", buf, strerror(errno)); |
166 | LOG (llevError, "Cannot open faces file %s: %s\n", buf, strerror (errno)); |
157 | exit(-1); |
167 | exit (-1); |
158 | } |
168 | } |
159 | while (fgets(buf, MAX_BUF, fp)!=NULL) { |
169 | while (fgets (buf, MAX_BUF, fp) != NULL) |
160 | if (*buf=='#') continue; |
170 | { |
|
|
171 | if (*buf == '#') |
|
|
172 | continue; |
161 | if (!strncmp(buf,"end",3)) { |
173 | if (!strncmp (buf, "end", 3)) |
|
|
174 | { |
162 | on_face = NULL; |
175 | on_face = NULL; |
163 | } |
176 | } |
164 | else if (!strncmp(buf,"face",4)) { |
177 | else if (!strncmp (buf, "face", 4)) |
|
|
178 | { |
165 | int tmp; |
179 | int tmp; |
166 | |
180 | |
167 | cp = buf + 5; |
181 | cp = buf + 5; |
168 | cp[strlen(cp)-1] = '\0'; /* remove newline */ |
182 | cp[strlen (cp) - 1] = '\0'; /* remove newline */ |
169 | |
183 | |
170 | if ((tmp=FindFace(cp,-1))==-1) { |
184 | if ((tmp = FindFace (cp, -1)) == -1) |
|
|
185 | { |
171 | LOG(llevError,"Could not find face %s\n", cp); |
186 | LOG (llevError, "Could not find face %s\n", cp); |
172 | continue; |
187 | continue; |
173 | } |
188 | } |
174 | on_face = &new_faces[tmp]; |
189 | on_face = &new_faces[tmp]; |
175 | on_face->visibility=0; |
190 | on_face->visibility = 0; |
176 | } |
191 | } |
177 | else if (on_face==NULL) { |
192 | else if (on_face == NULL) |
|
|
193 | { |
178 | LOG(llevError,"Got line with no face set: %s\n", buf); |
194 | LOG (llevError, "Got line with no face set: %s\n", buf); |
179 | } |
195 | } |
180 | else if (!strncmp(buf,"color_fg",8)) { |
196 | else if (!strncmp (buf, "color_fg", 8)) |
|
|
197 | { |
181 | cp = buf + 9; |
198 | cp = buf + 9; |
182 | cp[strlen(cp)-1] = '\0'; |
199 | cp[strlen (cp) - 1] = '\0'; |
183 | if (on_face->magicmap==255) on_face->magicmap=find_color(cp); |
200 | if (on_face->magicmap == 255) |
|
|
201 | on_face->magicmap = find_color (cp); |
184 | } |
202 | } |
185 | else if (!strncmp(buf,"color_bg",8)) { |
203 | else if (!strncmp (buf, "color_bg", 8)) |
|
|
204 | { |
186 | /* ignore it */ |
205 | /* ignore it */ |
187 | } |
206 | } |
188 | else if (!strncmp(buf,"visibility",10)) { |
207 | else if (!strncmp (buf, "visibility", 10)) |
|
|
208 | { |
189 | on_face->visibility = atoi(buf + 11); |
209 | on_face->visibility = atoi (buf + 11); |
190 | } |
210 | } |
191 | else if (!strncmp(buf,"magicmap",8)) { |
211 | else if (!strncmp (buf, "magicmap", 8)) |
|
|
212 | { |
192 | cp=buf+9; |
213 | cp = buf + 9; |
193 | cp[strlen(cp)-1] = '\0'; |
214 | cp[strlen (cp) - 1] = '\0'; |
194 | on_face->magicmap=find_color(cp); |
215 | on_face->magicmap = find_color (cp); |
195 | } |
216 | } |
196 | else if (!strncmp(buf,"is_floor",8)) { |
217 | else if (!strncmp (buf, "is_floor", 8)) |
|
|
218 | { |
197 | int value = atoi(buf+9); |
219 | int value = atoi (buf + 9); |
|
|
220 | |
|
|
221 | if (value) |
198 | if (value) on_face->magicmap |= FACE_FLOOR; |
222 | on_face->magicmap |= FACE_FLOOR; |
199 | } |
223 | } |
|
|
224 | else |
200 | else LOG(llevDebug,"Got unknown line in faces file: %s\n", buf); |
225 | LOG (llevDebug, "Got unknown line in faces file: %s\n", buf); |
201 | } |
226 | } |
202 | LOG(llevDebug,"done\n"); |
227 | LOG (llevDebug, "done\n"); |
203 | fclose(fp); |
228 | fclose (fp); |
204 | } |
229 | } |
205 | |
230 | |
206 | /* This reads the bmaps file to get all the bitmap names and |
231 | /* This reads the bmaps file to get all the bitmap names and |
207 | * stuff. It only needs to be done once, because it is player |
232 | * stuff. It only needs to be done once, because it is player |
208 | * independent (ie, what display the person is on will not make a |
233 | * independent (ie, what display the person is on will not make a |
209 | * difference.) |
234 | * difference.) |
210 | */ |
235 | */ |
211 | |
236 | |
|
|
237 | void |
212 | void ReadBmapNames (void) { |
238 | ReadBmapNames (void) |
|
|
239 | { |
213 | char buf[MAX_BUF], *p, *q; |
240 | char buf[MAX_BUF], *p, *q; |
214 | FILE *fp; |
241 | FILE *fp; |
215 | int value, nrofbmaps = 0, i; |
242 | int value, nrofbmaps = 0, i; |
216 | size_t l; |
243 | size_t l; |
217 | |
244 | |
218 | bmaps_checksum=0; |
245 | bmaps_checksum = 0; |
219 | sprintf (buf,"%s/bmaps", settings.datadir); |
246 | sprintf (buf, "%s/bmaps", settings.datadir); |
220 | LOG(llevDebug,"Reading bmaps from %s...",buf); |
247 | LOG (llevDebug, "Reading bmaps from %s...", buf); |
221 | if ((fp=fopen(buf,"r"))==NULL) { |
248 | if ((fp = fopen (buf, "r")) == NULL) |
|
|
249 | { |
222 | LOG(llevError, "Cannot open bmaps file %s: %s\n", buf, strerror(errno)); |
250 | LOG (llevError, "Cannot open bmaps file %s: %s\n", buf, strerror (errno)); |
223 | exit(-1); |
251 | exit (-1); |
224 | } |
|
|
225 | |
252 | } |
|
|
253 | |
226 | /* First count how many bitmaps we have, so we can allocate correctly */ |
254 | /* First count how many bitmaps we have, so we can allocate correctly */ |
227 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
255 | while (fgets (buf, MAX_BUF, fp) != NULL) |
228 | if(buf[0] != '#' && buf[0] != '\n' ) |
256 | if (buf[0] != '#' && buf[0] != '\n') |
229 | nrofbmaps++; |
257 | nrofbmaps++; |
230 | rewind(fp); |
258 | rewind (fp); |
231 | |
259 | |
232 | xbm = new bmappair [nrofbmaps]; |
260 | xbm = new bmappair[nrofbmaps]; |
233 | memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); |
261 | memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); |
234 | |
262 | |
235 | while(nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) { |
263 | while (nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) |
|
|
264 | { |
236 | if (*buf == '#') |
265 | if (*buf == '#') |
237 | continue; |
266 | continue; |
238 | |
267 | |
239 | p = (*buf == '\\') ? (buf + 1): buf; |
268 | p = (*buf == '\\') ? (buf + 1) : buf; |
240 | if (!(p = strtok (p , " \t")) || !(q = strtok (NULL , " \t\n"))) { |
269 | if (!(p = strtok (p, " \t")) || !(q = strtok (NULL, " \t\n"))) |
|
|
270 | { |
241 | LOG(llevDebug,"Warning, syntax error: %s\n", buf); |
271 | LOG (llevDebug, "Warning, syntax error: %s\n", buf); |
242 | continue; |
272 | continue; |
243 | } |
273 | } |
244 | value = atoi (p); |
274 | value = atoi (p); |
245 | xbm[nroffiles].name = strdup_local(q); |
275 | xbm[nroffiles].name = strdup_local (q); |
246 | |
276 | |
247 | /* We need to calculate the checksum of the bmaps file |
277 | /* We need to calculate the checksum of the bmaps file |
248 | * name->number mapping to send to the client. This does not |
278 | * name->number mapping to send to the client. This does not |
249 | * need to match what sum or other utility may come up with - |
279 | * need to match what sum or other utility may come up with - |
250 | * as long as we get the same results on the same real file |
280 | * as long as we get the same results on the same real file |
251 | * data, it does the job as it lets the client know if |
281 | * data, it does the job as it lets the client know if |
252 | * the file has the same data or not. |
282 | * the file has the same data or not. |
253 | */ |
283 | */ |
254 | ROTATE_RIGHT(bmaps_checksum); |
284 | ROTATE_RIGHT (bmaps_checksum); |
255 | bmaps_checksum += value & 0xff; |
285 | bmaps_checksum += value & 0xff; |
256 | bmaps_checksum &= 0xffffffff; |
286 | bmaps_checksum &= 0xffffffff; |
257 | |
287 | |
258 | ROTATE_RIGHT(bmaps_checksum); |
288 | ROTATE_RIGHT (bmaps_checksum); |
259 | bmaps_checksum += (value >> 8) & 0xff; |
289 | bmaps_checksum += (value >> 8) & 0xff; |
260 | bmaps_checksum &= 0xffffffff; |
290 | bmaps_checksum &= 0xffffffff; |
261 | for (l=0; l<strlen(q); l++) { |
291 | for (l = 0; l < strlen (q); l++) |
|
|
292 | { |
262 | ROTATE_RIGHT(bmaps_checksum); |
293 | ROTATE_RIGHT (bmaps_checksum); |
263 | bmaps_checksum += q[l]; |
294 | bmaps_checksum += q[l]; |
264 | bmaps_checksum &= 0xffffffff; |
295 | bmaps_checksum &= 0xffffffff; |
265 | } |
296 | } |
266 | |
297 | |
267 | xbm[nroffiles].number = value; |
298 | xbm[nroffiles].number = value; |
268 | nroffiles++; |
299 | nroffiles++; |
269 | if(value >= nrofpixmaps) |
300 | if (value >= nrofpixmaps) |
270 | nrofpixmaps = value+1; |
301 | nrofpixmaps = value + 1; |
271 | } |
302 | } |
272 | fclose(fp); |
303 | fclose (fp); |
273 | |
304 | |
274 | LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles); |
305 | LOG (llevDebug, "done (got %d/%d/%d)\n", nrofpixmaps, nrofbmaps, nroffiles); |
275 | |
306 | |
276 | new_faces = new New_Face [nrofpixmaps]; |
307 | new_faces = new New_Face[nrofpixmaps]; |
|
|
308 | |
277 | for (i = 0; i < nrofpixmaps; i++) { |
309 | for (i = 0; i < nrofpixmaps; i++) |
|
|
310 | { |
278 | new_faces[i].name = ""; |
311 | new_faces[i].name = ""; |
279 | new_faces[i].number = i; |
312 | new_faces[i].number = i; |
280 | new_faces[i].visibility=0; |
313 | new_faces[i].visibility = 0; |
281 | new_faces[i].magicmap=255; |
314 | new_faces[i].magicmap = 255; |
282 | } |
315 | } |
283 | for (i = 0; i < nroffiles; i++) { |
316 | for (i = 0; i < nroffiles; i++) |
|
|
317 | { |
284 | new_faces[xbm[i].number].name = xbm[i].name; |
318 | new_faces[xbm[i].number].name = xbm[i].name; |
285 | } |
319 | } |
286 | |
320 | |
287 | // non-pod datatype, likely not allowed |
321 | // non-pod datatype, likely not allowed |
288 | qsort (xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
322 | qsort (xbm, nroffiles, sizeof (struct bmappair), (int (*)(const void *, const void *)) compar); |
289 | |
323 | |
290 | ReadFaceData(); |
324 | ReadFaceData (); |
291 | |
325 | |
292 | for (i = 0; i < nrofpixmaps; i++) { |
326 | for (i = 0; i < nrofpixmaps; i++) |
|
|
327 | { |
293 | if (new_faces[i].magicmap==255) { |
328 | if (new_faces[i].magicmap == 255) |
|
|
329 | { |
294 | #if 0 /* Useful for initial debugging, not needed now */ |
330 | #if 0 /* Useful for initial debugging, not needed now */ |
295 | LOG(llevDebug,"Face %s still had default magicmap, resetting to black\n", |
331 | LOG (llevDebug, "Face %s still had default magicmap, resetting to black\n", new_faces[i].name); |
296 | new_faces[i].name); |
|
|
297 | #endif |
332 | #endif |
298 | new_faces[i].magicmap=0; |
333 | new_faces[i].magicmap = 0; |
299 | } |
334 | } |
300 | } |
335 | } |
301 | /* Actually forcefully setting the colors here probably should not |
336 | /* Actually forcefully setting the colors here probably should not |
302 | * be done - it could easily create confusion. |
337 | * be done - it could easily create confusion. |
303 | */ |
338 | */ |
304 | blank_face = &new_faces[FindFace(BLANK_FACE_NAME, 0)]; |
339 | blank_face = &new_faces[FindFace (BLANK_FACE_NAME, 0)]; |
305 | blank_face->magicmap = find_color ("khaki") | FACE_FLOOR; |
340 | blank_face->magicmap = find_color ("khaki") | FACE_FLOOR; |
306 | |
341 | |
307 | empty_face = &new_faces[FindFace(EMPTY_FACE_NAME, 0)]; |
342 | empty_face = &new_faces[FindFace (EMPTY_FACE_NAME, 0)]; |
308 | |
343 | |
309 | dark_faces[0] = &new_faces[FindFace (DARK_FACE1_NAME,0)]; |
344 | dark_faces[0] = &new_faces[FindFace (DARK_FACE1_NAME, 0)]; |
310 | dark_faces[1] = &new_faces[FindFace (DARK_FACE2_NAME,0)]; |
345 | dark_faces[1] = &new_faces[FindFace (DARK_FACE2_NAME, 0)]; |
311 | dark_faces[2] = &new_faces[FindFace (DARK_FACE3_NAME,0)]; |
346 | dark_faces[2] = &new_faces[FindFace (DARK_FACE3_NAME, 0)]; |
312 | |
347 | |
313 | smooth_face = &new_faces[FindFace(SMOOTH_FACE_NAME,0)]; |
348 | smooth_face = &new_faces[FindFace (SMOOTH_FACE_NAME, 0)]; |
314 | } |
349 | } |
315 | |
350 | |
316 | /* This returns an the face number of face 'name'. Number is constant |
351 | /* This returns an the face number of face 'name'. Number is constant |
317 | * during an invocation, but not necessarily between versions (this |
352 | * during an invocation, but not necessarily between versions (this |
318 | * is because the faces are arranged in alphabetical order, so |
353 | * is because the faces are arranged in alphabetical order, so |
… | |
… | |
324 | * you want some default face used, or can be set to negative |
359 | * you want some default face used, or can be set to negative |
325 | * so that it will be known that the face could not be found |
360 | * so that it will be known that the face could not be found |
326 | * (needed in client, so that it will know to request that image |
361 | * (needed in client, so that it will know to request that image |
327 | * from the server) |
362 | * from the server) |
328 | */ |
363 | */ |
|
|
364 | int |
329 | int FindFace (const char *name, int error) { |
365 | FindFace (const char *name, int error) |
|
|
366 | { |
330 | struct bmappair *bp, tmp; |
367 | struct bmappair *bp, tmp; |
331 | char *p; |
368 | char *p; |
332 | |
369 | |
|
|
370 | if (!name) |
|
|
371 | return error; |
|
|
372 | |
333 | if ((p = strchr (name, '\n'))) |
373 | if ((p = strchr (name, '\n'))) |
334 | *p = '\0'; |
374 | *p = '\0'; |
335 | |
375 | |
336 | tmp.name = (char *)name; |
376 | tmp.name = (char *) name; |
337 | bp = (struct bmappair *)bsearch |
|
|
338 | (&tmp, xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); |
377 | bp = (struct bmappair *) bsearch (&tmp, xbm, nroffiles, sizeof (struct bmappair), (int (*)(const void *, const void *)) compar); |
339 | |
378 | |
340 | return bp ? bp->number : error; |
379 | return bp ? bp->number : error; |
341 | } |
380 | } |
342 | |
381 | |
343 | /* Reads the smooth file to know how to smooth datas. |
382 | /* Reads the smooth file to know how to smooth datas. |
344 | * the smooth file if made of 2 elements lines. |
383 | * the smooth file if made of 2 elements lines. |
345 | * lines starting with # are comment |
384 | * lines starting with # are comment |
346 | * the first element of line is face to smooth |
385 | * the first element of line is face to smooth |
347 | * the next element is the 16x2 faces picture |
386 | * the next element is the 16x2 faces picture |
348 | * used for smoothing |
387 | * used for smoothing |
349 | */ |
388 | */ |
|
|
389 | int |
350 | int ReadSmooth (void) { |
390 | ReadSmooth (void) |
|
|
391 | { |
351 | char buf[MAX_BUF], *p, *q; |
392 | char buf[MAX_BUF], *p, *q; |
352 | FILE *fp; |
393 | FILE *fp; |
353 | int smoothcount = 0; |
394 | int smoothcount = 0; |
354 | |
395 | |
355 | bmaps_checksum=0; |
396 | bmaps_checksum = 0; |
356 | sprintf (buf,"%s/smooth", settings.datadir); |
397 | sprintf (buf, "%s/smooth", settings.datadir); |
357 | LOG(llevDebug,"Reading smooth from %s...",buf); |
398 | LOG (llevDebug, "Reading smooth from %s...", buf); |
358 | if ((fp=fopen(buf,"r"))==NULL) { |
399 | if ((fp = fopen (buf, "r")) == NULL) |
|
|
400 | { |
359 | LOG(llevError, "Cannot open smooth file %s: %s\n", strerror(errno)); |
401 | LOG (llevError, "Cannot open smooth file %s: %s\n", strerror (errno)); |
360 | exit(-1); |
402 | exit (-1); |
361 | } |
403 | } |
362 | |
404 | |
363 | /* First count how many smooth we have, so we can allocate correctly */ |
405 | /* First count how many smooth we have, so we can allocate correctly */ |
364 | while (fgets (buf, MAX_BUF, fp)!=NULL) |
406 | while (fgets (buf, MAX_BUF, fp) != NULL) |
365 | if(buf[0] != '#' && buf[0] != '\n' ) |
407 | if (buf[0] != '#' && buf[0] != '\n') |
366 | smoothcount++; |
408 | smoothcount++; |
367 | rewind(fp); |
409 | rewind (fp); |
368 | |
410 | |
369 | smooth = new smoothing [smoothcount]; |
411 | smooth = new smoothing[smoothcount]; |
370 | |
412 | |
371 | while(nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp)!=NULL) { |
413 | while (nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp) != NULL) |
|
|
414 | { |
372 | if (*buf == '#') |
415 | if (*buf == '#') |
373 | continue; |
416 | continue; |
374 | p=strchr(buf,' '); |
417 | p = strchr (buf, ' '); |
375 | if (!p) |
418 | if (!p) |
376 | continue; |
419 | continue; |
377 | *p='\0'; |
420 | *p = '\0'; |
378 | q=buf; |
421 | q = buf; |
379 | smooth[nrofsmooth].id=FindFace(q,0); |
422 | smooth[nrofsmooth].id = FindFace (q, 0); |
380 | q=p+1; |
423 | q = p + 1; |
381 | smooth[nrofsmooth].smooth=FindFace(q,0); |
424 | smooth[nrofsmooth].smooth = FindFace (q, 0); |
382 | nrofsmooth++; |
425 | nrofsmooth++; |
383 | } |
426 | } |
384 | fclose(fp); |
427 | fclose (fp); |
385 | |
428 | |
386 | LOG(llevDebug,"done (got %d smooth entries)\n",nrofsmooth); |
429 | LOG (llevDebug, "done (got %d smooth entries)\n", nrofsmooth); |
387 | qsort (smooth, nrofsmooth, sizeof(struct smoothing), (int (*)(const void*, const void*))compar_smooth); |
430 | qsort (smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth); |
388 | return nrofsmooth; |
431 | return nrofsmooth; |
389 | } |
432 | } |
390 | |
433 | |
391 | /** |
434 | /** |
392 | * Find the smooth face for a given face. |
435 | * Find the smooth face for a given face. |
393 | * |
436 | * |
… | |
… | |
395 | * |
438 | * |
396 | * @param smoothed return value: set to smooth face |
439 | * @param smoothed return value: set to smooth face |
397 | * |
440 | * |
398 | * @return 1=smooth face found, 0=no smooth face found |
441 | * @return 1=smooth face found, 0=no smooth face found |
399 | */ |
442 | */ |
|
|
443 | int |
400 | int FindSmooth (uint16 face, uint16* smoothed) { |
444 | FindSmooth (uint16 face, uint16 * smoothed) |
|
|
445 | { |
401 | struct smoothing *bp, tmp; |
446 | struct smoothing *bp, tmp; |
402 | |
447 | |
403 | tmp.id = face; |
448 | tmp.id = face; |
404 | bp = (struct smoothing *)bsearch |
449 | bp = (struct smoothing *) bsearch |
405 | (&tmp, smooth, nrofsmooth, sizeof(struct smoothing), (int (*)(const void*, const void*))compar_smooth); |
450 | (&tmp, smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth); |
406 | (*smoothed)=0; |
451 | (*smoothed) = 0; |
407 | if (bp) |
452 | if (bp) |
408 | (*smoothed)=bp->smooth; |
453 | (*smoothed) = bp->smooth; |
409 | return bp ? 1 : 0; |
454 | return bp ? 1 : 0; |
410 | } |
455 | } |
411 | |
456 | |
412 | /** |
457 | /** |
413 | * Deallocates memory allocated by ReadBmapNames() and ReadSmooth(). |
458 | * Deallocates memory allocated by ReadBmapNames() and ReadSmooth(). |
414 | */ |
459 | */ |
|
|
460 | void |
415 | void free_all_images(void) |
461 | free_all_images (void) |
416 | { |
462 | { |
417 | int i; |
463 | int i; |
418 | |
464 | |
419 | for (i=0; i<nroffiles; i++) |
465 | for (i = 0; i < nroffiles; i++) |
420 | free(xbm[i].name); |
466 | free (xbm[i].name); |
421 | |
467 | |
422 | delete [] xbm; |
468 | delete[]xbm; |
423 | delete [] new_faces; |
469 | delete[]new_faces; |
424 | delete [] smooth; |
470 | delete[]smooth; |
425 | } |
471 | } |