]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/heretic2/h2data/sprites.c
h2data: use static for homonyms
[xonotic/netradiant.git] / tools / heretic2 / h2data / 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 static dsprframe_t frames[MAX_SPRFRAMES];
29
30 char spritename[1024];
31
32
33 void FinishSprite( void );
34 void Cmd_Spritename( void );
35
36 char spr_prefix[1024];
37
38 extern char        *g_outputDir;
39
40
41 /*
42    ==============
43    FinishSprite
44    ==============
45  */
46 void FinishSprite( void ){
47         FILE    *spriteouthandle;
48         int i, curframe;
49         dsprite_t spritetemp;
50         char savename[1024];
51
52         if ( sprite.numframes == 0 ) {
53                 return;
54         }
55
56         if ( !strlen( spritename ) ) {
57                 Error( "Didn't name sprite file" );
58         }
59
60         sprintf( savename, "%sSprites/%s/%s.sp2", g_outputDir, spr_prefix, spritename );
61
62         if ( g_release ) {
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         char    *name;
115
116         GetScriptToken( false );
117
118         if ( g_release ) {
119                 return;
120         }
121
122         name = ExpandPathAndArchive( token );
123
124         // load the image
125         printf( "loading %s\n", name );
126         TrueColorImage = LoadAnyImage( name, &byteimage, &lbmpalette, &byteimagewidth, &byteimageheight );
127
128         if ( !TrueColorImage ) {
129 //              RemapZero (byteimage, lbmpalette, byteimagewidth, byteimageheight);
130         }
131         else
132         {
133                 if ( longimage ) {
134                         free( longimage );
135                 }
136                 longimage = (unsigned *)byteimage;
137                 longimagewidth = byteimagewidth;
138                 longimageheight = byteimageheight;
139
140                 byteimage = NULL;
141                 byteimagewidth = 0;
142                 byteimageheight = 0;
143         }
144 }
145
146
147 /*
148    ===============
149    Cmd_SpriteFrame
150    ===============
151  */
152
153 void Cmd_SpriteFrame( void ){
154         int x,y,xl,yl,xh,yh,w,h;
155         dsprframe_t     *pframe;
156         int ox, oy, linedelta, size;
157 //      byte                    *cropped;
158         char filename[1024];
159         miptex_t        *qtex;
160         miptex32_t      *qtex32;
161         unsigned        *destl, *sourcel;
162         unsigned bufferl[256 * 256];
163         byte            *dest, *source;
164         byte buffer[256 * 256];
165
166         GetScriptToken( false );
167         xl = atoi( token );
168         GetScriptToken( false );
169         yl = atoi( token );
170         GetScriptToken( false );
171         w = atoi( token );
172         GetScriptToken( false );
173         h = atoi( token );
174
175         // origin offset is optional
176         if ( ScriptTokenAvailable() ) {
177                 GetScriptToken( false );
178                 ox = atoi( token );
179                 GetScriptToken( false );
180                 oy = atoi( token );
181         }
182         else
183         {
184                 ox = w / 2;
185                 oy = h / 2;
186         }
187
188         if ( ( xl & 0x0f ) || ( yl & 0x0f ) || ( w & 0x0f ) || ( h & 0x0f ) ) {
189                 Error( "Sprite dimensions not multiples of 16\n" );
190         }
191
192         if ( ( w > 256 ) || ( h > 256 ) ) {
193                 Error( "Sprite has a dimension longer than 256" );
194         }
195
196         xh = xl + w;
197         yh = yl + h;
198
199         if ( sprite.numframes >= MAX_SPRFRAMES ) {
200                 Error( "Too many frames; increase MAX_SPRFRAMES\n" );
201         }
202
203         pframe = &frames[sprite.numframes];
204         pframe->width = w;
205         pframe->height = h;
206         pframe->origin_x = ox;
207         pframe->origin_y = oy;
208
209         if ( g_release ) {
210                 ReleaseFile( pframe->name );
211                 return;
212         }
213
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 );
217
218                 if ( g_release ) {
219                         return; // textures are only released by $maps
220
221                 }
222                 xh = xl + w;
223                 yh = yl + h;
224
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 );
228                 }
229
230                 sourcel = longimage + ( yl * longimagewidth ) + xl;
231                 destl = bufferl;
232                 linedelta = ( longimagewidth - w );
233
234                 for ( y = yl ; y < yh ; y++ )
235                 {
236                         for ( x = xl ; x < xh ; x++ )
237                         {
238                                 *destl++ = *sourcel++;  // RGBA
239                         }
240                         sourcel += linedelta;
241                 }
242
243                 qtex32 = CreateMip32( bufferl, w, h, &size, true );
244
245                 qtex32->contents = 0;
246                 qtex32->value = 0;
247                 strcpy( qtex32->name, pframe->name );
248                 //
249                 // write it out
250                 //
251                 printf( "writing %s\n", filename );
252                 SaveFile( filename, (byte *)qtex32, size );
253
254                 free( qtex32 );
255         }
256         else
257         {
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 );
260
261                 if ( g_release ) {
262                         return; // textures are only released by $maps
263
264                 }
265                 xh = xl + w;
266                 yh = yl + h;
267
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 );
271                 }
272
273                 source = byteimage + yl * byteimagewidth + xl;
274                 dest = buffer;
275                 linedelta = byteimagewidth - w;
276
277                 for ( y = yl ; y < yh ; y++ )
278                 {
279                         for ( x = xl ; x < xh ; x++ )
280                         {
281                                 *dest++ = *source++;
282                         }
283                         source += linedelta;
284                 }
285
286                 qtex = CreateMip( buffer, w, h, lbmpalette, &size, true );
287
288                 qtex->flags = 0;
289                 qtex->contents = 0;
290                 qtex->value = 0;
291                 strcpy( qtex->name, pframe->name );
292                 //
293                 // write it out
294                 //
295                 printf( "writing %s\n", filename );
296                 SaveFile( filename, (byte *)qtex, size );
297
298                 free( qtex );
299         }
300
301         sprite.numframes++;
302 }
303
304
305 /*
306    ==============
307    Cmd_SpriteName
308    ==============
309  */
310 void Cmd_SpriteName( void ){
311         if ( sprite.numframes ) {
312                 FinishSprite();
313         }
314
315         GetScriptToken( false );
316         strcpy( spritename, token );
317         memset( &sprite, 0, sizeof( sprite ) );
318         memset( &frames, 0, sizeof( frames ) );
319 }
320
321
322 /*
323    ===============
324    Cmd_Sprdir
325    ===============
326  */
327 void Cmd_Sprdir( void ){
328         char filename[1024];
329
330         GetScriptToken( false );
331         strcpy( spr_prefix, token );
332         // create the directory if needed
333         sprintf( filename, "%sSprites", g_outputDir );
334         Q_mkdir( filename );
335         sprintf( filename, "%sSprites/%s", g_outputDir, spr_prefix );
336         Q_mkdir( filename );
337 }