]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/bsploader.cpp
Merge branch 'NateEag-master-patch-12920' into 'master'
[xonotic/netradiant.git] / contrib / bobtoolz / bsploader.cpp
1 #include <globaldefs.h>
2 #include "bsploader.h"
3 #include "dialogs/dialogs-gtk.h"
4 #include "cmdlib.h"
5
6 int numnodes;
7 int numplanes;
8 int numleafs;
9 int numleafsurfaces;
10 int numVisBytes;
11 int numDrawVerts;
12 int numDrawSurfaces;
13 int numbrushes;
14 int numbrushsides;
15 int numleafbrushes;
16
17 byte                *visBytes =           NULL;
18 dnode_t           *dnodes =             NULL;
19 dplane_t          *dplanes =              NULL;
20 dleaf_t           *dleafs =             NULL;
21 qdrawVert_t   *drawVerts =        NULL;
22 dsurface_t    *drawSurfaces =       NULL;
23 int                 *dleafsurfaces =    NULL;
24 dbrush_t          *dbrushes =             NULL;
25 dbrushside_t    *dbrushsides =      NULL;
26 int                 *dleafbrushes =     NULL;
27
28 const int BSP_IDENT = ( ( 'P' << 24 ) + ( 'S' << 16 ) + ( 'B' << 8 ) + 'I' );
29 const int Q3_BSP_VERSION = 46;
30 const int WOLF_BSP_VERSION = 47;
31
32 /*
33    ================
34    FileLength
35    ================
36  */
37 int FileLength( FILE *f ){
38         int pos;
39         int end;
40
41         pos = ftell( f );
42         fseek( f, 0, SEEK_END );
43         end = ftell( f );
44         fseek( f, pos, SEEK_SET );
45
46         return end;
47 }
48
49 /*
50    ==============
51    LoadFile
52    ==============
53  */
54 bool    LoadFile( const char *filename, byte **bufferptr ){
55         FILE    *f;
56         int length;
57         byte    *buffer;
58
59         f = fopen( filename, "rb" );
60         if ( !f ) {
61                 return false;
62         }
63
64         length = FileLength( f );
65         buffer = new byte[length + 1];
66         buffer[length] = 0;
67         fread( buffer, 1, length, f );
68         fclose( f );
69
70         *bufferptr = buffer;
71         return true;
72 }
73
74 int    LittleLong( int l ){
75         if (GDEF_ARCH_ENDIAN_BIG) {
76                 std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(int));
77         }
78         return l;
79 }
80
81 float   LittleFloat( float l ){
82         if (GDEF_ARCH_ENDIAN_BIG) {
83                 std::reverse( reinterpret_cast<unsigned char*>( &l ), reinterpret_cast<unsigned char*>( &l ) + sizeof( float ) );
84         }
85         return l;
86 }
87
88 /*
89    =============
90    SwapBlock
91
92    If all values are 32 bits, this can be used to swap everything
93    =============
94  */
95 void SwapBlock( int *block, int sizeOfBlock ) {
96         int i;
97
98         sizeOfBlock >>= 2;
99         for ( i = 0 ; i < sizeOfBlock ; i++ ) {
100                 block[i] = LittleLong( block[i] );
101         }
102 }
103
104 /*
105    =============
106    SwapBSPFile
107
108    Byte swaps all data in a bsp file.
109    =============
110  */
111 void SwapBSPFile( void ) {
112         int i;
113
114         // models
115 //      SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
116
117         // shaders (don't swap the name)
118 //      for ( i = 0 ; i < numShaders ; i++ ) {
119 //              dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
120 //              dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
121 //      }
122
123         // planes
124         SwapBlock( (int *)dplanes, numplanes * sizeof( dplanes[0] ) );
125
126         // nodes
127         SwapBlock( (int *)dnodes, numnodes * sizeof( dnodes[0] ) );
128
129         // leafs
130         SwapBlock( (int *)dleafs, numleafs * sizeof( dleafs[0] ) );
131
132         // leaffaces
133         SwapBlock( (int *)dleafsurfaces, numleafsurfaces * sizeof( dleafsurfaces[0] ) );
134
135         // leafbrushes
136         SwapBlock( (int *)dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ) );
137
138         // brushes
139         SwapBlock( (int *)dbrushes, numbrushes * sizeof( dbrushes[0] ) );
140
141         // brushsides
142         SwapBlock( (int *)dbrushsides, numbrushsides * sizeof( dbrushsides[0] ) );
143
144         // vis
145         ( (int *)&visBytes )[0] = LittleLong( ( (int *)&visBytes )[0] );
146         ( (int *)&visBytes )[1] = LittleLong( ( (int *)&visBytes )[1] );
147
148         // drawverts (don't swap colors )
149         for ( i = 0 ; i < numDrawVerts ; i++ ) {
150                 drawVerts[i].lightmap[0] = LittleFloat( drawVerts[i].lightmap[0] );
151                 drawVerts[i].lightmap[1] = LittleFloat( drawVerts[i].lightmap[1] );
152                 drawVerts[i].st[0] = LittleFloat( drawVerts[i].st[0] );
153                 drawVerts[i].st[1] = LittleFloat( drawVerts[i].st[1] );
154                 drawVerts[i].xyz[0] = LittleFloat( drawVerts[i].xyz[0] );
155                 drawVerts[i].xyz[1] = LittleFloat( drawVerts[i].xyz[1] );
156                 drawVerts[i].xyz[2] = LittleFloat( drawVerts[i].xyz[2] );
157                 drawVerts[i].normal[0] = LittleFloat( drawVerts[i].normal[0] );
158                 drawVerts[i].normal[1] = LittleFloat( drawVerts[i].normal[1] );
159                 drawVerts[i].normal[2] = LittleFloat( drawVerts[i].normal[2] );
160         }
161
162         // drawindexes
163 //      SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
164
165         // drawsurfs
166         SwapBlock( (int *)drawSurfaces, numDrawSurfaces * sizeof( drawSurfaces[0] ) );
167
168         // fogs
169 //      for ( i = 0 ; i < numFogs ; i++ ) {
170 //              dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
171 //              dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
172 //      }
173 }
174
175 /*
176    =============
177    CopyLump
178    =============
179  */
180 int CopyLump( dheader_t *header, int lump, void **dest, int size ) {
181         int length, ofs;
182
183         length = header->lumps[lump].filelen;
184         ofs = header->lumps[lump].fileofs;
185
186         if ( length == 0 ) {
187                 return 0;
188         }
189
190         *dest = new byte[length];
191         memcpy( *dest, (byte *)header + ofs, length );
192
193         return length / size;
194 }
195
196 /*
197    =============
198    LoadBSPFile
199    =============
200  */
201 bool    LoadBSPFile( const char *filename ) {
202         dheader_t   *header;
203
204         // load the file header
205         if ( !LoadFile( filename, (byte **)&header ) ) {
206                 return false;
207         }
208
209         // swap the header
210         SwapBlock( (int *)header, sizeof( *header ) );
211
212         if ( header->ident != BSP_IDENT ) {
213                 DoMessageBox( "Cant find a valid IBSP file", "Error", eMB_OK );
214                 return false;
215         }
216         if ( ( header->version != Q3_BSP_VERSION ) &&
217                  ( header->version != WOLF_BSP_VERSION ) ) {
218                 DoMessageBox( "File is incorrect version", "Error", eMB_OK );
219                 return false;
220         }
221
222         numbrushsides =     CopyLump( header, LUMP_BRUSHES,         (void**)&dbrushsides,   sizeof( dbrushside_t ) );
223         numbrushes =        CopyLump( header, LUMP_BRUSHES,         (void**)&dbrushes,      sizeof( dbrush_t ) );
224         numplanes =         CopyLump( header, LUMP_PLANES,          (void**)&dplanes,       sizeof( dplane_t ) );
225         numleafs =          CopyLump( header, LUMP_LEAFS,           (void**)&dleafs,        sizeof( dleaf_t ) );
226         numnodes =          CopyLump( header, LUMP_NODES,           (void**)&dnodes,        sizeof( dnode_t ) );
227         numDrawVerts =      CopyLump( header, LUMP_DRAWVERTS,       (void**)&drawVerts,     sizeof( qdrawVert_t ) );
228         numDrawSurfaces =   CopyLump( header, LUMP_SURFACES,        (void**)&drawSurfaces,  sizeof( dsurface_t ) );
229         numleafsurfaces =   CopyLump( header, LUMP_LEAFSURFACES,    (void**)&dleafsurfaces, sizeof( int ) );
230         numVisBytes =       CopyLump( header, LUMP_VISIBILITY,      (void**)&visBytes,      1 );
231         numleafbrushes =    CopyLump( header, LUMP_LEAFBRUSHES,     (void**)&dleafbrushes,  sizeof( int ) );
232
233         delete header;      // everything has been copied out
234
235         // swap everything
236         SwapBSPFile();
237
238         return true;
239 }
240
241 void FreeBSPData(){
242         if ( visBytes ) {
243                 delete visBytes;
244         }
245         if ( dnodes ) {
246                 delete dnodes;
247         }
248         if ( dplanes ) {
249                 delete dplanes;
250         }
251         if ( dleafs ) {
252                 delete dleafs;
253         }
254         if ( drawVerts ) {
255                 delete drawVerts;
256         }
257         if ( drawSurfaces ) {
258                 delete drawSurfaces;
259         }
260         if ( dleafsurfaces ) {
261                 delete dleafsurfaces;
262         }
263         if ( dleafbrushes ) {
264                 delete dleafbrushes;
265         }
266         if ( dbrushes ) {
267                 delete dbrushes;
268         }
269         if ( dbrushsides ) {
270                 delete dbrushsides;
271         }
272 }