ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/image.C
(Generate patch)

Comparing deliantra/server/common/image.C (file contents):
Revision 1.1 by elmex, Sun Aug 13 17:16:00 2006 UTC vs.
Revision 1.15 by root, Mon Feb 5 02:17:28 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines