]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake2/qdata/sprites.c
better progress display
[divverent/netradiant.git] / tools / quake2 / qdata / sprites.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 #include "qdata.h"
23 #include "inout.h"
24
25 #define MAX_SPRFRAMES                   MAX_MD2SKINS
26
27 dsprite_t               sprite;
28 dsprframe_t             frames[MAX_SPRFRAMES];
29
30 byte                    *byteimage, *lbmpalette;
31 int                             byteimagewidth, byteimageheight;
32
33 char                    spritename[1024];
34
35
36 void FinishSprite (void);
37 void Cmd_Spritename (void);
38
39
40
41 /*
42 ==============
43 FinishSprite    
44 ==============
45 */
46 void FinishSprite (void)
47 {
48         FILE    *spriteouthandle;
49         int                     i, curframe;
50         dsprite_t       spritetemp;
51         char            savename[1024];
52
53         if (sprite.numframes == 0)
54                 return;
55
56         if (!strlen(spritename))
57                 Error ("Didn't name sprite file");
58                 
59         sprintf (savename, "%s%s.sp2", gamedir, spritename);
60
61         if (g_release)
62         {
63                 char    name[1024];
64
65                 sprintf (name, "%s.sp2", spritename);
66                 ReleaseFile (name);
67                 spritename[0] = 0;              // clear for a new sprite
68                 sprite.numframes = 0;
69                 return;
70         }
71
72
73         printf ("saving in %s\n", savename);
74         CreatePath (savename);
75         spriteouthandle = SafeOpenWrite (savename);
76
77
78 //
79 // write out the sprite header
80 //
81         spritetemp.ident = LittleLong (IDSPRITEHEADER);
82         spritetemp.version = LittleLong (SPRITE_VERSION);
83         spritetemp.numframes = LittleLong (sprite.numframes);
84
85         SafeWrite (spriteouthandle, &spritetemp, 12);
86
87 //
88 // write out the frames
89 //
90         curframe = 0;
91
92         for (i=0 ; i<sprite.numframes ; i++)
93         {
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);
98         }
99         SafeWrite (spriteouthandle, frames, sizeof(frames[0])*sprite.numframes);
100
101         fclose (spriteouthandle);
102         
103         spritename[0] = 0;              // clear for a new sprite
104         sprite.numframes = 0;
105 }
106
107
108 /*
109 ===============
110 Cmd_Load
111 ===============
112 */
113 void Cmd_Load (void)
114 {
115         char    *name;
116
117         GetToken (false);
118
119         if (g_release)
120                 return;
121
122         name = ExpandPathAndArchive(token);
123
124         // load the image
125         printf ("loading %s\n", name);
126         Load256Image (name, &byteimage, &lbmpalette, 
127                 &byteimagewidth, &byteimageheight);
128         RemapZero (byteimage, lbmpalette, 
129                 byteimagewidth, byteimageheight);
130 }
131
132
133 /*
134 ===============
135 Cmd_SpriteFrame
136 ===============
137 */
138 void Cmd_SpriteFrame (void)
139 {
140         int             y,xl,yl,xh,yh,w,h;
141         dsprframe_t             *pframe;
142         int                             ox, oy;
143         byte                    *cropped;
144         char                    savename[1024];
145
146         GetToken (false);
147         xl = atoi (token);
148         GetToken (false);
149         yl = atoi (token);
150         GetToken (false);
151         w = atoi (token);
152         GetToken (false);
153         h = atoi (token);
154
155         // origin offset is optional
156         if (TokenAvailable ())
157         {
158                 GetToken (false);
159                 ox = atoi (token);
160                 GetToken (false);
161                 oy = atoi (token);              
162         }
163         else
164         {
165                 ox = w/2;
166                 oy = h/2;
167         }
168
169         if ((xl & 0x07) || (yl & 0x07) || (w & 0x07) || (h & 0x07))
170                 Error ("Sprite dimensions not multiples of 8\n");
171
172         if ((w > 256) || (h > 256))
173                 Error ("Sprite has a dimension longer than 256");
174
175         xh = xl+w;
176         yh = yl+h;
177
178         if (sprite.numframes >= MAX_SPRFRAMES)
179                 Error ("Too many frames; increase MAX_SPRFRAMES\n");
180
181         pframe = &frames[sprite.numframes];
182         pframe->width = w;
183         pframe->height = h;
184         pframe->origin_x = ox;
185         pframe->origin_y = oy;
186         sprintf (pframe->name, "%s_%i.pcx", spritename, sprite.numframes);
187         sprintf (savename, "%s%s_%i.pcx", gamedir, spritename, sprite.numframes);
188         sprite.numframes++;
189
190         if (g_release)
191         {
192                 ReleaseFile (pframe->name);
193                 return;
194         }
195
196         // crop it to the proper size
197         cropped = malloc (w*h);
198         for (y=0 ; y<h ; y++)
199         {
200                 memcpy (cropped+y*w, byteimage+(y+yl)*byteimagewidth+xl, w);
201         }
202
203         // save off the new image
204         printf ("saving %s\n", savename);
205         CreatePath (savename);
206         WritePCXfile (savename, cropped, w,     h, lbmpalette);
207
208         free (cropped);
209 }
210
211
212
213 /*
214 ==============
215 Cmd_SpriteName
216 ==============
217 */
218 void Cmd_SpriteName (void)
219 {
220         if (sprite.numframes)
221                 FinishSprite ();
222
223         GetToken (false);
224         strcpy (spritename, token);
225         memset (&sprite, 0, sizeof(sprite));
226         memset (&frames, 0, sizeof(frames));
227 }
228