| 1 |
root |
1.1 |
/* |
| 2 |
|
|
* static char *rcsid_xutil_c = |
| 3 |
pippijn |
1.2 |
* "$Id$"; |
| 4 |
root |
1.1 |
*/ |
| 5 |
|
|
|
| 6 |
|
|
/* |
| 7 |
|
|
CrossFire, A Multiplayer game for X-windows |
| 8 |
|
|
|
| 9 |
|
|
Copyright (C) 2002 Mark Wedel & Crossfire Development Team |
| 10 |
|
|
Copyright (C) 1992 Frank Tore Johansen |
| 11 |
|
|
|
| 12 |
|
|
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 |
| 14 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 15 |
|
|
(at your option) any later version. |
| 16 |
|
|
|
| 17 |
|
|
This program is distributed in the hope that it will be useful, |
| 18 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
|
|
GNU General Public License for more details. |
| 21 |
|
|
|
| 22 |
|
|
You should have received a copy of the GNU General Public License |
| 23 |
|
|
along with this program; if not, write to the Free Software |
| 24 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 |
|
|
|
| 26 |
|
|
The authors can be reached via e-mail at crossfire-devel@real-time.com |
| 27 |
|
|
*/ |
| 28 |
|
|
|
| 29 |
|
|
|
| 30 |
|
|
#include <autoconf.h> |
| 31 |
|
|
/* Stupid pngconf.h file has a check to see if _SETJMP_H is |
| 32 |
|
|
* already defined, and if so, it generates a compilation error. |
| 33 |
|
|
* I have no idea what they were thinking - wasn't the point of those |
| 34 |
|
|
* defines so that you didn't need to worry about order or including |
| 35 |
|
|
* the same file multiple times? |
| 36 |
|
|
*/ |
| 37 |
|
|
#ifdef HAVE_LIBPNG |
| 38 |
|
|
#include "png.c" |
| 39 |
|
|
#endif |
| 40 |
|
|
|
| 41 |
|
|
#include <Defines.h> |
| 42 |
|
|
#include <funcpoint.h> |
| 43 |
|
|
#include <loader.h> |
| 44 |
|
|
|
| 45 |
|
|
#define IMAGE_BUF 65536 |
| 46 |
|
|
|
| 47 |
|
|
/* |
| 48 |
|
|
* ReadPixmaps(): When color pixmaps are used instead of fonts, this function |
| 49 |
|
|
* does the actual reading of pixmap-file. This function is based largely on |
| 50 |
|
|
* the ReadBitmaps function. |
| 51 |
|
|
*/ |
| 52 |
|
|
|
| 53 |
|
|
/* New method: Pixmaps are stored as a montage on the disk (in several |
| 54 |
|
|
* files). This way, we only need to call XCreatePixmap... a couple |
| 55 |
|
|
* times, and the rest are XCopyArea. This is much faster, since |
| 56 |
|
|
* the XPM library does not seem to be especially efficient about loading |
| 57 |
|
|
* large numbers of pixmaps. |
| 58 |
|
|
* |
| 59 |
|
|
* Return true if we have gone to a private colormap. |
| 60 |
|
|
* |
| 61 |
|
|
* type is type of images to load. If Dm_Bitmaps, cmap, and masks |
| 62 |
|
|
* can be null. |
| 63 |
|
|
*/ |
| 64 |
|
|
|
| 65 |
|
|
/* Useful when trying to optimize load time some */ |
| 66 |
|
|
#define IMAGE_TIME_LOAD |
| 67 |
|
|
|
| 68 |
|
|
int ReadImages(Display *gdisp, Pixmap **pixmaps, Pixmap **masks, |
| 69 |
|
|
Colormap *cmap, enum DisplayMode type) { |
| 70 |
|
|
|
| 71 |
|
|
Window root = RootWindow (gdisp,DefaultScreen(gdisp)); |
| 72 |
|
|
int use_private_cmap=0,num, compressed, len,i; |
| 73 |
|
|
FILE *infile; |
| 74 |
|
|
char *cp, databuf[IMAGE_BUF], filename[MAX_BUF]; |
| 75 |
|
|
#ifdef IMAGE_TIME_LOAD |
| 76 |
|
|
time_t start_time = time(NULL); |
| 77 |
|
|
#endif |
| 78 |
|
|
|
| 79 |
|
|
/* This function is called before the game gc's are created. So |
| 80 |
|
|
* we create one for our own use here. |
| 81 |
|
|
*/ |
| 82 |
|
|
GC gc= XCreateGC(gdisp, root, 0, NULL); |
| 83 |
|
|
|
| 84 |
|
|
if (!nrofpixmaps) |
| 85 |
|
|
ReadBmapNames (); |
| 86 |
|
|
|
| 87 |
|
|
/* The processes that create a pixmap already allocates the space for |
| 88 |
|
|
* the pixmap data. Therefor, only space for the pointers to that data |
| 89 |
|
|
* needs to be allocated. The same might apply for the function |
| 90 |
|
|
* that creates bitmaps below, but I am not as sure in that case. |
| 91 |
|
|
* Mark Wedel |
| 92 |
|
|
*/ |
| 93 |
|
|
|
| 94 |
|
|
*pixmaps = (Pixmap *) malloc(sizeof(Pixmap *) * nrofpixmaps); |
| 95 |
|
|
if (type==Dm_Png) |
| 96 |
|
|
*masks = (Pixmap *) malloc(sizeof(Pixmap *) * nrofpixmaps); |
| 97 |
|
|
|
| 98 |
|
|
for (i=0; i < nrofpixmaps; i++) |
| 99 |
|
|
(*pixmaps)[i] = 0; |
| 100 |
|
|
|
| 101 |
|
|
LOG(llevDebug,"Building images..."); |
| 102 |
|
|
|
| 103 |
|
|
if (type==Dm_Png) { |
| 104 |
|
|
sprintf(filename,"%s/crossfire.0",settings.datadir); |
| 105 |
|
|
#ifdef HAVE_LIBPNG |
| 106 |
|
|
init_pngx_loader(gdisp); |
| 107 |
|
|
#endif |
| 108 |
|
|
} |
| 109 |
|
|
|
| 110 |
|
|
if ((infile = open_and_uncompress(filename,0,&compressed))==NULL) { |
| 111 |
|
|
LOG(llevError,"Unable to open %s\n", filename); |
| 112 |
|
|
abort(); |
| 113 |
|
|
} |
| 114 |
|
|
i=0; |
| 115 |
|
|
while (fgets(databuf,MAX_BUF,infile)) { |
| 116 |
|
|
|
| 117 |
|
|
/* First, verify that that image header line is OK */ |
| 118 |
|
|
if(strncmp(databuf,"IMAGE ",6)!=0) { |
| 119 |
|
|
LOG(llevError,"ReadImages:Bad image line - not IMAGE, instead\n%s",databuf); |
| 120 |
|
|
abort(); |
| 121 |
|
|
} |
| 122 |
|
|
num = atoi(databuf+6); |
| 123 |
|
|
if (num<0 || num > nrofpixmaps) { |
| 124 |
|
|
LOG(llevError,"Pixmap number less than zero: %d, %s\n",num, databuf); |
| 125 |
|
|
abort(); |
| 126 |
|
|
} |
| 127 |
|
|
/* Skip accross the number data */ |
| 128 |
|
|
for (cp=databuf+6; *cp!=' '; cp++) ; |
| 129 |
|
|
len = atoi(cp); |
| 130 |
|
|
if (len==0 || len>IMAGE_BUF) { |
| 131 |
|
|
LOG(llevError,"ReadImages: length not valid: %d\n%s", |
| 132 |
|
|
len,databuf); |
| 133 |
|
|
abort(); |
| 134 |
|
|
} |
| 135 |
|
|
if (fread(databuf, 1, len, infile)!=len) { |
| 136 |
|
|
LOG(llevError,"read_client_images: Did not read desired amount of data, wanted %d\n%s", |
| 137 |
|
|
len, databuf); |
| 138 |
|
|
abort(); |
| 139 |
|
|
} |
| 140 |
|
|
i++; |
| 141 |
|
|
if (!(i % 100)) LOG(llevDebug,"."); |
| 142 |
|
|
|
| 143 |
|
|
if (type==Dm_Png) { |
| 144 |
|
|
#ifdef HAVE_LIBPNG |
| 145 |
|
|
unsigned long x,y; |
| 146 |
|
|
if (png_to_xpixmap(gdisp, root, databuf, len, |
| 147 |
|
|
&(*pixmaps)[num], &(*masks)[num], cmap, &x, &y)) { |
| 148 |
|
|
|
| 149 |
|
|
LOG(llevError,"Error loading png file.\n"); |
| 150 |
|
|
} |
| 151 |
|
|
#endif |
| 152 |
|
|
} |
| 153 |
|
|
} |
| 154 |
|
|
close_and_delete(infile, compressed); |
| 155 |
|
|
XFreeGC(gdisp, gc); |
| 156 |
|
|
|
| 157 |
|
|
/* Check for any holes. There should not be. This is an interim check |
| 158 |
|
|
* to use until the xbm_values is removed. The program that |
| 159 |
|
|
* creates the bitmap/pixmap files just adds the bitmaps, so holes should |
| 160 |
|
|
* not be in the image file. |
| 161 |
|
|
*/ |
| 162 |
|
|
for (i = 0; i < nrofpixmaps; i++) |
| 163 |
|
|
if ((*pixmaps)[i] == 0) { |
| 164 |
|
|
LOG(llevDebug, "Warning, pixmap %d is not defined, setting it to blank\n", i); |
| 165 |
|
|
(*pixmaps)[i] = (*pixmaps)[blank_face->number]; |
| 166 |
|
|
} |
| 167 |
|
|
LOG(llevDebug,"done.\n"); |
| 168 |
|
|
#ifdef IMAGE_TIME_LOAD |
| 169 |
|
|
fprintf(stderr,"Creation of images took %ld seconds\n", time(NULL) - start_time); |
| 170 |
|
|
#endif |
| 171 |
|
|
return use_private_cmap; |
| 172 |
|
|
} |
| 173 |
|
|
|
| 174 |
|
|
/* This frees all the pixmaps. This not only makes for better code, |
| 175 |
|
|
* some XServers may not free the memory used by the pixmaps unless it is done |
| 176 |
|
|
* this way. For color pixmaps, this will be called twice - the |
| 177 |
|
|
* first time, the normal pixmaps will be passed through, the second |
| 178 |
|
|
* time, it will be called with the masks as the pixmaps argument. |
| 179 |
|
|
*/ |
| 180 |
|
|
|
| 181 |
|
|
void free_pixmaps(Display *gdisp, Pixmap *pixmaps) |
| 182 |
|
|
{ |
| 183 |
|
|
int i; |
| 184 |
|
|
|
| 185 |
|
|
for (i=0; i < nrofpixmaps; i++) { |
| 186 |
|
|
if (pixmaps[i]!=0) { |
| 187 |
|
|
XFreePixmap(gdisp, pixmaps[i]); |
| 188 |
|
|
pixmaps[i] = 0; |
| 189 |
|
|
} |
| 190 |
|
|
} |
| 191 |
|
|
} |
| 192 |
|
|
|