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