Atrinik Server 2.5
server/image.c
Go to the documentation of this file.
00001 /************************************************************************
00002 *            Atrinik, a Multiplayer Online Role Playing Game            *
00003 *                                                                       *
00004 *    Copyright (C) 2009-2011 Alex Tokar and Atrinik Development Team    *
00005 *                                                                       *
00006 * Fork from Daimonin (Massive Multiplayer Online Role Playing Game)     *
00007 * and Crossfire (Multiplayer game for X-windows).                       *
00008 *                                                                       *
00009 * This program is free software; you can redistribute it and/or modify  *
00010 * it under the terms of the GNU General Public License as published by  *
00011 * the Free Software Foundation; either version 2 of the License, or     *
00012 * (at your option) any later version.                                   *
00013 *                                                                       *
00014 * This program is distributed in the hope that it will be useful,       *
00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017 * GNU General Public License for more details.                          *
00018 *                                                                       *
00019 * You should have received a copy of the GNU General Public License     *
00020 * along with this program; if not, write to the Free Software           *
00021 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
00022 *                                                                       *
00023 * The author can be reached at admin@atrinik.org                        *
00024 ************************************************************************/
00025 
00030 #include <global.h>
00031 
00032 New_Face *new_faces;
00033 
00046 struct bmappair
00047 {
00048     char *name;
00049 
00050     unsigned int number;
00051 };
00052 
00056 static struct bmappair *xbm = NULL;
00057 
00061 New_Face *blank_face, *next_item_face, *prev_item_face;
00062 
00064 int nroffiles = 0;
00065 
00069 int nrofpixmaps = 0;
00070 
00073 static int compar(struct bmappair *a, struct bmappair *b)
00074 {
00075     return strcmp(a->name, b->name);
00076 }
00077 
00082 int read_bmap_names()
00083 {
00084     char buf[MAX_BUF], *cp;
00085     FILE *fp;
00086     int nrofbmaps = 0, i;
00087     size_t line = 0;
00088 
00089     snprintf(buf, sizeof(buf), "%s/bmaps", settings.datadir);
00090     LOG(llevDebug, "Reading bmaps from %s...", buf);
00091 
00092     if ((fp = fopen(buf, "r")) == NULL)
00093     {
00094         LOG(llevError, "Can't open bmaps file: %s\n", buf);
00095     }
00096 
00097     /* First count how many bitmaps we have, so we can allocate correctly */
00098     while (fgets(buf, sizeof(buf) - 1, fp))
00099     {
00100         if (buf[0] != '#' && buf[0] != '\n')
00101         {
00102             nrofbmaps++;
00103         }
00104     }
00105 
00106     rewind(fp);
00107 
00108     xbm = (struct bmappair *) malloc(sizeof(struct bmappair) * (nrofbmaps + 1));
00109     memset(xbm, 0, sizeof(struct bmappair) * (nrofbmaps + 1));
00110 
00111     while (fgets(buf, sizeof(buf) - 1, fp))
00112     {
00113         if (*buf == '#')
00114         {
00115             continue;
00116         }
00117 
00118         cp = strchr(buf, '\n');
00119 
00120         if (cp)
00121         {
00122             *cp = '\0';
00123         }
00124 
00125         cp = strchr(buf, ' ');
00126 
00127         if (cp)
00128         {
00129             cp++;
00130             xbm[nroffiles].name = strdup_local(cp);
00131         }
00132         else
00133         {
00134             xbm[nroffiles].name = strdup_local(buf);
00135         }
00136 
00137         xbm[nroffiles].number = line;
00138 
00139         nroffiles++;
00140 
00141         if ((int) line > nrofpixmaps)
00142         {
00143             nrofpixmaps++;
00144         }
00145 
00146         line++;
00147     }
00148 
00149     fclose(fp);
00150 
00151     LOG(llevDebug, " done (got %d/%d/%d)\n", nrofpixmaps, nrofbmaps, nroffiles);
00152 
00153     new_faces = (New_Face *) malloc(sizeof(New_Face) * (nrofpixmaps + 1));
00154 
00155     for (i = 0; i <= nrofpixmaps; i++)
00156     {
00157         new_faces[i].name = "";
00158         new_faces[i].number = i;
00159     }
00160 
00161     for (i = 0; i < nroffiles; i++)
00162     {
00163         new_faces[xbm[i].number].name = xbm[i].name;
00164     }
00165 
00166     nrofpixmaps++;
00167 
00168     qsort(xbm, nrofbmaps, sizeof(struct bmappair), (void *) (int (*)())compar);
00169 
00170     blank_face = &new_faces[find_face(BLANK_FACE_NAME, 0)];
00171     next_item_face = &new_faces[find_face(NEXT_ITEM_FACE_NAME, 0)];
00172     prev_item_face = &new_faces[find_face(PREVIOUS_ITEM_FACE_NAME, 0)];
00173 
00174     return nrofpixmaps;
00175 }
00176 
00184 int find_face(char *name, int error)
00185 {
00186     int i;
00187     struct bmappair *bp, tmp;
00188     char *p;
00189 
00190     /* Using actual numbers for faces is a very bad idea.  This is because
00191      * each time the archetype file is rebuilt, all the face numbers
00192      * change. */
00193     if ((i = atoi(name)))
00194     {
00195         LOG(llevBug, "Integer face name used: %s\n", name);
00196         return i;
00197     }
00198 
00199     if ((p = strchr(name, '\n')))
00200     {
00201         *p = '\0';
00202     }
00203 
00204     tmp.name = name;
00205     bp = (struct bmappair *) bsearch(&tmp, xbm, nroffiles, sizeof(struct bmappair), (void *) (int (*)()) compar);
00206 
00207     return bp ? bp->number : (unsigned int) error;
00208 }
00209 
00211 void free_all_images()
00212 {
00213     int i;
00214 
00215     for (i = 0; i < nroffiles; i++)
00216     {
00217         free(xbm[i].name);
00218     }
00219 
00220     free(xbm);
00221     free(new_faces);
00222 }