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