]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/qdata/sprites.c
Merge branch 'master' into divVerent/farplanedist-sky-fix
[xonotic/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         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, "%s%s.sp2", gamedir, 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         GetToken( 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         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         int y,xl,yl,xh,yh,w,h;
140         dsprframe_t     *pframe;
141         int ox, oy;
142         byte            *cropped;
143         char savename[1024];
144
145         GetToken( false );
146         xl = atoi( token );
147         GetToken( false );
148         yl = atoi( token );
149         GetToken( false );
150         w = atoi( token );
151         GetToken( false );
152         h = atoi( token );
153
154         // origin offset is optional
155         if ( TokenAvailable() ) {
156                 GetToken( false );
157                 ox = atoi( token );
158                 GetToken( false );
159                 oy = atoi( token );
160         }
161         else
162         {
163                 ox = w / 2;
164                 oy = h / 2;
165         }
166
167         if ( ( xl & 0x07 ) || ( yl & 0x07 ) || ( w & 0x07 ) || ( h & 0x07 ) ) {
168                 Error( "Sprite dimensions not multiples of 8\n" );
169         }
170
171         if ( ( w > 256 ) || ( h > 256 ) ) {
172                 Error( "Sprite has a dimension longer than 256" );
173         }
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
182         pframe = &frames[sprite.numframes];
183         pframe->width = w;
184         pframe->height = h;
185         pframe->origin_x = ox;
186         pframe->origin_y = oy;
187         sprintf( pframe->name, "%s_%i.pcx", spritename, sprite.numframes );
188         sprintf( savename, "%s%s_%i.pcx", gamedir, spritename, sprite.numframes );
189         sprite.numframes++;
190
191         if ( g_release ) {
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         if ( sprite.numframes ) {
220                 FinishSprite();
221         }
222
223         GetToken( false );
224         strcpy( spritename, token );
225         memset( &sprite, 0, sizeof( sprite ) );
226         memset( &frames, 0, sizeof( frames ) );
227 }