]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/qdata_heretic2/sprites.c
Merge commit '515673c08f8718a237e90c2130a1f5294f966d6a'
[xonotic/netradiant.git] / tools / quake2 / qdata_heretic2 / 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
23 #include "qdata.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 qboolean                TrueColorImage;
34 unsigned                *longimage;
35 int                             longimagewidth, longimageheight;
36
37 char                    spritename[1024];
38
39
40 void FinishSprite (void);
41 void Cmd_Spritename (void);
42
43 char spr_prefix[1024];
44 char pic_prefix[1024];
45
46 extern  char            *g_outputDir;
47
48
49 /*
50 ==============
51 FinishSprite    
52 ==============
53 */
54 void FinishSprite (void)
55 {
56         FILE    *spriteouthandle;
57         int                     i, curframe;
58         dsprite_t       spritetemp;
59         char            savename[1024];
60
61         if (sprite.numframes == 0)
62                 return;
63
64         if (!strlen(spritename))
65                 Error ("Didn't name sprite file");
66                 
67         sprintf (savename, "%sSprites/%s/%s.sp2", g_outputDir, spr_prefix, spritename);
68
69         if (g_release)
70         {
71                 char    name[1024];
72
73                 sprintf (name, "%s.sp2", spritename);
74                 ReleaseFile (name);
75                 spritename[0] = 0;              // clear for a new sprite
76                 sprite.numframes = 0;
77                 return;
78         }
79
80
81         printf ("saving in %s\n", savename);
82         CreatePath (savename);
83         spriteouthandle = SafeOpenWrite (savename);
84
85
86 //
87 // write out the sprite header
88 //
89         spritetemp.ident = LittleLong (IDSPRITEHEADER);
90         spritetemp.version = LittleLong (SPRITE_VERSION);
91         spritetemp.numframes = LittleLong (sprite.numframes);
92
93         SafeWrite (spriteouthandle, &spritetemp, 12);
94
95 //
96 // write out the frames
97 //
98         curframe = 0;
99
100         for (i=0 ; i<sprite.numframes ; i++)
101         {
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);
106         }
107         SafeWrite (spriteouthandle, frames, sizeof(frames[0])*sprite.numframes);
108
109         fclose (spriteouthandle);
110         
111         spritename[0] = 0;              // clear for a new sprite
112         sprite.numframes = 0;
113 }
114
115
116 /*
117 ===============
118 Cmd_Load
119 ===============
120 */
121 void Cmd_Load (void)
122 {
123         char    *name;
124
125         GetScriptToken (false);
126
127         if (g_release)
128                 return;
129
130         name = ExpandPathAndArchive(token);
131
132         // load the image
133         printf ("loading %s\n", name);
134         TrueColorImage = LoadAnyImage (name, &byteimage, &lbmpalette, &byteimagewidth, &byteimageheight);
135
136         if (!TrueColorImage)
137         {
138 //              RemapZero (byteimage, lbmpalette, byteimagewidth, byteimageheight);
139         }
140         else
141         {
142                 if (longimage)
143                         free(longimage);
144                 longimage = (unsigned *)byteimage;
145                 longimagewidth = byteimagewidth;
146                 longimageheight = byteimageheight;
147
148                 byteimage = NULL;
149                 byteimagewidth = 0;
150                 byteimageheight = 0;
151         }
152 }
153
154
155 /*
156 ===============
157 Cmd_SpriteFrame
158 ===============
159 */
160
161 void Cmd_SpriteFrame (void)
162 {
163         int             x,y,xl,yl,xh,yh,w,h;
164         dsprframe_t             *pframe;
165         int                             ox, oy, linedelta, size;
166 //      byte                    *cropped;
167         char                    filename[1024];
168         miptex_t                *qtex;
169         miptex32_t              *qtex32;
170         unsigned        *destl, *sourcel;
171         unsigned                bufferl[256*256];
172         byte            *dest, *source;
173         byte                    buffer[256*256];
174
175         GetScriptToken (false);
176         xl = atoi (token);
177         GetScriptToken (false);
178         yl = atoi (token);
179         GetScriptToken (false);
180         w = atoi (token);
181         GetScriptToken (false);
182         h = atoi (token);
183
184         // origin offset is optional
185         if (ScriptTokenAvailable ())
186         {
187                 GetScriptToken (false);
188                 ox = atoi (token);
189                 GetScriptToken (false);
190                 oy = atoi (token);              
191         }
192         else
193         {
194                 ox = w/2;
195                 oy = h/2;
196         }
197
198         if ((xl & 0x0f) || (yl & 0x0f) || (w & 0x0f) || (h & 0x0f))
199                 Error ("Sprite dimensions not multiples of 16\n");
200
201         if ((w > 256) || (h > 256))
202                 Error ("Sprite has a dimension longer than 256");
203
204         xh = xl+w;
205         yh = yl+h;
206
207         if (sprite.numframes >= MAX_SPRFRAMES)
208                 Error ("Too many frames; increase MAX_SPRFRAMES\n");
209
210         pframe = &frames[sprite.numframes];
211         pframe->width = w;
212         pframe->height = h;
213         pframe->origin_x = ox;
214         pframe->origin_y = oy;
215
216         if (g_release)
217         {
218                 ReleaseFile (pframe->name);
219                 return;
220         }
221
222         if (TrueColorImage)
223         {
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);
226
227                 if (g_release)
228                         return; // textures are only released by $maps
229
230                 xh = xl+w;
231                 yh = yl+h;
232
233                 if (xl >= longimagewidth || xh > longimagewidth ||
234                         yl >= longimageheight || yh > longimageheight)
235                 {
236                         Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,longimagewidth,longimageheight);
237                 }
238
239                 sourcel = longimage + (yl*longimagewidth) + xl;
240                 destl = bufferl;
241                 linedelta = (longimagewidth - w);
242
243                 for (y=yl ; y<yh ; y++)
244                 {
245                         for (x=xl ; x<xh ; x++)
246                         {
247                                 *destl++ = *sourcel++;  // RGBA
248                         }
249                         sourcel += linedelta;
250                 }
251
252                 qtex32 = CreateMip32(bufferl, w, h, &size, true);
253
254                 qtex32->contents = 0;
255                 qtex32->value = 0;
256                 strcpy(qtex32->name, pframe->name);
257         //
258         // write it out
259         //
260                 printf ("writing %s\n", filename);
261                 SaveFile (filename, (byte *)qtex32, size);
262
263                 free (qtex32);
264         }
265         else
266         {
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);
269
270                 if (g_release)
271                         return; // textures are only released by $maps
272
273                 xh = xl+w;
274                 yh = yl+h;
275
276                 if (xl >= byteimagewidth || xh > byteimagewidth ||
277                         yl >= byteimageheight || yh > byteimageheight)
278                 {
279                         Error ("line %i: bad clip dimmensions (%d,%d) (%d,%d) > image (%d,%d)", scriptline, xl,yl,w,h,byteimagewidth,byteimageheight);
280                 }
281
282                 source = byteimage + yl*byteimagewidth + xl;
283                 dest = buffer;
284                 linedelta = byteimagewidth - w;
285
286                 for (y=yl ; y<yh ; y++)
287                 {
288                         for (x=xl ; x<xh ; x++)
289                         {
290                                 *dest++ = *source++;
291                         }
292                         source += linedelta;
293                 }
294
295                 qtex = CreateMip(buffer, w, h, lbmpalette, &size, true);
296
297                 qtex->flags = 0;
298                 qtex->contents = 0;
299                 qtex->value = 0;
300                 strcpy(qtex->name, pframe->name);
301         //
302         // write it out
303         //
304                 printf ("writing %s\n", filename);
305                 SaveFile (filename, (byte *)qtex, size);
306
307                 free (qtex);
308         }
309
310         sprite.numframes++;
311 }
312
313
314 /*
315 ==============
316 Cmd_SpriteName
317 ==============
318 */
319 void Cmd_SpriteName (void)
320 {
321         if (sprite.numframes)
322                 FinishSprite ();
323
324         GetScriptToken (false);
325         strcpy (spritename, token);
326         memset (&sprite, 0, sizeof(sprite));
327         memset (&frames, 0, sizeof(frames));
328 }
329
330
331 /*
332 ===============
333 Cmd_Sprdir
334 ===============
335 */
336 void Cmd_Sprdir (void)
337 {
338         char    filename[1024];
339
340         GetScriptToken (false);
341         strcpy (spr_prefix, token);
342         // create the directory if needed
343         sprintf (filename, "%sSprites", g_outputDir);
344         Q_mkdir (filename); 
345         sprintf (filename, "%sSprites/%s", g_outputDir, spr_prefix);
346         Q_mkdir (filename); 
347 }
348
349