1 |
/* |
2 |
* static char *rcsid_xutil_c = |
3 |
* "$Id: xutil.c,v 1.13 2005/10/24 20:48:15 akirschbaum Exp $"; |
4 |
*/ |
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 |
/* |
46 |
* Converts between Fontindex and XChar2b types. |
47 |
* Used in global.h, for draw_face (in face, FontindextoXChars |
48 |
* is defined to this function name |
49 |
*/ |
50 |
|
51 |
XChar2b fontindex_to_XChar2b(Fontindex s) |
52 |
{ |
53 |
XChar2b c; |
54 |
|
55 |
c.byte1 = s/256; |
56 |
c.byte2 = s%256; |
57 |
|
58 |
return c; |
59 |
} |
60 |
|
61 |
|
62 |
#define IMAGE_BUF 65536 |
63 |
|
64 |
/* |
65 |
* ReadPixmaps(): When color pixmaps are used instead of fonts, this function |
66 |
* does the actual reading of pixmap-file. This function is based largely on |
67 |
* the ReadBitmaps function. |
68 |
*/ |
69 |
|
70 |
/* New method: Pixmaps are stored as a montage on the disk (in several |
71 |
* files). This way, we only need to call XCreatePixmap... a couple |
72 |
* times, and the rest are XCopyArea. This is much faster, since |
73 |
* the XPM library does not seem to be especially efficient about loading |
74 |
* large numbers of pixmaps. |
75 |
* |
76 |
* Return true if we have gone to a private colormap. |
77 |
* |
78 |
* type is type of images to load. If Dm_Bitmaps, cmap, and masks |
79 |
* can be null. |
80 |
*/ |
81 |
|
82 |
/* Useful when trying to optimize load time some */ |
83 |
#define IMAGE_TIME_LOAD |
84 |
|
85 |
int ReadImages(Display *gdisp, Pixmap **pixmaps, Pixmap **masks, |
86 |
Colormap *cmap, enum DisplayMode type) { |
87 |
|
88 |
Window root = RootWindow (gdisp,DefaultScreen(gdisp)); |
89 |
int use_private_cmap=0,num, compressed, len,i; |
90 |
FILE *infile; |
91 |
char *cp, databuf[IMAGE_BUF], filename[MAX_BUF]; |
92 |
#ifdef IMAGE_TIME_LOAD |
93 |
time_t start_time = time(NULL); |
94 |
#endif |
95 |
|
96 |
/* This function is called before the game gc's are created. So |
97 |
* we create one for our own use here. |
98 |
*/ |
99 |
GC gc= XCreateGC(gdisp, root, 0, NULL); |
100 |
|
101 |
if (!nrofpixmaps) |
102 |
ReadBmapNames (); |
103 |
|
104 |
/* The processes that create a pixmap already allocates the space for |
105 |
* the pixmap data. Therefor, only space for the pointers to that data |
106 |
* needs to be allocated. The same might apply for the function |
107 |
* that creates bitmaps below, but I am not as sure in that case. |
108 |
* Mark Wedel |
109 |
*/ |
110 |
|
111 |
*pixmaps = (Pixmap *) malloc(sizeof(Pixmap *) * nrofpixmaps); |
112 |
if (type==Dm_Png) |
113 |
*masks = (Pixmap *) malloc(sizeof(Pixmap *) * nrofpixmaps); |
114 |
|
115 |
for (i=0; i < nrofpixmaps; i++) |
116 |
(*pixmaps)[i] = 0; |
117 |
|
118 |
LOG(llevDebug,"Building images..."); |
119 |
|
120 |
if (type==Dm_Png) { |
121 |
sprintf(filename,"%s/crossfire.0",settings.datadir); |
122 |
#ifdef HAVE_LIBPNG |
123 |
init_pngx_loader(gdisp); |
124 |
#endif |
125 |
} |
126 |
|
127 |
if ((infile = open_and_uncompress(filename,0,&compressed))==NULL) { |
128 |
LOG(llevError,"Unable to open %s\n", filename); |
129 |
abort(); |
130 |
} |
131 |
i=0; |
132 |
while (fgets(databuf,MAX_BUF,infile)) { |
133 |
|
134 |
/* First, verify that that image header line is OK */ |
135 |
if(strncmp(databuf,"IMAGE ",6)!=0) { |
136 |
LOG(llevError,"ReadImages:Bad image line - not IMAGE, instead\n%s",databuf); |
137 |
abort(); |
138 |
} |
139 |
num = atoi(databuf+6); |
140 |
if (num<0 || num > nrofpixmaps) { |
141 |
LOG(llevError,"Pixmap number less than zero: %d, %s\n",num, databuf); |
142 |
abort(); |
143 |
} |
144 |
/* Skip accross the number data */ |
145 |
for (cp=databuf+6; *cp!=' '; cp++) ; |
146 |
len = atoi(cp); |
147 |
if (len==0 || len>IMAGE_BUF) { |
148 |
LOG(llevError,"ReadImages: length not valid: %d\n%s", |
149 |
len,databuf); |
150 |
abort(); |
151 |
} |
152 |
if (fread(databuf, 1, len, infile)!=len) { |
153 |
LOG(llevError,"read_client_images: Did not read desired amount of data, wanted %d\n%s", |
154 |
len, databuf); |
155 |
abort(); |
156 |
} |
157 |
i++; |
158 |
if (!(i % 100)) LOG(llevDebug,"."); |
159 |
|
160 |
if (type==Dm_Png) { |
161 |
#ifdef HAVE_LIBPNG |
162 |
unsigned long x,y; |
163 |
if (png_to_xpixmap(gdisp, root, databuf, len, |
164 |
&(*pixmaps)[num], &(*masks)[num], cmap, &x, &y)) { |
165 |
|
166 |
LOG(llevError,"Error loading png file.\n"); |
167 |
} |
168 |
#endif |
169 |
} |
170 |
} |
171 |
close_and_delete(infile, compressed); |
172 |
XFreeGC(gdisp, gc); |
173 |
|
174 |
/* Check for any holes. There should not be. This is an interim check |
175 |
* to use until the xbm_values is removed. The program that |
176 |
* creates the bitmap/pixmap files just adds the bitmaps, so holes should |
177 |
* not be in the image file. |
178 |
*/ |
179 |
for (i = 0; i < nrofpixmaps; i++) |
180 |
if ((*pixmaps)[i] == 0) { |
181 |
LOG(llevDebug, "Warning, pixmap %d is not defined, setting it to blank\n", i); |
182 |
(*pixmaps)[i] = (*pixmaps)[blank_face->number]; |
183 |
} |
184 |
LOG(llevDebug,"done.\n"); |
185 |
#ifdef IMAGE_TIME_LOAD |
186 |
fprintf(stderr,"Creation of images took %ld seconds\n", time(NULL) - start_time); |
187 |
#endif |
188 |
return use_private_cmap; |
189 |
} |
190 |
|
191 |
/* This frees all the pixmaps. This not only makes for better code, |
192 |
* some XServers may not free the memory used by the pixmaps unless it is done |
193 |
* this way. For color pixmaps, this will be called twice - the |
194 |
* first time, the normal pixmaps will be passed through, the second |
195 |
* time, it will be called with the masks as the pixmaps argument. |
196 |
*/ |
197 |
|
198 |
void free_pixmaps(Display *gdisp, Pixmap *pixmaps) |
199 |
{ |
200 |
int i; |
201 |
|
202 |
for (i=0; i < nrofpixmaps; i++) { |
203 |
if (pixmaps[i]!=0) { |
204 |
XFreePixmap(gdisp, pixmaps[i]); |
205 |
pixmaps[i] = 0; |
206 |
} |
207 |
} |
208 |
} |
209 |
|