2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
25 byte *byteimage, *lbmpalette;
\r
26 int byteimagewidth, byteimageheight;
\r
28 qboolean TrueColorImage;
\r
29 int longimagewidth, longimageheight;
\r
31 char book_prefix[1024];
\r
32 byte buffer[640 * 480];
\r
33 unsigned long bufferl[640 * 480];
\r
35 miptex_t *CreateBook8(byte *buffer, int w, int h, byte *palette, int *FinalSize)
\r
42 size = sizeof(*mp) + (w * h);
\r
43 mp = (miptex_t *)SafeMalloc(size, "CreateBook8");
\r
44 memset(mp, 0, size);
\r
46 mp->version = MIP_VERSION;
\r
48 for(i=j=0;i<256;i++,j+=3)
\r
50 mp->palette[i].r = palette[j];
\r
51 mp->palette[i].g = palette[j+1];
\r
52 mp->palette[i].b = palette[j+2];
\r
54 pos = (byte *)(mp + 1);
\r
58 mp->offsets[0] = sizeof(*mp);
\r
59 memcpy(pos, buffer, w * h);
\r
65 miptex32_t *CreateBook32(long *buffer, int w, int h, int *FinalSize)
\r
71 size = sizeof(*mp) + (w * h * 4);
\r
72 mp = (miptex32_t *)SafeMalloc(size, "CreateBook32");
\r
73 memset(mp, 0, size);
\r
75 mp->version = MIP32_VERSION;
\r
77 pos = (byte *)(mp + 1);
\r
81 mp->offsets[0] = sizeof(*mp);
\r
82 memcpy(pos, buffer, w * h * 4);
\r
89 // Routines to chop a random sized image into gl texture friendly chunks
\r
91 typedef struct rect_s
\r
98 int GetCoords(int x, int store[MAX_MD2SKINS])
\r
100 int index, start, delta;
\r
106 store[index++] = start;
\r
112 store[index++] = start;
\r
123 int ChopImage(int w, int h, rect_t coords[MAX_MD2SKINS])
\r
125 int xs[MAX_MD2SKINS], ys[MAX_MD2SKINS];
\r
126 int xcount, ycount, x, y, index;
\r
129 xcount = GetCoords(w, xs) - 1;
\r
130 ycount = GetCoords(h, ys) - 1;
\r
132 for(y = 0; y < ycount; y++)
\r
134 for(x = 0; x < xcount; x++, index++)
\r
136 coords[index].x = xs[x];
\r
137 coords[index].y = ys[y];
\r
138 coords[index].w = xs[x + 1] - xs[x];
\r
139 coords[index].h = ys[y + 1] - ys[y];
\r
140 coords[index].name[0] = x + '0';
\r
141 coords[index].name[1] = y + '0';
\r
142 coords[index].name[2] = 0;
\r
156 int xl,yl,xh,yh,w,h;
\r
157 byte *dest, *source;
\r
158 int flags, value, contents;
\r
160 char filename[1024];
\r
161 unsigned long *destl, *sourcel;
\r
162 int linedelta, x, y;
\r
165 miptex32_t *qtex32;
\r
166 float scale_x, scale_y;
\r
168 rect_t coords[MAX_MD2SKINS];
\r
169 bookframe_t bframes[MAX_MD2SKINS];
\r
173 GetScriptToken (false);
\r
174 strcpy (lumpname, token);
\r
176 GetScriptToken (false);
\r
178 GetScriptToken (false);
\r
180 GetScriptToken (false);
\r
182 GetScriptToken (false);
\r
189 if ( (w & 7) || (h & 7) )
\r
190 Error ("line %i: miptex sizes must be multiples of 8", scriptline);
\r
196 scale_x = scale_y = 0.5;
\r
206 if (xl >= longimagewidth || xh > longimagewidth ||
\r
207 yl >= longimageheight || yh > longimageheight)
\r
209 Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight);
\r
212 sourcel = (unsigned long *) longimage + (yl * longimagewidth) + xl;
\r
213 destl = (unsigned long *) longimage;
\r
214 linedelta = (longimagewidth - w);
\r
216 for(y = yl; y < yh; y++)
\r
218 for(x = xl; x < xh; x++)
\r
220 *destl++ = *sourcel++; // RGBA
\r
222 sourcel += linedelta;
\r
225 // Get rectangles to chop into
\r
226 numrects = ChopImage(w, h, coords);
\r
229 for(i = 0; i < numrects; i++, bf++)
\r
231 // Copy section of image to buffer
\r
232 sourcel = (unsigned long *) longimage + (coords[i].y * w) + coords[i].x;
\r
234 linedelta = w - coords[i].w;
\r
236 for(y = 0; y < coords[i].h; y++)
\r
238 for(x = 0; x < coords[i].w; x++)
\r
240 *destl++ = *sourcel++;
\r
242 sourcel += linedelta;
\r
245 qtex32 = CreateBook32(bufferl, coords[i].w, coords[i].h, &size);
\r
247 qtex32->flags = flags;
\r
248 qtex32->contents = contents;
\r
249 qtex32->value = value;
\r
250 qtex32->scale_x = scale_x;
\r
251 qtex32->scale_y = scale_y;
\r
253 sprintf (filename, "%sbook/%s/%s_%s.m32", gamedir, book_prefix, lumpname, coords[i].name);
\r
254 sprintf (qtex32->name, "%s/%s_%s.m32", book_prefix, lumpname, coords[i].name);
\r
256 strcpy(bf->name, qtex32->name);
\r
257 bf->x = coords[i].x;
\r
258 bf->y = coords[i].y;
\r
259 bf->w = coords[i].w;
\r
260 bf->h = coords[i].h;
\r
264 printf ("writing %s\n", filename);
\r
265 SaveFile (filename, (byte *)qtex32, size);
\r
275 if (xl >= byteimagewidth || xh > byteimagewidth ||
\r
276 yl >= byteimageheight || yh > byteimageheight)
\r
278 Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight);
\r
281 // Copy image to top left
\r
282 source = byteimage + yl*byteimagewidth + xl;
\r
284 linedelta = byteimagewidth - w;
\r
286 for(y = yl; y < yh; y++)
\r
288 for(x = xl; x < xh; x++)
\r
290 *dest++ = *source++;
\r
292 source += linedelta;
\r
295 // Get rectangles to chop into
\r
296 numrects = ChopImage(w, h, coords);
\r
299 for(i = 0; i < numrects; i++, bf++)
\r
301 // Copy section of image to buffer
\r
302 source = byteimage + (coords[i].y * w) + coords[i].x;
\r
304 linedelta = w - coords[i].w;
\r
306 for(y = 0; y < coords[i].h; y++)
\r
308 for(x = 0; x < coords[i].w; x++)
\r
310 *dest++ = *source++;
\r
312 source += linedelta;
\r
315 qtex = CreateBook8(buffer, coords[i].w, coords[i].h, lbmpalette, &size);
\r
317 qtex->flags = flags;
\r
318 qtex->contents = contents;
\r
319 qtex->value = value;
\r
321 sprintf (filename, "%sbook/%s/%s_%s.m8", gamedir, book_prefix, lumpname, coords[i].name);
\r
322 sprintf (qtex->name, "%s/%s_%s.m8", book_prefix, lumpname, coords[i].name);
\r
324 strcpy(bf->name, qtex->name);
\r
325 bf->x = coords[i].x;
\r
326 bf->y = coords[i].y;
\r
327 bf->w = coords[i].w;
\r
328 bf->h = coords[i].h;
\r
332 printf ("writing %s\n", filename);
\r
333 SaveFile (filename, (byte *)qtex, size);
\r
338 // Set up descriptor
\r
339 size = sizeof(bookframe_t) * numrects;
\r
341 book.bheader.ident = IDBOOKHEADER;
\r
342 book.bheader.version = BOOK_VERSION;
\r
343 book.bheader.num_segments = numrects;
\r
344 book.bheader.total_w = w;
\r
345 book.bheader.total_h = h;
\r
346 memcpy(book.bframes, bframes, size);
\r
348 // Save out segment descriptor
\r
349 sprintf (filename, "%sBook/%s/%s.bk", gamedir, book_prefix, lumpname);
\r
350 printf ("writing %s\n", filename);
\r
351 SaveFile (filename, (byte *)&book, size + sizeof(bookheader_t));
\r
359 void Cmd_Bookdir (void)
\r
361 char filename[1024];
\r
363 GetScriptToken (false);
\r
364 strcpy (book_prefix, token);
\r
365 // create the directory if needed
\r
366 sprintf (filename, "%sBook", gamedir);
\r
367 Q_mkdir (filename);
\r
368 sprintf (filename, "%sBook/%s", gamedir, book_prefix);
\r
369 Q_mkdir (filename);
\r