]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/qdata_heretic2/sprites.c
reduce more diff noise
[xonotic/netradiant.git] / tools / quake2 / qdata_heretic2 / sprites.c
1 /*
2    Copyright (C) 1999-2007 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         FILE    *spriteouthandle;
56         int i, curframe;
57         dsprite_t spritetemp;
58         char savename[1024];
59
60         if ( sprite.numframes == 0 ) {
61                 return;
62         }
63
64         if ( !strlen( spritename ) ) {
65                 Error( "Didn't name sprite file" );
66         }
67
68         sprintf( savename, "%sSprites/%s/%s.sp2", g_outputDir, spr_prefix, spritename );
69
70         if ( g_release ) {
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         char    *name;
123
124         GetScriptToken( false );
125
126         if ( g_release ) {
127                 return;
128         }
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 //              RemapZero (byteimage, lbmpalette, byteimagewidth, byteimageheight);
138         }
139         else
140         {
141                 if ( longimage ) {
142                         free( longimage );
143                 }
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         int x,y,xl,yl,xh,yh,w,h;
163         dsprframe_t     *pframe;
164         int ox, oy, linedelta, size;
165 //      byte                    *cropped;
166         char filename[1024];
167         miptex_t        *qtex;
168         miptex32_t      *qtex32;
169         unsigned        *destl, *sourcel;
170         unsigned bufferl[256 * 256];
171         byte            *dest, *source;
172         byte buffer[256 * 256];
173
174         GetScriptToken( false );
175         xl = atoi( token );
176         GetScriptToken( false );
177         yl = atoi( token );
178         GetScriptToken( false );
179         w = atoi( token );
180         GetScriptToken( false );
181         h = atoi( token );
182
183         // origin offset is optional
184         if ( ScriptTokenAvailable() ) {
185                 GetScriptToken( false );
186                 ox = atoi( token );
187                 GetScriptToken( false );
188                 oy = atoi( token );
189         }
190         else
191         {
192                 ox = w / 2;
193                 oy = h / 2;
194         }
195
196         if ( ( xl & 0x0f ) || ( yl & 0x0f ) || ( w & 0x0f ) || ( h & 0x0f ) ) {
197                 Error( "Sprite dimensions not multiples of 16\n" );
198         }
199
200         if ( ( w > 256 ) || ( h > 256 ) ) {
201                 Error( "Sprite has a dimension longer than 256" );
202         }
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
211         pframe = &frames[sprite.numframes];
212         pframe->width = w;
213         pframe->height = h;
214         pframe->origin_x = ox;
215         pframe->origin_y = oy;
216
217         if ( g_release ) {
218                 ReleaseFile( pframe->name );
219                 return;
220         }
221
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 );
225
226                 if ( g_release ) {
227                         return; // textures are only released by $maps
228
229                 }
230                 xh = xl + w;
231                 yh = yl + h;
232
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 );
236                 }
237
238                 sourcel = longimage + ( yl * longimagewidth ) + xl;
239                 destl = bufferl;
240                 linedelta = ( longimagewidth - w );
241
242                 for ( y = yl ; y < yh ; y++ )
243                 {
244                         for ( x = xl ; x < xh ; x++ )
245                         {
246                                 *destl++ = *sourcel++;  // RGBA
247                         }
248                         sourcel += linedelta;
249                 }
250
251                 qtex32 = CreateMip32( bufferl, w, h, &size, true );
252
253                 qtex32->contents = 0;
254                 qtex32->value = 0;
255                 strcpy( qtex32->name, pframe->name );
256                 //
257                 // write it out
258                 //
259                 printf( "writing %s\n", filename );
260                 SaveFile( filename, (byte *)qtex32, size );
261
262                 free( qtex32 );
263         }
264         else
265         {
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 );
268
269                 if ( g_release ) {
270                         return; // textures are only released by $maps
271
272                 }
273                 xh = xl + w;
274                 yh = yl + h;
275
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 );
279                 }
280
281                 source = byteimage + yl * byteimagewidth + xl;
282                 dest = buffer;
283                 linedelta = byteimagewidth - w;
284
285                 for ( y = yl ; y < yh ; y++ )
286                 {
287                         for ( x = xl ; x < xh ; x++ )
288                         {
289                                 *dest++ = *source++;
290                         }
291                         source += linedelta;
292                 }
293
294                 qtex = CreateMip( buffer, w, h, lbmpalette, &size, true );
295
296                 qtex->flags = 0;
297                 qtex->contents = 0;
298                 qtex->value = 0;
299                 strcpy( qtex->name, pframe->name );
300                 //
301                 // write it out
302                 //
303                 printf( "writing %s\n", filename );
304                 SaveFile( filename, (byte *)qtex, size );
305
306                 free( qtex );
307         }
308
309         sprite.numframes++;
310 }
311
312
313 /*
314    ==============
315    Cmd_SpriteName
316    ==============
317  */
318 void Cmd_SpriteName( void ){
319         if ( sprite.numframes ) {
320                 FinishSprite();
321         }
322
323         GetScriptToken( false );
324         strcpy( spritename, token );
325         memset( &sprite, 0, sizeof( sprite ) );
326         memset( &frames, 0, sizeof( frames ) );
327 }
328
329
330 /*
331    ===============
332    Cmd_Sprdir
333    ===============
334  */
335 void Cmd_Sprdir( void ){
336         char filename[1024];
337
338         GetScriptToken( false );
339         strcpy( spr_prefix, token );
340         // create the directory if needed
341         sprintf( filename, "%sSprites", g_outputDir );
342         Q_mkdir( filename );
343         sprintf( filename, "%sSprites/%s", g_outputDir, spr_prefix );
344         Q_mkdir( filename );
345 }