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)
43 fseek(f, 0, SEEK_END);
45 fseek(f, pos, SEEK_SET);
55 bool LoadFile(const char *filename, byte **bufferptr)
61 f = fopen(filename, "rb");
66 length = FileLength(f);
67 buffer = new byte[length + 1];
69 fread(buffer, 1, length, f);
78 if (GDEF_ARCH_ENDIAN_BIG) {
79 std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(int));
84 float LittleFloat(float l)
86 if (GDEF_ARCH_ENDIAN_BIG) {
87 std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(float));
96 If all values are 32 bits, this can be used to swap everything
99 void SwapBlock(int *block, int sizeOfBlock)
104 for (i = 0; i < sizeOfBlock; i++) {
105 block[i] = LittleLong(block[i]);
113 Byte swaps all data in a bsp file.
116 void SwapBSPFile(void)
121 // SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
123 // shaders (don't swap the name)
124 // for ( i = 0 ; i < numShaders ; i++ ) {
125 // dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
126 // dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
130 SwapBlock((int *) dplanes, numplanes * sizeof(dplanes[0]));
133 SwapBlock((int *) dnodes, numnodes * sizeof(dnodes[0]));
136 SwapBlock((int *) dleafs, numleafs * sizeof(dleafs[0]));
139 SwapBlock((int *) dleafsurfaces, numleafsurfaces * sizeof(dleafsurfaces[0]));
142 SwapBlock((int *) dleafbrushes, numleafbrushes * sizeof(dleafbrushes[0]));
145 SwapBlock((int *) dbrushes, numbrushes * sizeof(dbrushes[0]));
148 SwapBlock((int *) dbrushsides, numbrushsides * sizeof(dbrushsides[0]));
151 ((int *) &visBytes)[0] = LittleLong(((int *) &visBytes)[0]);
152 ((int *) &visBytes)[1] = LittleLong(((int *) &visBytes)[1]);
154 // drawverts (don't swap colors )
155 for (i = 0; i < numDrawVerts; i++) {
156 drawVerts[i].lightmap[0] = LittleFloat(drawVerts[i].lightmap[0]);
157 drawVerts[i].lightmap[1] = LittleFloat(drawVerts[i].lightmap[1]);
158 drawVerts[i].st[0] = LittleFloat(drawVerts[i].st[0]);
159 drawVerts[i].st[1] = LittleFloat(drawVerts[i].st[1]);
160 drawVerts[i].xyz[0] = LittleFloat(drawVerts[i].xyz[0]);
161 drawVerts[i].xyz[1] = LittleFloat(drawVerts[i].xyz[1]);
162 drawVerts[i].xyz[2] = LittleFloat(drawVerts[i].xyz[2]);
163 drawVerts[i].normal[0] = LittleFloat(drawVerts[i].normal[0]);
164 drawVerts[i].normal[1] = LittleFloat(drawVerts[i].normal[1]);
165 drawVerts[i].normal[2] = LittleFloat(drawVerts[i].normal[2]);
169 // SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
172 SwapBlock((int *) drawSurfaces, numDrawSurfaces * sizeof(drawSurfaces[0]));
175 // for ( i = 0 ; i < numFogs ; i++ ) {
176 // dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
177 // dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
186 int CopyLump(dheader_t *header, int lump, void **dest, int size)
190 length = header->lumps[lump].filelen;
191 ofs = header->lumps[lump].fileofs;
197 *dest = new byte[length];
198 memcpy(*dest, (byte *) header + ofs, length);
200 return length / size;
208 bool LoadBSPFile(const char *filename)
212 // load the file header
213 if (!LoadFile(filename, (byte **) &header)) {
218 SwapBlock((int *) header, sizeof(*header));
220 if (header->ident != BSP_IDENT) {
221 DoMessageBox("Cant find a valid IBSP file", "Error", eMB_OK);
224 if ((header->version != Q3_BSP_VERSION) &&
225 (header->version != WOLF_BSP_VERSION)) {
226 DoMessageBox("File is incorrect version", "Error", eMB_OK);
230 numbrushsides = CopyLump(header, LUMP_BRUSHES, (void **) &dbrushsides, sizeof(dbrushside_t));
231 numbrushes = CopyLump(header, LUMP_BRUSHES, (void **) &dbrushes, sizeof(dbrush_t));
232 numplanes = CopyLump(header, LUMP_PLANES, (void **) &dplanes, sizeof(dplane_t));
233 numleafs = CopyLump(header, LUMP_LEAFS, (void **) &dleafs, sizeof(dleaf_t));
234 numnodes = CopyLump(header, LUMP_NODES, (void **) &dnodes, sizeof(dnode_t));
235 numDrawVerts = CopyLump(header, LUMP_DRAWVERTS, (void **) &drawVerts, sizeof(qdrawVert_t));
236 numDrawSurfaces = CopyLump(header, LUMP_SURFACES, (void **) &drawSurfaces, sizeof(dsurface_t));
237 numleafsurfaces = CopyLump(header, LUMP_LEAFSURFACES, (void **) &dleafsurfaces, sizeof(int));
238 numVisBytes = CopyLump(header, LUMP_VISIBILITY, (void **) &visBytes, 1);
239 numleafbrushes = CopyLump(header, LUMP_LEAFBRUSHES, (void **) &dleafbrushes, sizeof(int));
241 delete header; // everything has been copied out
270 delete dleafsurfaces;