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 static dsprframe_t frames[MAX_SPRFRAMES];
30 char spritename[1024];
33 void FinishSprite( void );
34 void Cmd_Spritename( void );
36 char spr_prefix[1024];
38 extern char *g_outputDir;
46 void FinishSprite( void ){
47 FILE *spriteouthandle;
52 if ( sprite.numframes == 0 ) {
56 if ( !strlen( spritename ) ) {
57 Error( "Didn't name sprite file" );
60 sprintf( savename, "%sSprites/%s/%s.sp2", g_outputDir, spr_prefix, spritename );
65 sprintf( name, "%s.sp2", spritename );
67 spritename[0] = 0; // clear for a new sprite
73 printf( "saving in %s\n", savename );
74 CreatePath( savename );
75 spriteouthandle = SafeOpenWrite( savename );
79 // write out the sprite header
81 spritetemp.ident = LittleLong( IDSPRITEHEADER );
82 spritetemp.version = LittleLong( SPRITE_VERSION );
83 spritetemp.numframes = LittleLong( sprite.numframes );
85 SafeWrite( spriteouthandle, &spritetemp, 12 );
88 // write out the frames
92 for ( i = 0 ; i < sprite.numframes ; i++ )
94 frames[i].width = LittleLong( frames[i].width );
95 frames[i].height = LittleLong( frames[i].height );
96 frames[i].origin_x = LittleLong( frames[i].origin_x );
97 frames[i].origin_y = LittleLong( frames[i].origin_y );
99 SafeWrite( spriteouthandle, frames, sizeof( frames[0] ) * sprite.numframes );
101 fclose( spriteouthandle );
103 spritename[0] = 0; // clear for a new sprite
104 sprite.numframes = 0;
113 void Cmd_Load( void ){
116 GetScriptToken( false );
122 name = ExpandPathAndArchive( token );
125 printf( "loading %s\n", name );
126 TrueColorImage = LoadAnyImage( name, &byteimage, &lbmpalette, &byteimagewidth, &byteimageheight );
128 if ( !TrueColorImage ) {
129 // RemapZero (byteimage, lbmpalette, byteimagewidth, byteimageheight);
136 longimage = (unsigned *)byteimage;
137 longimagewidth = byteimagewidth;
138 longimageheight = byteimageheight;
153 void Cmd_SpriteFrame( void ){
154 int x,y,xl,yl,xh,yh,w,h;
156 int ox, oy, linedelta, size;
161 unsigned *destl, *sourcel;
162 unsigned bufferl[256 * 256];
164 byte buffer[256 * 256];
166 GetScriptToken( false );
168 GetScriptToken( false );
170 GetScriptToken( false );
172 GetScriptToken( false );
175 // origin offset is optional
176 if ( ScriptTokenAvailable() ) {
177 GetScriptToken( false );
179 GetScriptToken( false );
188 if ( ( xl & 0x0f ) || ( yl & 0x0f ) || ( w & 0x0f ) || ( h & 0x0f ) ) {
189 Error( "Sprite dimensions not multiples of 16\n" );
192 if ( ( w > 256 ) || ( h > 256 ) ) {
193 Error( "Sprite has a dimension longer than 256" );
199 if ( sprite.numframes >= MAX_SPRFRAMES ) {
200 Error( "Too many frames; increase MAX_SPRFRAMES\n" );
203 pframe = &frames[sprite.numframes];
206 pframe->origin_x = ox;
207 pframe->origin_y = oy;
210 ReleaseFile( pframe->name );
214 if ( TrueColorImage ) {
215 sprintf( filename, "%ssprites/%s/%s_%i.m32", g_outputDir, spr_prefix, spritename, sprite.numframes );
216 sprintf( pframe->name, "%s/%s_%i.m32", spr_prefix, spritename, sprite.numframes );
219 return; // textures are only released by $maps
225 if ( xl >= longimagewidth || xh > longimagewidth ||
226 yl >= longimageheight || yh > longimageheight ) {
227 Error( "line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight );
230 sourcel = longimage + ( yl * longimagewidth ) + xl;
232 linedelta = ( longimagewidth - w );
234 for ( y = yl ; y < yh ; y++ )
236 for ( x = xl ; x < xh ; x++ )
238 *destl++ = *sourcel++; // RGBA
240 sourcel += linedelta;
243 qtex32 = CreateMip32( bufferl, w, h, &size, true );
245 qtex32->contents = 0;
247 strcpy( qtex32->name, pframe->name );
251 printf( "writing %s\n", filename );
252 SaveFile( filename, (byte *)qtex32, size );
258 sprintf( filename, "%ssprites/%s/%s_%i.m8", g_outputDir, spr_prefix, spritename, sprite.numframes );
259 sprintf( pframe->name, "%s/%s_%i.m8", spr_prefix, spritename, sprite.numframes );
262 return; // textures are only released by $maps
268 if ( xl >= byteimagewidth || xh > byteimagewidth ||
269 yl >= byteimageheight || yh > byteimageheight ) {
270 Error( "line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight );
273 source = byteimage + yl * byteimagewidth + xl;
275 linedelta = byteimagewidth - w;
277 for ( y = yl ; y < yh ; y++ )
279 for ( x = xl ; x < xh ; x++ )
286 qtex = CreateMip( buffer, w, h, lbmpalette, &size, true );
291 strcpy( qtex->name, pframe->name );
295 printf( "writing %s\n", filename );
296 SaveFile( filename, (byte *)qtex, size );
310 void Cmd_SpriteName( void ){
311 if ( sprite.numframes ) {
315 GetScriptToken( false );
316 strcpy( spritename, token );
317 memset( &sprite, 0, sizeof( sprite ) );
318 memset( &frames, 0, sizeof( frames ) );
327 void Cmd_Sprdir( void ){
330 GetScriptToken( false );
331 strcpy( spr_prefix, token );
332 // create the directory if needed
333 sprintf( filename, "%sSprites", g_outputDir );
335 sprintf( filename, "%sSprites/%s", g_outputDir, spr_prefix );