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.19 by root, Sun Mar 11 02:12:44 2007 UTC vs.
Revision 1.23 by root, Thu Apr 12 14:18:04 2007 UTC

20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * 21 *
22 * The maintainer of this code can be reached at <crossfire@schmorp.de> 22 * The maintainer of this code can be reached at <crossfire@schmorp.de>
23 */ 23 */
24 24
25
26#include <global.h> 25#include <global.h>
27#include <stdio.h> 26#include <stdio.h>
28 27
29#include "face.h" 28#include "face.h"
30#include "crc.h" 29#include "crc.h"
31 30
32faceidx blank_face, empty_face; 31faceidx blank_face, empty_face;
33 32
34facehash_t facehash; 33facehash_t facehash;
35std::vector<faceinfo> faces; 34std::vector<faceinfo> faces;
36std::vector<facedata> face32, face64;
37
38/**
39 * id is the face to smooth, smooth is the 16x2 face used to smooth id.
40 */
41struct smoothing : zero_initialised
42{
43 uint16 id;
44 uint16 smooth;
45};
46
47/**
48 * Contains all defined smoothing entries. smooth is an array of nrofsmooth
49 * entries. It is sorted by smooth[].id.
50 */
51static struct smoothing *smooth = NULL;
52int nrofsmooth = 0;
53 35
54/* the only thing this table is used for now is to 36/* the only thing this table is used for now is to
55 * translate the colorname in the magicmap field of the 37 * translate the colorname in the magicmap field of the
56 * face into a numeric index that is then sent to the 38 * face into a numeric index that is then sent to the
57 * client for magic map commands. The order of this table 39 * client for magic map commands. The order of this table
71 "brown", /* 10 */ 53 "brown", /* 10 */
72 "yellow", /* 11 */ 54 "yellow", /* 11 */
73 "khaki" /* 12 */ 55 "khaki" /* 12 */
74}; 56};
75 57
76static int
77compar_smooth (const struct smoothing *a, const struct smoothing *b)
78{
79 if (a->id < b->id)
80 return -1;
81 if (b->id < a->id)
82 return 1;
83 return 0;
84}
85
86/* 58/*
87 * Returns the matching color in the coloralias if found, 59 * Returns the matching color in the coloralias if found,
88 * 0 otherwise. Note that 0 will actually be black, so there is no 60 * 0 otherwise. Note that 0 will actually be black, so there is no
89 * way the calling function can tell if an error occurred or not 61 * way the calling function can tell if an error occurred or not
90 */ 62 */
99 71
100 LOG (llevError, "Unknown color: %s\n", name); 72 LOG (llevError, "Unknown color: %s\n", name);
101 return 0; 73 return 0;
102} 74}
103 75
104int 76faceidx
105face_find (const char *name, int defidx) 77face_find (const char *name, faceidx defidx)
106{ 78{
79 if (!name)
80 return defidx;
81
107 facehash_t::iterator i = facehash.find (name); 82 facehash_t::iterator i = facehash.find (name);
108 83
109 return i == facehash.end () 84 return i == facehash.end ()
110 ? defidx : i->second; 85 ? defidx : i->second;
111} 86}
112 87
113facedata * 88faceinfo *
114face_data (int idx, int faceset) 89face_info (faceidx idx)
115{ 90{
116 return &(faceset ? face64 : face32)[idx]; 91 assert (0 < (faceidx)-1); // faceidx must be unsigned
92
93 if (idx >= faces.size ())
94 return 0;
95
96 return &faces [idx];
117} 97}
118 98
119/* Reads the smooth file to know how to smooth datas. 99facedata *
120 * the smooth file if made of 2 elements lines. 100face_data (faceidx idx, int faceset)
121 * lines starting with # are comment
122 * the first element of line is face to smooth
123 * the next element is the 16x2 faces picture
124 * used for smoothing
125 */
126int
127ReadSmooth (void)
128{ 101{
129 char buf[MAX_BUF], *p, *q; 102 if (faceinfo *f = face_info (idx))
130 FILE *fp; 103 return &(faceset ? f->data32 : f->data64);
131 int smoothcount = 0;
132 104
133 sprintf (buf, "%s/smooth", settings.datadir); 105 return 0;
134 LOG (llevDebug, "Reading smooth from %s...\n", buf);
135 if ((fp = fopen (buf, "r")) == NULL)
136 {
137 LOG (llevError, "Cannot open smooth file %s: %s\n", strerror (errno));
138 exit (-1);
139 }
140
141 /* First count how many smooth we have, so we can allocate correctly */
142 while (fgets (buf, MAX_BUF, fp) != NULL)
143 if (buf[0] != '#' && buf[0] != '\n')
144 smoothcount++;
145 rewind (fp);
146
147 smooth = new smoothing[smoothcount];
148
149 while (nrofsmooth < smoothcount && fgets (buf, MAX_BUF, fp) != NULL)
150 {
151 if (*buf == '#')
152 continue;
153 p = strchr (buf, ' ');
154 if (!p)
155 continue;
156 *p = '\0';
157 q = buf;
158 smooth[nrofsmooth].id = face_find (q);
159 q = p + 1;
160 smooth[nrofsmooth].smooth = face_find (q);
161 nrofsmooth++;
162 }
163 fclose (fp);
164
165 LOG (llevDebug, "done (got %d smooth entries)\n", nrofsmooth);
166 qsort (smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth);
167 return nrofsmooth;
168} 106}
169 107
170/**
171 * Find the smooth face for a given face.
172 *
173 * @param face the face to find the smoothing face for
174 *
175 * @param smoothed return value: set to smooth face
176 *
177 * @return 1=smooth face found, 0=no smooth face found
178 */
179int
180FindSmooth (uint16 face, uint16 * smoothed)
181{
182 struct smoothing *bp, tmp;
183
184 tmp.id = face;
185 bp = (struct smoothing *) bsearch
186 (&tmp, smooth, nrofsmooth, sizeof (struct smoothing), (int (*)(const void *, const void *)) compar_smooth);
187 (*smoothed) = 0;
188 if (bp)
189 (*smoothed) = bp->smooth;
190 return bp ? 1 : 0;
191}
192

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines