2 Copyright (C) 1999-2006 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)
56 FILE *spriteouthandle;
61 if (sprite.numframes == 0)
64 if (!strlen(spritename))
65 Error ("Didn't name sprite file");
67 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;
125 GetScriptToken (false);
130 name = ExpandPathAndArchive(token);
133 printf ("loading %s\n", name);
134 TrueColorImage = LoadAnyImage (name, &byteimage, &lbmpalette, &byteimagewidth, &byteimageheight);
138 // RemapZero (byteimage, lbmpalette, byteimagewidth, byteimageheight);
144 longimage = (unsigned *)byteimage;
145 longimagewidth = byteimagewidth;
146 longimageheight = byteimageheight;
161 void Cmd_SpriteFrame (void)
163 int x,y,xl,yl,xh,yh,w,h;
165 int ox, oy, linedelta, size;
170 unsigned *destl, *sourcel;
171 unsigned bufferl[256*256];
173 byte buffer[256*256];
175 GetScriptToken (false);
177 GetScriptToken (false);
179 GetScriptToken (false);
181 GetScriptToken (false);
184 // origin offset is optional
185 if (ScriptTokenAvailable ())
187 GetScriptToken (false);
189 GetScriptToken (false);
198 if ((xl & 0x0f) || (yl & 0x0f) || (w & 0x0f) || (h & 0x0f))
199 Error ("Sprite dimensions not multiples of 16\n");
201 if ((w > 256) || (h > 256))
202 Error ("Sprite has a dimension longer than 256");
207 if (sprite.numframes >= MAX_SPRFRAMES)
208 Error ("Too many frames; increase MAX_SPRFRAMES\n");
210 pframe = &frames[sprite.numframes];
213 pframe->origin_x = ox;
214 pframe->origin_y = oy;
218 ReleaseFile (pframe->name);
224 sprintf (filename, "%ssprites/%s/%s_%i.m32", g_outputDir, spr_prefix, spritename, sprite.numframes);
225 sprintf (pframe->name, "%s/%s_%i.m32", spr_prefix, spritename, sprite.numframes);
228 return; // textures are only released by $maps
233 if (xl >= longimagewidth || xh > longimagewidth ||
234 yl >= longimageheight || yh > longimageheight)
236 Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight);
239 sourcel = longimage + (yl*longimagewidth) + xl;
241 linedelta = (longimagewidth - w);
243 for (y=yl ; y<yh ; y++)
245 for (x=xl ; x<xh ; x++)
247 *destl++ = *sourcel++; // RGBA
249 sourcel += linedelta;
252 qtex32 = CreateMip32(bufferl, w, h, &size, true);
254 qtex32->contents = 0;
256 strcpy(qtex32->name, pframe->name);
260 printf ("writing %s\n", filename);
261 SaveFile (filename, (byte *)qtex32, size);
267 sprintf (filename, "%ssprites/%s/%s_%i.m8", g_outputDir, spr_prefix, spritename, sprite.numframes);
268 sprintf (pframe->name, "%s/%s_%i.m8", spr_prefix, spritename, sprite.numframes);
271 return; // textures are only released by $maps
276 if (xl >= byteimagewidth || xh > byteimagewidth ||
277 yl >= byteimageheight || yh > byteimageheight)
279 Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight);
282 source = byteimage + yl*byteimagewidth + xl;
284 linedelta = byteimagewidth - w;
286 for (y=yl ; y<yh ; y++)
288 for (x=xl ; x<xh ; x++)
295 qtex = CreateMip(buffer, w, h, lbmpalette, &size, true);
300 strcpy(qtex->name, pframe->name);
304 printf ("writing %s\n", filename);
305 SaveFile (filename, (byte *)qtex, size);
319 void Cmd_SpriteName (void)
321 if (sprite.numframes)
324 GetScriptToken (false);
325 strcpy (spritename, token);
326 memset (&sprite, 0, sizeof(sprite));
327 memset (&frames, 0, sizeof(frames));
336 void Cmd_Sprdir (void)
340 GetScriptToken (false);
341 strcpy (spr_prefix, token);
342 // create the directory if needed
343 sprintf (filename, "%sSprites", g_outputDir);
345 sprintf (filename, "%sSprites/%s", g_outputDir, spr_prefix);