]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/qdata_heretic2/book.c
ok
[xonotic/netradiant.git] / tools / quake2 / qdata_heretic2 / book.c
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22
23 #include "qdata.h"
24
25 byte                    *byteimage, *lbmpalette;
26 int                             byteimagewidth, byteimageheight;
27
28 qboolean                TrueColorImage;
29 int                             longimagewidth, longimageheight;
30
31 char                    book_prefix[1024];
32 byte                    buffer[640 * 480];
33 unsigned long   bufferl[640 * 480];
34
35 miptex_t *CreateBook8(byte *buffer, int w, int h, byte *palette, int *FinalSize)
36 {
37         miptex_t        *mp;
38         int                     i, j;
39         byte            *pos;
40         int                     size;
41
42         size = sizeof(*mp) + (w * h);
43         mp = (miptex_t *)SafeMalloc(size, "CreateBook8");
44         memset(mp, 0, size);
45
46         mp->version = MIP_VERSION;
47
48         for(i=j=0;i<256;i++,j+=3)
49         {
50                 mp->palette[i].r = palette[j];
51                 mp->palette[i].g = palette[j+1];
52                 mp->palette[i].b = palette[j+2];
53         }
54         pos = (byte *)(mp + 1);
55
56         mp->width[0] = w;
57         mp->height[0] = h;
58         mp->offsets[0] = sizeof(*mp);
59         memcpy(pos, buffer, w * h);
60
61         *FinalSize = size;
62         return(mp);
63 }
64
65 miptex32_t *CreateBook32(long *buffer, int w, int h, int *FinalSize)
66 {
67         miptex32_t      *mp;
68         byte            *pos;
69         int                     size;
70
71         size = sizeof(*mp) + (w * h * 4);
72         mp = (miptex32_t *)SafeMalloc(size, "CreateBook32");
73         memset(mp, 0, size);
74
75         mp->version = MIP32_VERSION;
76
77         pos = (byte *)(mp + 1);
78
79         mp->width[0] = w;
80         mp->height[0] = h;
81         mp->offsets[0] = sizeof(*mp);
82         memcpy(pos, buffer, w * h * 4);
83
84         *FinalSize = size;
85         return(mp);
86 }
87
88
89 // Routines to chop a random sized image into gl texture friendly chunks
90
91 typedef struct rect_s 
92 {
93         int             x, y;
94         int             w, h;
95         char    name[4];
96 } rect_t;
97
98 int GetCoords(int x, int store[MAX_MD2SKINS])
99 {
100         int             index, start, delta;
101
102         index = 0;
103         start = 0;
104         delta = 256;
105
106         store[index++] = start;
107         while(x)
108         {
109                 if(x >= delta)
110                 {
111                         start += delta;
112                         store[index++] = start;
113                         x -= delta;
114                 }
115                 else
116                 {
117                         delta >>= 1;
118                 }
119         }
120         return(index);
121 }
122
123 int ChopImage(int w, int h, rect_t coords[MAX_MD2SKINS])
124 {
125         int             xs[MAX_MD2SKINS], ys[MAX_MD2SKINS];
126         int             xcount, ycount, x, y, index;
127
128         index = 0;
129         xcount = GetCoords(w, xs) - 1;
130         ycount = GetCoords(h, ys) - 1;
131
132         for(y = 0; y < ycount; y++)
133         {
134                 for(x = 0; x < xcount; x++, index++)
135                 {
136                         coords[index].x = xs[x];
137                         coords[index].y = ys[y];
138                         coords[index].w = xs[x + 1] - xs[x];
139                         coords[index].h = ys[y + 1] - ys[y];
140                         coords[index].name[0] = x + '0';
141                         coords[index].name[1] = y + '0';
142                         coords[index].name[2] = 0;
143                 }
144         }
145         return(index);
146 }
147
148 /*
149 ===============
150 Cmd_Pic
151 ===============
152 */
153
154 void Cmd_Book()
155 {
156         int             xl,yl,xh,yh,w,h;
157         byte            *dest, *source;
158         int                             flags, value, contents;
159         char                    lumpname[64];
160         char                    filename[1024];
161         unsigned long   *destl, *sourcel;
162         int             linedelta, x, y;
163         int                             size;
164         miptex_t                *qtex;
165         miptex32_t              *qtex32;
166         float                   scale_x, scale_y;
167         int                             numrects, i;
168         rect_t                  coords[MAX_MD2SKINS];
169         bookframe_t             bframes[MAX_MD2SKINS];
170         bookframe_t             *bf;
171         book_t                  book;
172
173         GetScriptToken (false);
174         strcpy (lumpname, token);
175         
176         GetScriptToken (false);
177         xl = atoi (token);
178         GetScriptToken (false);
179         yl = atoi (token);
180         GetScriptToken (false);
181         w = atoi (token);
182         GetScriptToken (false);
183         h = atoi (token);
184
185         total_x += w;
186         total_y += h;
187         total_textures++;
188
189         if ( (w & 7) || (h & 7) )
190                 Error ("line %i: miptex sizes must be multiples of 8", scriptline);
191
192         flags = 0;
193         contents = 0;
194         value = 0;
195
196         scale_x = scale_y = 0.5;
197
198         if (g_release)
199                 return;
200
201         if(TrueColorImage)
202         {
203                 xh = xl + w;
204                 yh = yl + h;
205
206                 if (xl >= longimagewidth || xh > longimagewidth ||
207                         yl >= longimageheight || yh > longimageheight)
208                 {
209                         Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight);
210                 }
211
212                 sourcel = (unsigned long *) longimage + (yl * longimagewidth) + xl;
213                 destl = (unsigned long *) longimage;
214                 linedelta = (longimagewidth - w);
215
216                 for(y = yl; y < yh; y++)
217                 {
218                         for(x = xl; x < xh; x++)
219                         {
220                                 *destl++ = *sourcel++;  // RGBA
221                         }
222                         sourcel += linedelta;
223                 }
224
225                 // Get rectangles to chop into
226                 numrects = ChopImage(w, h, coords);
227
228                 bf = bframes;
229                 for(i = 0; i < numrects; i++, bf++)
230                 {
231                         // Copy section of image to buffer
232                         sourcel = (unsigned long *) longimage + (coords[i].y * w) + coords[i].x;
233                         destl = bufferl;
234                         linedelta = w - coords[i].w;
235
236                         for(y = 0; y < coords[i].h; y++)
237                         {
238                                 for(x = 0; x < coords[i].w; x++)
239                                 {
240                                         *destl++ = *sourcel++;
241                                 }
242                                 sourcel += linedelta;
243                         }
244
245                         qtex32 = CreateBook32(bufferl, coords[i].w, coords[i].h, &size);
246
247                         qtex32->flags = flags;
248                         qtex32->contents = contents;
249                         qtex32->value = value;
250                         qtex32->scale_x = scale_x;
251                         qtex32->scale_y = scale_y;
252
253                         sprintf (filename, "%sbook/%s/%s_%s.m32", gamedir, book_prefix, lumpname, coords[i].name);
254                         sprintf (qtex32->name, "%s/%s_%s.m32", book_prefix, lumpname, coords[i].name);
255
256                         strcpy(bf->name, qtex32->name);
257                         bf->x = coords[i].x;
258                         bf->y = coords[i].y;
259                         bf->w = coords[i].w;
260                         bf->h = coords[i].h;
261                 //
262                 // write it out
263                 //
264                         printf ("writing %s\n", filename);
265                         SaveFile (filename, (byte *)qtex32, size);
266
267                         free (qtex32);
268                 }
269         }
270         else
271         {
272                 xh = xl + w;
273                 yh = yl + h;
274
275                 if (xl >= byteimagewidth || xh > byteimagewidth ||
276                         yl >= byteimageheight || yh > byteimageheight)
277                 {
278                         Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight);
279                 }
280
281                 // Copy image to top left
282                 source = byteimage + yl*byteimagewidth + xl;
283                 dest = byteimage;
284                 linedelta = byteimagewidth - w;
285
286                 for(y = yl; y < yh; y++)
287                 {
288                         for(x = xl; x < xh; x++)
289                         {
290                                 *dest++ = *source++;
291                         }
292                         source += linedelta;
293                 }
294
295                 // Get rectangles to chop into
296                 numrects = ChopImage(w, h, coords);
297
298                 bf = bframes;
299                 for(i = 0; i < numrects; i++, bf++)
300                 {
301                         // Copy section of image to buffer
302                         source = byteimage + (coords[i].y * w) + coords[i].x;
303                         dest = buffer;
304                         linedelta = w - coords[i].w;
305
306                         for(y = 0; y < coords[i].h; y++)
307                         {
308                                 for(x = 0; x < coords[i].w; x++)
309                                 {
310                                         *dest++ = *source++;
311                                 }
312                                 source += linedelta;
313                         }
314
315                         qtex = CreateBook8(buffer, coords[i].w, coords[i].h, lbmpalette, &size);
316
317                         qtex->flags = flags;
318                         qtex->contents = contents;
319                         qtex->value = value;
320
321                         sprintf (filename, "%sbook/%s/%s_%s.m8", gamedir, book_prefix, lumpname, coords[i].name);
322                         sprintf (qtex->name, "%s/%s_%s.m8", book_prefix, lumpname, coords[i].name);
323
324                         strcpy(bf->name, qtex->name);
325                         bf->x = coords[i].x;
326                         bf->y = coords[i].y;
327                         bf->w = coords[i].w;
328                         bf->h = coords[i].h;
329                 //
330                 // write it out
331                 //
332                         printf ("writing %s\n", filename);
333                         SaveFile (filename, (byte *)qtex, size);
334
335                         free (qtex);
336                 }
337         }
338         // Set up descriptor
339         size = sizeof(bookframe_t) * numrects;
340
341         book.bheader.ident = IDBOOKHEADER;
342         book.bheader.version = BOOK_VERSION;
343         book.bheader.num_segments = numrects;
344         book.bheader.total_w = w;
345         book.bheader.total_h = h;
346         memcpy(book.bframes, bframes, size);
347
348         // Save out segment descriptor
349         sprintf (filename, "%sBook/%s/%s.bk", gamedir, book_prefix, lumpname);
350         printf ("writing %s\n", filename);
351         SaveFile (filename, (byte *)&book, size + sizeof(bookheader_t));
352 }
353
354 /*
355 ===============
356 Cmd_picdir
357 ===============
358 */
359 void Cmd_Bookdir (void)
360 {
361         char    filename[1024];
362
363         GetScriptToken (false);
364         strcpy (book_prefix, token);
365         // create the directory if needed
366         sprintf (filename, "%sBook", gamedir);
367         Q_mkdir (filename); 
368         sprintf (filename, "%sBook/%s", gamedir, book_prefix);
369         Q_mkdir (filename); 
370 }
371
372 // end