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.5 by root, Fri Sep 8 16:51:42 2006 UTC

1/* 1/*
2 * static char *rcsid_image_c = 2 * static char *rcsid_image_c =
3 * "$Id: image.C,v 1.1 2006/08/13 17:16:00 elmex Exp $"; 3 * "$Id: image.C,v 1.5 2006/09/08 16:51:42 root Exp $";
4 */ 4 */
5 5
6/* 6/*
7 CrossFire, A Multiplayer game for X-windows 7 CrossFire, A Multiplayer game for X-windows
8 8
42 * that some number corresponds to the face at that number - for 42 * 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 43 * xbm, it may not. At current time, these do in fact match because
44 * the bmaps file is created in a sorted order. 44 * the bmaps file is created in a sorted order.
45 */ 45 */
46 46
47struct bmappair { 47struct bmappair
48{
48 char *name; 49 char *name;
49 unsigned int number; 50 unsigned int number;
50}; 51};
52
53void free (bmappair *); // guard to catch free when delete should be used
51 54
52static struct bmappair *xbm=NULL; 55static struct bmappair *xbm=NULL;
53 56
54/* Following can just as easily be pointers, but 57/* Following can just as easily be pointers, but
55 * it is easier to keep them like this. 58 * it is easier to keep them like this.
71int nrofpixmaps = 0; 74int nrofpixmaps = 0;
72 75
73/** 76/**
74 * id is the face to smooth, smooth is the 16x2 face used to smooth id. 77 * id is the face to smooth, smooth is the 16x2 face used to smooth id.
75 */ 78 */
76struct smoothing { 79struct smoothing : zero_initialised
80{
77 uint16 id; 81 uint16 id;
78 uint16 smooth; 82 uint16 smooth;
79}; 83};
84
85void free (smoothing *); // guard to catch free when delete should be used
80 86
81/** 87/**
82 * Contains all defined smoothing entries. smooth is an array of nrofsmooth 88 * Contains all defined smoothing entries. smooth is an array of nrofsmooth
83 * entries. It is sorted by smooth[].id. 89 * entries. It is sorted by smooth[].id.
84 */ 90 */
145 FILE *fp; 151 FILE *fp;
146 152
147 sprintf(buf,"%s/faces", settings.datadir); 153 sprintf(buf,"%s/faces", settings.datadir);
148 LOG(llevDebug,"Reading faces from %s...",buf); 154 LOG(llevDebug,"Reading faces from %s...",buf);
149 if ((fp=fopen(buf,"r"))==NULL) { 155 if ((fp=fopen(buf,"r"))==NULL) {
150 LOG(llevError, "Cannot open faces file %s: %s\n", buf, strerror_local(errno)); 156 LOG(llevError, "Cannot open faces file %s: %s\n", buf, strerror(errno));
151 exit(-1); 157 exit(-1);
152 } 158 }
153 while (fgets(buf, MAX_BUF, fp)!=NULL) { 159 while (fgets(buf, MAX_BUF, fp)!=NULL) {
154 if (*buf=='#') continue; 160 if (*buf=='#') continue;
155 if (!strncmp(buf,"end",3)) { 161 if (!strncmp(buf,"end",3)) {
156 on_face = NULL; 162 on_face = NULL;
157 } 163 }
158 else if (!strncmp(buf,"face",4)) { 164 else if (!strncmp(buf,"face",4)) {
159 int tmp; 165 int tmp;
160 166
161 cp = buf + 5; 167 cp = buf + 5;
162 cp[strlen(cp)-1] = '\0'; /* remove newline */ 168 cp[strlen(cp)-1] = '\0'; /* remove newline */
163 169
164 if ((tmp=FindFace(cp,-1))==-1) { 170 if ((tmp=FindFace(cp,-1))==-1) {
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) {
172 LOG(llevError,"Got line with no face set: %s\n", buf); 178 LOG(llevError,"Got line with no face set: %s\n", buf);
173 } 179 }
174 else if (!strncmp(buf,"color_fg",8)) { 180 else if (!strncmp(buf,"color_fg",8)) {
175 cp = buf + 9; 181 cp = buf + 9;
176 cp[strlen(cp)-1] = '\0'; 182 cp[strlen(cp)-1] = '\0';
177 if (on_face->magicmap==255) on_face->magicmap=find_color(cp); 183 if (on_face->magicmap==255) on_face->magicmap=find_color(cp);
178 } 184 }
179 else if (!strncmp(buf,"color_bg",8)) { 185 else if (!strncmp(buf,"color_bg",8)) {
180 /* ignore it */ 186 /* ignore it */
181 } 187 }
182 else if (!strncmp(buf,"visibility",10)) { 188 else if (!strncmp(buf,"visibility",10)) {
183 on_face->visibility = atoi(buf + 11); 189 on_face->visibility = atoi(buf + 11);
184 } 190 }
185 else if (!strncmp(buf,"magicmap",8)) { 191 else if (!strncmp(buf,"magicmap",8)) {
186 cp=buf+9; 192 cp=buf+9;
187 cp[strlen(cp)-1] = '\0'; 193 cp[strlen(cp)-1] = '\0';
188 on_face->magicmap=find_color(cp); 194 on_face->magicmap=find_color(cp);
189 } 195 }
190 else if (!strncmp(buf,"is_floor",8)) { 196 else if (!strncmp(buf,"is_floor",8)) {
191 int value = atoi(buf+9); 197 int value = atoi(buf+9);
192 if (value) on_face->magicmap |= FACE_FLOOR; 198 if (value) on_face->magicmap |= FACE_FLOOR;
193 } 199 }
194 else LOG(llevDebug,"Got unknown line in faces file: %s\n", buf); 200 else LOG(llevDebug,"Got unknown line in faces file: %s\n", buf);
195 } 201 }
196 LOG(llevDebug,"done\n"); 202 LOG(llevDebug,"done\n");
197 fclose(fp); 203 fclose(fp);
198} 204}
199 205
211 217
212 bmaps_checksum=0; 218 bmaps_checksum=0;
213 sprintf (buf,"%s/bmaps", settings.datadir); 219 sprintf (buf,"%s/bmaps", settings.datadir);
214 LOG(llevDebug,"Reading bmaps from %s...",buf); 220 LOG(llevDebug,"Reading bmaps from %s...",buf);
215 if ((fp=fopen(buf,"r"))==NULL) { 221 if ((fp=fopen(buf,"r"))==NULL) {
216 LOG(llevError, "Cannot open bmaps file %s: %s\n", buf, strerror_local(errno)); 222 LOG(llevError, "Cannot open bmaps file %s: %s\n", buf, strerror(errno));
217 exit(-1); 223 exit(-1);
218 } 224 }
219 225
220 /* First count how many bitmaps we have, so we can allocate correctly */ 226 /* First count how many bitmaps we have, so we can allocate correctly */
221 while (fgets (buf, MAX_BUF, fp)!=NULL) 227 while (fgets (buf, MAX_BUF, fp)!=NULL)
222 if(buf[0] != '#' && buf[0] != '\n' ) 228 if(buf[0] != '#' && buf[0] != '\n' )
223 nrofbmaps++; 229 nrofbmaps++;
224 rewind(fp); 230 rewind(fp);
225 231
226 xbm = (struct bmappair *) malloc(sizeof(struct bmappair) * nrofbmaps); 232 xbm = new bmappair [nrofbmaps];
227 memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps); 233 memset (xbm, 0, sizeof (struct bmappair) * nrofbmaps);
228 234
229 while(nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) { 235 while(nroffiles < nrofbmaps && fgets (buf, MAX_BUF, fp) != NULL) {
230 if (*buf == '#') 236 if (*buf == '#')
231 continue; 237 continue;
232 238
233 p = (*buf == '\\') ? (buf + 1): buf; 239 p = (*buf == '\\') ? (buf + 1): buf;
234 if (!(p = strtok (p , " \t")) || !(q = strtok (NULL , " \t\n"))) { 240 if (!(p = strtok (p , " \t")) || !(q = strtok (NULL , " \t\n"))) {
235 LOG(llevDebug,"Warning, syntax error: %s\n", buf); 241 LOG(llevDebug,"Warning, syntax error: %s\n", buf);
236 continue; 242 continue;
237 } 243 }
238 value = atoi (p); 244 value = atoi (p);
239 xbm[nroffiles].name = strdup_local(q); 245 xbm[nroffiles].name = strdup_local(q);
240 246
241 /* We need to calculate the checksum of the bmaps file 247 /* We need to calculate the checksum of the bmaps file
242 * name->number mapping to send to the client. This does not 248 * name->number mapping to send to the client. This does not
243 * need to match what sum or other utility may come up with - 249 * 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 250 * 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 251 * data, it does the job as it lets the client know if
246 * the file has the same data or not. 252 * the file has the same data or not.
247 */ 253 */
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); 254 ROTATE_RIGHT(bmaps_checksum);
257 bmaps_checksum += q[l]; 255 bmaps_checksum += value & 0xff;
258 bmaps_checksum &= 0xffffffff; 256 bmaps_checksum &= 0xffffffff;
259 }
260 257
258 ROTATE_RIGHT(bmaps_checksum);
259 bmaps_checksum += (value >> 8) & 0xff;
260 bmaps_checksum &= 0xffffffff;
261 for (l=0; l<strlen(q); l++) {
262 ROTATE_RIGHT(bmaps_checksum);
263 bmaps_checksum += q[l];
264 bmaps_checksum &= 0xffffffff;
265 }
266
261 xbm[nroffiles].number = value; 267 xbm[nroffiles].number = value;
262 nroffiles++; 268 nroffiles++;
263 if(value >= nrofpixmaps) 269 if(value >= nrofpixmaps)
264 nrofpixmaps = value+1; 270 nrofpixmaps = value+1;
265 } 271 }
266 fclose(fp); 272 fclose(fp);
267 273
268 LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles); 274 LOG(llevDebug,"done (got %d/%d/%d)\n",nrofpixmaps,nrofbmaps,nroffiles);
269 275
270 new_faces = (New_Face *)malloc(sizeof(New_Face) * nrofpixmaps); 276 new_faces = new New_Face [nrofpixmaps];
271 for (i = 0; i < nrofpixmaps; i++) { 277 for (i = 0; i < nrofpixmaps; i++) {
272 new_faces[i].name = ""; 278 new_faces[i].name = "";
273 new_faces[i].number = i; 279 new_faces[i].number = i;
274 new_faces[i].visibility=0; 280 new_faces[i].visibility=0;
275 new_faces[i].magicmap=255; 281 new_faces[i].magicmap=255;
276 } 282 }
277 for (i = 0; i < nroffiles; i++) { 283 for (i = 0; i < nroffiles; i++) {
278 new_faces[xbm[i].number].name = xbm[i].name; 284 new_faces[xbm[i].number].name = xbm[i].name;
279 } 285 }
280 286
287 // non-pod datatype, likely not allowed
281 qsort (xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); 288 qsort (xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar);
282 289
283 ReadFaceData(); 290 ReadFaceData();
284 291
285 for (i = 0; i < nrofpixmaps; i++) { 292 for (i = 0; i < nrofpixmaps; i++) {
286 if (new_faces[i].magicmap==255) { 293 if (new_faces[i].magicmap==255) {
287#if 0 /* Useful for initial debugging, not needed now */ 294#if 0 /* Useful for initial debugging, not needed now */
288 LOG(llevDebug,"Face %s still had default magicmap, resetting to black\n", 295 LOG(llevDebug,"Face %s still had default magicmap, resetting to black\n",
289 new_faces[i].name); 296 new_faces[i].name);
290#endif 297#endif
291 new_faces[i].magicmap=0; 298 new_faces[i].magicmap=0;
292 } 299 }
293 } 300 }
294 /* Actually forcefully setting the colors here probably should not 301 /* Actually forcefully setting the colors here probably should not
295 * be done - it could easily create confusion. 302 * be done - it could easily create confusion.
296 */ 303 */
297 blank_face = &new_faces[FindFace(BLANK_FACE_NAME, 0)]; 304 blank_face = &new_faces[FindFace(BLANK_FACE_NAME, 0)];
321 */ 328 */
322int FindFace (const char *name, int error) { 329int FindFace (const char *name, int error) {
323 struct bmappair *bp, tmp; 330 struct bmappair *bp, tmp;
324 char *p; 331 char *p;
325 332
333 if (!name)
334 return error;
335
326 if ((p = strchr (name, '\n'))) 336 if ((p = strchr (name, '\n')))
327 *p = '\0'; 337 *p = '\0';
328 338
329 tmp.name = (char *)name; 339 tmp.name = (char *)name;
330 bp = (struct bmappair *)bsearch 340 bp = (struct bmappair *)bsearch
331 (&tmp, xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar); 341 (&tmp, xbm, nroffiles, sizeof(struct bmappair), (int (*)(const void*, const void*))compar);
332 342
333 return bp ? bp->number : error; 343 return bp ? bp->number : error;
334} 344}
335 345
336/* Reads the smooth file to know how to smooth datas. 346/* Reads the smooth file to know how to smooth datas.
347 357
348 bmaps_checksum=0; 358 bmaps_checksum=0;
349 sprintf (buf,"%s/smooth", settings.datadir); 359 sprintf (buf,"%s/smooth", settings.datadir);
350 LOG(llevDebug,"Reading smooth from %s...",buf); 360 LOG(llevDebug,"Reading smooth from %s...",buf);
351 if ((fp=fopen(buf,"r"))==NULL) { 361 if ((fp=fopen(buf,"r"))==NULL) {
352 LOG(llevError, "Cannot open smooth file %s: %s\n", strerror_local(errno)); 362 LOG(llevError, "Cannot open smooth file %s: %s\n", strerror(errno));
353 exit(-1); 363 exit(-1);
354 } 364 }
355 365
356 /* First count how many smooth we have, so we can allocate correctly */ 366 /* First count how many smooth we have, so we can allocate correctly */
357 while (fgets (buf, MAX_BUF, fp)!=NULL) 367 while (fgets (buf, MAX_BUF, fp)!=NULL)
358 if(buf[0] != '#' && buf[0] != '\n' ) 368 if(buf[0] != '#' && buf[0] != '\n' )
359 smoothcount++; 369 smoothcount++;
360 rewind(fp); 370 rewind(fp);
361 371
362 smooth = (struct smoothing *) malloc(sizeof(struct smoothing) * (smoothcount)); 372 smooth = new smoothing [smoothcount];
363 memset (smooth, 0, sizeof (struct smoothing) * (smoothcount));
364 373
365 while(nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp)!=NULL) { 374 while(nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp)!=NULL) {
366 if (*buf == '#') 375 if (*buf == '#')
367 continue; 376 continue;
368 p=strchr(buf,' '); 377 p=strchr(buf,' ');
409void free_all_images(void) 418void free_all_images(void)
410{ 419{
411 int i; 420 int i;
412 421
413 for (i=0; i<nroffiles; i++) 422 for (i=0; i<nroffiles; i++)
414 free(xbm[i].name); 423 free(xbm[i].name);
415 free(xbm); 424
416 free(new_faces); 425 delete [] xbm;
417 free(smooth); 426 delete [] new_faces;
427 delete [] smooth;
418} 428}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines