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 #define MAX_SPRFRAMES MAX_MD2SKINS
28 dsprframe_t frames[MAX_SPRFRAMES];
30 byte *byteimage, *lbmpalette;
31 int byteimagewidth, byteimageheight;
33 qboolean TrueColorImage;
35 int longimagewidth, longimageheight;
37 char spritename[1024];
40 void FinishSprite( void );
41 void Cmd_Spritename( void );
43 char spr_prefix[1024];
44 char pic_prefix[1024];
46 extern char *g_outputDir;
54 void FinishSprite( void ){
55 FILE *spriteouthandle;
60 if ( sprite.numframes == 0 ) {
64 if ( !strlen( spritename ) ) {
65 Error( "Didn't name sprite file" );
68 sprintf( savename, "%sSprites/%s/%s.sp2", g_outputDir, spr_prefix, spritename );
73 sprintf( name, "%s.sp2", spritename );
75 spritename[0] = 0; // clear for a new sprite
81 printf( "saving in %s\n", savename );
82 CreatePath( savename );
83 spriteouthandle = SafeOpenWrite( savename );
87 // write out the sprite header
89 spritetemp.ident = LittleLong( IDSPRITEHEADER );
90 spritetemp.version = LittleLong( SPRITE_VERSION );
91 spritetemp.numframes = LittleLong( sprite.numframes );
93 SafeWrite( spriteouthandle, &spritetemp, 12 );
96 // write out the frames
100 for ( i = 0 ; i < sprite.numframes ; i++ )
102 frames[i].width = LittleLong( frames[i].width );
103 frames[i].height = LittleLong( frames[i].height );
104 frames[i].origin_x = LittleLong( frames[i].origin_x );
105 frames[i].origin_y = LittleLong( frames[i].origin_y );
107 SafeWrite( spriteouthandle, frames, sizeof( frames[0] ) * sprite.numframes );
109 fclose( spriteouthandle );
111 spritename[0] = 0; // clear for a new sprite
112 sprite.numframes = 0;
121 void Cmd_Load( void ){
124 GetScriptToken( false );
130 name = ExpandPathAndArchive( token );
133 printf( "loading %s\n", name );
134 TrueColorImage = LoadAnyImage( name, &byteimage, &lbmpalette, &byteimagewidth, &byteimageheight );
136 if ( !TrueColorImage ) {
137 // RemapZero (byteimage, lbmpalette, byteimagewidth, byteimageheight);
144 longimage = (unsigned *)byteimage;
145 longimagewidth = byteimagewidth;
146 longimageheight = byteimageheight;
161 void Cmd_SpriteFrame( void ){
162 int x,y,xl,yl,xh,yh,w,h;
164 int ox, oy, linedelta, size;
169 unsigned *destl, *sourcel;
170 unsigned bufferl[256 * 256];
172 byte buffer[256 * 256];
174 GetScriptToken( false );
176 GetScriptToken( false );
178 GetScriptToken( false );
180 GetScriptToken( false );
183 // origin offset is optional
184 if ( ScriptTokenAvailable() ) {
185 GetScriptToken( false );
187 GetScriptToken( false );
196 if ( ( xl & 0x0f ) || ( yl & 0x0f ) || ( w & 0x0f ) || ( h & 0x0f ) ) {
197 Error( "Sprite dimensions not multiples of 16\n" );
200 if ( ( w > 256 ) || ( h > 256 ) ) {
201 Error( "Sprite has a dimension longer than 256" );
207 if ( sprite.numframes >= MAX_SPRFRAMES ) {
208 Error( "Too many frames; increase MAX_SPRFRAMES\n" );
211 pframe = &frames[sprite.numframes];
214 pframe->origin_x = ox;
215 pframe->origin_y = oy;
218 ReleaseFile( pframe->name );
222 if ( TrueColorImage ) {
223 sprintf( filename, "%ssprites/%s/%s_%i.m32", g_outputDir, spr_prefix, spritename, sprite.numframes );
224 sprintf( pframe->name, "%s/%s_%i.m32", spr_prefix, spritename, sprite.numframes );
227 return; // textures are only released by $maps
233 if ( xl >= longimagewidth || xh > longimagewidth ||
234 yl >= longimageheight || yh > longimageheight ) {
235 Error( "line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight );
238 sourcel = longimage + ( yl * longimagewidth ) + xl;
240 linedelta = ( longimagewidth - w );
242 for ( y = yl ; y < yh ; y++ )
244 for ( x = xl ; x < xh ; x++ )
246 *destl++ = *sourcel++; // RGBA
248 sourcel += linedelta;
251 qtex32 = CreateMip32( bufferl, w, h, &size, true );
253 qtex32->contents = 0;
255 strcpy( qtex32->name, pframe->name );
259 printf( "writing %s\n", filename );
260 SaveFile( filename, (byte *)qtex32, size );
266 sprintf( filename, "%ssprites/%s/%s_%i.m8", g_outputDir, spr_prefix, spritename, sprite.numframes );
267 sprintf( pframe->name, "%s/%s_%i.m8", spr_prefix, spritename, sprite.numframes );
270 return; // textures are only released by $maps
276 if ( xl >= byteimagewidth || xh > byteimagewidth ||
277 yl >= byteimageheight || yh > byteimageheight ) {
278 Error( "line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight );
281 source = byteimage + yl * byteimagewidth + xl;
283 linedelta = byteimagewidth - w;
285 for ( y = yl ; y < yh ; y++ )
287 for ( x = xl ; x < xh ; x++ )
294 qtex = CreateMip( buffer, w, h, lbmpalette, &size, true );
299 strcpy( qtex->name, pframe->name );
303 printf( "writing %s\n", filename );
304 SaveFile( filename, (byte *)qtex, size );
318 void Cmd_SpriteName( void ){
319 if ( sprite.numframes ) {
323 GetScriptToken( false );
324 strcpy( spritename, token );
325 memset( &sprite, 0, sizeof( sprite ) );
326 memset( &frames, 0, sizeof( frames ) );
335 void Cmd_Sprdir( void ){
338 GetScriptToken( false );
339 strcpy( spr_prefix, token );
340 // create the directory if needed
341 sprintf( filename, "%sSprites", g_outputDir );
343 sprintf( filename, "%sSprites/%s", g_outputDir, spr_prefix );