1 #include <globaldefs.h>
3 #include "dialogs/dialogs-gtk.h"
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;
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;
37 int FileLength( FILE *f ){
42 fseek( f, 0, SEEK_END );
44 fseek( f, pos, SEEK_SET );
54 bool LoadFile( const char *filename, byte **bufferptr ){
59 f = fopen( filename, "rb" );
64 length = FileLength( f );
65 buffer = new byte[length + 1];
67 fread( buffer, 1, length, f );
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));
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 ) );
92 If all values are 32 bits, this can be used to swap everything
95 void SwapBlock( int *block, int sizeOfBlock ) {
99 for ( i = 0 ; i < sizeOfBlock ; i++ ) {
100 block[i] = LittleLong( block[i] );
108 Byte swaps all data in a bsp file.
111 void SwapBSPFile( void ) {
115 // SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
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 );
124 SwapBlock( (int *)dplanes, numplanes * sizeof( dplanes[0] ) );
127 SwapBlock( (int *)dnodes, numnodes * sizeof( dnodes[0] ) );
130 SwapBlock( (int *)dleafs, numleafs * sizeof( dleafs[0] ) );
133 SwapBlock( (int *)dleafsurfaces, numleafsurfaces * sizeof( dleafsurfaces[0] ) );
136 SwapBlock( (int *)dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ) );
139 SwapBlock( (int *)dbrushes, numbrushes * sizeof( dbrushes[0] ) );
142 SwapBlock( (int *)dbrushsides, numbrushsides * sizeof( dbrushsides[0] ) );
145 ( (int *)&visBytes )[0] = LittleLong( ( (int *)&visBytes )[0] );
146 ( (int *)&visBytes )[1] = LittleLong( ( (int *)&visBytes )[1] );
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] );
163 // SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
166 SwapBlock( (int *)drawSurfaces, numDrawSurfaces * sizeof( drawSurfaces[0] ) );
169 // for ( i = 0 ; i < numFogs ; i++ ) {
170 // dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
171 // dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
180 int CopyLump( dheader_t *header, int lump, void **dest, int size ) {
183 length = header->lumps[lump].filelen;
184 ofs = header->lumps[lump].fileofs;
190 *dest = new byte[length];
191 memcpy( *dest, (byte *)header + ofs, length );
193 return length / size;
201 bool LoadBSPFile( const char *filename ) {
204 // load the file header
205 if ( !LoadFile( filename, (byte **)&header ) ) {
210 SwapBlock( (int *)header, sizeof( *header ) );
212 if ( header->ident != BSP_IDENT ) {
213 DoMessageBox( "Cant find a valid IBSP file", "Error", eMB_OK );
216 if ( ( header->version != Q3_BSP_VERSION ) &&
217 ( header->version != WOLF_BSP_VERSION ) ) {
218 DoMessageBox( "File is incorrect version", "Error", eMB_OK );
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 ) );
233 delete header; // everything has been copied out
257 if ( drawSurfaces ) {
260 if ( dleafsurfaces ) {
261 delete dleafsurfaces;
263 if ( dleafbrushes ) {