2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 char book_prefix[1024];
26 byte buffer[640 * 480];
27 unsigned long bufferl[640 * 480];
29 miptex_t *CreateBook8( byte *buffer, int w, int h, byte *palette, int *FinalSize ){
35 size = sizeof( *mp ) + ( w * h );
36 mp = (miptex_t *)SafeMalloc( size, "CreateBook8" );
37 memset( mp, 0, size );
39 mp->version = MIP_VERSION;
41 for ( i = j = 0; i < 256; i++,j += 3 )
43 mp->palette[i].r = palette[j];
44 mp->palette[i].g = palette[j + 1];
45 mp->palette[i].b = palette[j + 2];
47 pos = (byte *)( mp + 1 );
51 mp->offsets[0] = sizeof( *mp );
52 memcpy( pos, buffer, w * h );
58 miptex32_t *CreateBook32( long *buffer, int w, int h, int *FinalSize ){
63 size = sizeof( *mp ) + ( w * h * 4 );
64 mp = (miptex32_t *)SafeMalloc( size, "CreateBook32" );
65 memset( mp, 0, size );
67 mp->version = MIP32_VERSION;
69 pos = (byte *)( mp + 1 );
73 mp->offsets[0] = sizeof( *mp );
74 memcpy( pos, buffer, w * h * 4 );
81 // Routines to chop a random sized image into gl texture friendly chunks
90 int GetCoords( int x, int store[MAX_MD2SKINS] ){
91 int index, start, delta;
97 store[index++] = start;
102 store[index++] = start;
113 int ChopImage( int w, int h, rect_t coords[MAX_MD2SKINS] ){
114 int xs[MAX_MD2SKINS], ys[MAX_MD2SKINS];
115 int xcount, ycount, x, y, index;
118 xcount = GetCoords( w, xs ) - 1;
119 ycount = GetCoords( h, ys ) - 1;
121 for ( y = 0; y < ycount; y++ )
123 for ( x = 0; x < xcount; x++, index++ )
125 coords[index].x = xs[x];
126 coords[index].y = ys[y];
127 coords[index].w = xs[x + 1] - xs[x];
128 coords[index].h = ys[y + 1] - ys[y];
129 coords[index].name[0] = x + '0';
130 coords[index].name[1] = y + '0';
131 coords[index].name[2] = 0;
146 int flags, value, contents;
149 unsigned long *destl, *sourcel;
154 float scale_x, scale_y;
156 rect_t coords[MAX_MD2SKINS];
157 bookframe_t bframes[MAX_MD2SKINS];
161 GetScriptToken( false );
162 strcpy( lumpname, token );
164 GetScriptToken( false );
166 GetScriptToken( false );
168 GetScriptToken( false );
170 GetScriptToken( false );
177 if ( ( w & 7 ) || ( h & 7 ) ) {
178 Error( "line %i: miptex sizes must be multiples of 8", scriptline );
185 scale_x = scale_y = 0.5;
191 if ( TrueColorImage ) {
195 if ( xl >= longimagewidth || xh > longimagewidth ||
196 yl >= longimageheight || yh > longimageheight ) {
197 Error( "line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight );
200 sourcel = (unsigned long *) longimage + ( yl * longimagewidth ) + xl;
201 destl = (unsigned long *) longimage;
202 linedelta = ( longimagewidth - w );
204 for ( y = yl; y < yh; y++ )
206 for ( x = xl; x < xh; x++ )
208 *destl++ = *sourcel++; // RGBA
210 sourcel += linedelta;
213 // Get rectangles to chop into
214 numrects = ChopImage( w, h, coords );
217 for ( i = 0; i < numrects; i++, bf++ )
219 // Copy section of image to buffer
220 sourcel = (unsigned long *) longimage + ( coords[i].y * w ) + coords[i].x;
222 linedelta = w - coords[i].w;
224 for ( y = 0; y < coords[i].h; y++ )
226 for ( x = 0; x < coords[i].w; x++ )
228 *destl++ = *sourcel++;
230 sourcel += linedelta;
233 qtex32 = CreateBook32( bufferl, coords[i].w, coords[i].h, &size );
235 qtex32->flags = flags;
236 qtex32->contents = contents;
237 qtex32->value = value;
238 qtex32->scale_x = scale_x;
239 qtex32->scale_y = scale_y;
241 sprintf( filename, "%sbook/%s/%s_%s.m32", gamedir, book_prefix, lumpname, coords[i].name );
242 sprintf( qtex32->name, "%s/%s_%s.m32", book_prefix, lumpname, coords[i].name );
244 strcpy( bf->name, qtex32->name );
252 printf( "writing %s\n", filename );
253 SaveFile( filename, (byte *)qtex32, size );
263 if ( xl >= byteimagewidth || xh > byteimagewidth ||
264 yl >= byteimageheight || yh > byteimageheight ) {
265 Error( "line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight );
268 // Copy image to top left
269 source = byteimage + yl * byteimagewidth + xl;
271 linedelta = byteimagewidth - w;
273 for ( y = yl; y < yh; y++ )
275 for ( x = xl; x < xh; x++ )
282 // Get rectangles to chop into
283 numrects = ChopImage( w, h, coords );
286 for ( i = 0; i < numrects; i++, bf++ )
288 // Copy section of image to buffer
289 source = byteimage + ( coords[i].y * w ) + coords[i].x;
291 linedelta = w - coords[i].w;
293 for ( y = 0; y < coords[i].h; y++ )
295 for ( x = 0; x < coords[i].w; x++ )
302 qtex = CreateBook8( buffer, coords[i].w, coords[i].h, lbmpalette, &size );
305 qtex->contents = contents;
308 sprintf( filename, "%sbook/%s/%s_%s.m8", gamedir, book_prefix, lumpname, coords[i].name );
309 sprintf( qtex->name, "%s/%s_%s.m8", book_prefix, lumpname, coords[i].name );
311 strcpy( bf->name, qtex->name );
319 printf( "writing %s\n", filename );
320 SaveFile( filename, (byte *)qtex, size );
326 size = sizeof( bookframe_t ) * numrects;
328 book.bheader.ident = IDBOOKHEADER;
329 book.bheader.version = BOOK_VERSION;
330 book.bheader.num_segments = numrects;
331 book.bheader.total_w = w;
332 book.bheader.total_h = h;
333 memcpy( book.bframes, bframes, size );
335 // Save out segment descriptor
336 sprintf( filename, "%sBook/%s/%s.bk", gamedir, book_prefix, lumpname );
337 printf( "writing %s\n", filename );
338 SaveFile( filename, (byte *)&book, size + sizeof( bookheader_t ) );
346 void Cmd_Bookdir( void ){
349 GetScriptToken( false );
350 strcpy( book_prefix, token );
351 // create the directory if needed
352 sprintf( filename, "%sBook", gamedir );
354 sprintf( filename, "%sBook/%s", gamedir, book_prefix );