2 #include "dialogs/dialogs-gtk.h"
16 byte *visBytes = NULL;
17 dnode_t *dnodes = NULL;
18 dplane_t *dplanes = NULL;
19 dleaf_t *dleafs = NULL;
20 qdrawVert_t *drawVerts = NULL;
21 dsurface_t *drawSurfaces = NULL;
22 int *dleafsurfaces = NULL;
23 dbrush_t *dbrushes = NULL;
24 dbrushside_t *dbrushsides = NULL;
25 int *dleafbrushes = NULL;
27 #define BSP_IDENT (('P'<<24)+('S'<<16)+('B'<<8)+'I')
28 #define Q3_BSP_VERSION 46
29 #define WOLF_BSP_VERSION 47
36 int FileLength (FILE *f)
42 fseek (f, 0, SEEK_END);
44 fseek (f, pos, SEEK_SET);
54 bool LoadFile( const char *filename, byte **bufferptr)
60 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)
76 #if defined(__BIG_ENDIAN__)
77 std::reverse(reinterpret_cast<unsigned char*>(&l), reinterpret_cast<unsigned char*>(&l) + sizeof(int));
82 float LittleFloat (float l)
84 #if defined(__BIG_ENDIAN__)
85 std::reverse(reinterpret_cast<unsigned char*>(&l), reinterpret_cast<unsigned char*>(&l) + sizeof(float));
94 If all values are 32 bits, this can be used to swap everything
97 void SwapBlock( int *block, int sizeOfBlock ) {
101 for ( i = 0 ; i < sizeOfBlock ; i++ ) {
102 block[i] = LittleLong( block[i] );
110 Byte swaps all data in a bsp file.
113 void SwapBSPFile( void ) {
117 // SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
119 // shaders (don't swap the name)
120 // for ( i = 0 ; i < numShaders ; i++ ) {
121 // dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
122 // dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
126 SwapBlock( (int *)dplanes, numplanes * sizeof( dplanes[0] ) );
129 SwapBlock( (int *)dnodes, numnodes * sizeof( dnodes[0] ) );
132 SwapBlock( (int *)dleafs, numleafs * sizeof( dleafs[0] ) );
135 SwapBlock( (int *)dleafsurfaces, numleafsurfaces * sizeof( dleafsurfaces[0] ) );
138 SwapBlock( (int *)dleafbrushes, numleafbrushes * sizeof( dleafbrushes[0] ) );
141 SwapBlock( (int *)dbrushes, numbrushes * sizeof( dbrushes[0] ) );
144 SwapBlock( (int *)dbrushsides, numbrushsides * sizeof( dbrushsides[0] ) );
147 ((int *)&visBytes)[0] = LittleLong( ((int *)&visBytes)[0] );
148 ((int *)&visBytes)[1] = LittleLong( ((int *)&visBytes)[1] );
150 // drawverts (don't swap colors )
151 for ( i = 0 ; i < numDrawVerts ; i++ ) {
152 drawVerts[i].lightmap[0] = LittleFloat( drawVerts[i].lightmap[0] );
153 drawVerts[i].lightmap[1] = LittleFloat( drawVerts[i].lightmap[1] );
154 drawVerts[i].st[0] = LittleFloat( drawVerts[i].st[0] );
155 drawVerts[i].st[1] = LittleFloat( drawVerts[i].st[1] );
156 drawVerts[i].xyz[0] = LittleFloat( drawVerts[i].xyz[0] );
157 drawVerts[i].xyz[1] = LittleFloat( drawVerts[i].xyz[1] );
158 drawVerts[i].xyz[2] = LittleFloat( drawVerts[i].xyz[2] );
159 drawVerts[i].normal[0] = LittleFloat( drawVerts[i].normal[0] );
160 drawVerts[i].normal[1] = LittleFloat( drawVerts[i].normal[1] );
161 drawVerts[i].normal[2] = LittleFloat( drawVerts[i].normal[2] );
165 // SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
168 SwapBlock( (int *)drawSurfaces, numDrawSurfaces * sizeof( drawSurfaces[0] ) );
171 // for ( i = 0 ; i < numFogs ; i++ ) {
172 // dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
173 // dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
182 int CopyLump( dheader_t *header, int lump, void **dest, int size ) {
185 length = header->lumps[lump].filelen;
186 ofs = header->lumps[lump].fileofs;
191 *dest = new byte[length];
192 memcpy( *dest, (byte *)header + ofs, length );
194 return length / size;
202 bool LoadBSPFile( const char *filename ) {
205 // load the file header
206 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
256 delete dleafsurfaces;