1 #include <globaldefs.h>
4 #include "dialogs/dialogs-gtk.h"
18 byte *visBytes = NULL;
19 dnode_t *dnodes = NULL;
20 dplane_t *dplanes = NULL;
21 dleaf_t *dleafs = NULL;
22 qdrawVert_t *drawVerts = NULL;
23 dsurface_t *drawSurfaces = NULL;
24 int *dleafsurfaces = NULL;
25 dbrush_t *dbrushes = NULL;
26 dbrushside_t *dbrushsides = NULL;
27 int *dleafbrushes = NULL;
29 const int BSP_IDENT = (('P' << 24) + ('S' << 16) + ('B' << 8) + 'I');
30 const int Q3_BSP_VERSION = 46;
31 const int WOLF_BSP_VERSION = 47;
38 int FileLength(FILE *f)
44 fseek(f, 0, SEEK_END);
46 fseek(f, pos, SEEK_SET);
56 bool LoadFile(const char *filename, byte **bufferptr)
62 f = fopen(filename, "rb");
67 length = FileLength(f);
68 buffer = new byte[length + 1];
70 assert(fread(buffer, 1, length, f));
79 if (GDEF_ARCH_ENDIAN_BIG) {
80 std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(int));
85 float LittleFloat(float l)
87 if (GDEF_ARCH_ENDIAN_BIG) {
88 std::reverse(reinterpret_cast<unsigned char *>( &l ), reinterpret_cast<unsigned char *>( &l ) + sizeof(float));
97 If all values are 32 bits, this can be used to swap everything
100 void SwapBlock(int *block, int sizeOfBlock)
105 for (i = 0; i < sizeOfBlock; i++) {
106 block[i] = LittleLong(block[i]);
114 Byte swaps all data in a bsp file.
117 void SwapBSPFile(void)
122 // SwapBlock( (int *)dmodels, nummodels * sizeof( dmodels[0] ) );
124 // shaders (don't swap the name)
125 // for ( i = 0 ; i < numShaders ; i++ ) {
126 // dshaders[i].contentFlags = LittleLong( dshaders[i].contentFlags );
127 // dshaders[i].surfaceFlags = LittleLong( dshaders[i].surfaceFlags );
131 SwapBlock((int *) dplanes, numplanes * sizeof(dplanes[0]));
134 SwapBlock((int *) dnodes, numnodes * sizeof(dnodes[0]));
137 SwapBlock((int *) dleafs, numleafs * sizeof(dleafs[0]));
140 SwapBlock((int *) dleafsurfaces, numleafsurfaces * sizeof(dleafsurfaces[0]));
143 SwapBlock((int *) dleafbrushes, numleafbrushes * sizeof(dleafbrushes[0]));
146 SwapBlock((int *) dbrushes, numbrushes * sizeof(dbrushes[0]));
149 SwapBlock((int *) dbrushsides, numbrushsides * sizeof(dbrushsides[0]));
152 ((int *) &visBytes)[0] = LittleLong(((int *) &visBytes)[0]);
153 ((int *) &visBytes)[1] = LittleLong(((int *) &visBytes)[1]);
155 // drawverts (don't swap colors )
156 for (i = 0; i < numDrawVerts; i++) {
157 drawVerts[i].lightmap[0] = LittleFloat(drawVerts[i].lightmap[0]);
158 drawVerts[i].lightmap[1] = LittleFloat(drawVerts[i].lightmap[1]);
159 drawVerts[i].st[0] = LittleFloat(drawVerts[i].st[0]);
160 drawVerts[i].st[1] = LittleFloat(drawVerts[i].st[1]);
161 drawVerts[i].xyz[0] = LittleFloat(drawVerts[i].xyz[0]);
162 drawVerts[i].xyz[1] = LittleFloat(drawVerts[i].xyz[1]);
163 drawVerts[i].xyz[2] = LittleFloat(drawVerts[i].xyz[2]);
164 drawVerts[i].normal[0] = LittleFloat(drawVerts[i].normal[0]);
165 drawVerts[i].normal[1] = LittleFloat(drawVerts[i].normal[1]);
166 drawVerts[i].normal[2] = LittleFloat(drawVerts[i].normal[2]);
170 // SwapBlock( (int *)drawIndexes, numDrawIndexes * sizeof( drawIndexes[0] ) );
173 SwapBlock((int *) drawSurfaces, numDrawSurfaces * sizeof(drawSurfaces[0]));
176 // for ( i = 0 ; i < numFogs ; i++ ) {
177 // dfogs[i].brushNum = LittleLong( dfogs[i].brushNum );
178 // dfogs[i].visibleSide = LittleLong( dfogs[i].visibleSide );
187 int CopyLump(dheader_t *header, int lump, void **dest, int size)
191 length = header->lumps[lump].filelen;
192 ofs = header->lumps[lump].fileofs;
198 *dest = new byte[length];
199 memcpy(*dest, (byte *) header + ofs, length);
201 return length / size;
209 bool LoadBSPFile(const char *filename)
213 // load the file header
214 if (!LoadFile(filename, (byte **) &header)) {
219 SwapBlock((int *) header, sizeof(*header));
221 if (header->ident != BSP_IDENT) {
222 DoMessageBox("Cant find a valid IBSP file", "Error", eMB_OK);
225 if ((header->version != Q3_BSP_VERSION) &&
226 (header->version != WOLF_BSP_VERSION)) {
227 DoMessageBox("File is incorrect version", "Error", eMB_OK);
231 numbrushsides = CopyLump(header, LUMP_BRUSHES, (void **) &dbrushsides, sizeof(dbrushside_t));
232 numbrushes = CopyLump(header, LUMP_BRUSHES, (void **) &dbrushes, sizeof(dbrush_t));
233 numplanes = CopyLump(header, LUMP_PLANES, (void **) &dplanes, sizeof(dplane_t));
234 numleafs = CopyLump(header, LUMP_LEAFS, (void **) &dleafs, sizeof(dleaf_t));
235 numnodes = CopyLump(header, LUMP_NODES, (void **) &dnodes, sizeof(dnode_t));
236 numDrawVerts = CopyLump(header, LUMP_DRAWVERTS, (void **) &drawVerts, sizeof(qdrawVert_t));
237 numDrawSurfaces = CopyLump(header, LUMP_SURFACES, (void **) &drawSurfaces, sizeof(dsurface_t));
238 numleafsurfaces = CopyLump(header, LUMP_LEAFSURFACES, (void **) &dleafsurfaces, sizeof(int));
239 numVisBytes = CopyLump(header, LUMP_VISIBILITY, (void **) &visBytes, 1);
240 numleafbrushes = CopyLump(header, LUMP_LEAFBRUSHES, (void **) &dleafbrushes, sizeof(int));
242 delete header; // everything has been copied out
271 delete dleafsurfaces;