Atrinik Server  4.0
image.c
Go to the documentation of this file.
1 /*************************************************************************
2  * Atrinik, a Multiplayer Online Role Playing Game *
3  * *
4  * Copyright (C) 2009-2014 Alex Tokar and Atrinik Development Team *
5  * *
6  * Fork from Crossfire (Multiplayer game for X-windows). *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the Free Software *
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
21  * *
22  * The author can be reached at admin@atrinik.org *
23  ************************************************************************/
24 
30 #include <global.h>
31 #include <toolkit/string.h>
32 
33 New_Face *new_faces;
34 
48 struct bmappair {
49  char *name;
50 
51  unsigned int number;
52 } ;
53 
58 static struct bmappair *xbm = NULL;
59 
61 int nroffiles = 0;
62 
67 int nrofpixmaps = 0;
68 
72 static int compar(const void *a, const void *b)
73 {
74  return strcmp(((const struct bmappair *) a)->name, ((const struct bmappair *) b)->name);
75 }
76 
82 int read_bmap_names(void)
83 {
84  char buf[MAX_BUF], *cp;
85  FILE *fp;
86  int nrofbmaps = 0, i;
87  size_t line = 0;
88 
89  snprintf(buf, sizeof(buf), "%s/bmaps", settings.libpath);
90 
91  if ((fp = fopen(buf, "r")) == NULL) {
92  LOG(ERROR, "Can't open bmaps file: %s", buf);
93  exit(1);
94  }
95 
96  /* First count how many bitmaps we have, so we can allocate correctly */
97  while (fgets(buf, sizeof(buf) - 1, fp)) {
98  if (buf[0] != '#' && buf[0] != '\n') {
99  nrofbmaps++;
100  }
101  }
102 
103  rewind(fp);
104 
105  nrofpixmaps = 0;
106  nroffiles = 0;
107 
108  xbm = emalloc(sizeof(struct bmappair) * (nrofbmaps + 1));
109  memset(xbm, 0, sizeof(struct bmappair) * (nrofbmaps + 1));
110 
111  while (fgets(buf, sizeof(buf) - 1, fp)) {
112  if (*buf == '#') {
113  continue;
114  }
115 
116  cp = strchr(buf, '\n');
117 
118  if (cp) {
119  *cp = '\0';
120  }
121 
122  cp = strchr(buf, ' ');
123 
124  if (cp) {
125  cp++;
126  xbm[nroffiles].name = estrdup(cp);
127  } else {
128  xbm[nroffiles].name = estrdup(buf);
129  }
130 
131  xbm[nroffiles].number = line;
132 
133  nroffiles++;
134 
135  if ((int) line > nrofpixmaps) {
136  nrofpixmaps++;
137  }
138 
139  line++;
140  }
141 
142  fclose(fp);
143 
144  new_faces = emalloc(sizeof(New_Face) * (nrofpixmaps + 1));
145 
146  for (i = 0; i < nrofpixmaps + 1; i++) {
147  new_faces[i].name = "";
148  new_faces[i].number = i;
149  }
150 
151  for (i = 0; i < nroffiles; i++) {
152  new_faces[xbm[i].number].name = xbm[i].name;
153  }
154 
155  nrofpixmaps++;
156 
157  qsort(xbm, nrofbmaps, sizeof(struct bmappair), compar);
158 
159  return nrofpixmaps;
160 }
161 
172 int find_face(const char *name, int error)
173 {
174  struct bmappair *bp, tmp;
175 
176  tmp.name = (char *) name;
177  bp = bsearch(&tmp, xbm, nroffiles, sizeof(struct bmappair), compar);
178 
179  return bp ? bp->number : (unsigned int) error;
180 }
181 
183 void free_all_images(void)
184 {
185  int i;
186 
187  for (i = 0; i < nroffiles; i++) {
188  efree(xbm[i].name);
189  }
190 
191  efree(xbm);
192  efree(new_faces);
193 }
int find_face(const char *name, int error)
Definition: image.c:172
static int compar(const void *a, const void *b)
Definition: image.c:72
static struct bmappair * xbm
Definition: image.c:58
int nroffiles
Definition: image.c:61
char libpath[MAX_BUF]
Definition: global.h:338
uint16_t number
Definition: face.h:46
const char * name
Definition: face.h:40
int read_bmap_names(void)
Definition: image.c:82
struct settings_struct settings
Definition: init.c:55
void free_all_images(void)
Definition: image.c:183
Definition: image.c:48
int nrofpixmaps
Definition: image.c:67