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