#include "cmdlib.h" #include "mathlib.h" #include "bspfile.h" //============================================================================= int nummodels; dmodel_t dmodels[max_map_models]; int visdatasize; byte dvisdata[max_map_visibility]; int lightdatasize; byte dlightdata[max_map_lighting]; int texdatasize; byte dtexdata[max_map_miptex]; // (dmiptexlump_t) int entdatasize; char dentdata[max_map_entstring]; int numleafs; dleaf_t dleafs[max_map_leafs]; int numplanes; dplane_t dplanes[max_map_planes]; int numvertexes; dvertex_t dvertexes[max_map_verts]; int numnodes; dnode_t dnodes[max_map_nodes]; int numtexinfo; texinfo_t texinfo[max_map_texinfo]; int numfaces; dface_t dfaces[max_map_faces]; int numclipnodes; dclipnode_t dclipnodes[max_map_clipnodes]; int numedges; dedge_t dedges[max_map_edges]; int nummarksurfaces; unsigned short dmarksurfaces[max_map_marksurfaces]; int numsurfedges; int dsurfedges[max_map_surfedges]; //============================================================================= /* ============= swapbspfile byte swaps all data in a bsp file. ============= */ void swapbspfile (qboolean todisk) { int i, j, c; dmodel_t *d; dmiptexlump_t *mtl; // models for (i=0 ; iheadnode[j] = littlelong (d->headnode[j]); d->visleafs = littlelong (d->visleafs); d->firstface = littlelong (d->firstface); d->numfaces = littlelong (d->numfaces); for (j=0 ; j<3 ; j++) { d->mins[j] = littlefloat(d->mins[j]); d->maxs[j] = littlefloat(d->maxs[j]); d->origin[j] = littlefloat(d->origin[j]); } } // // vertexes // for (i=0 ; inummiptex; else c = littlelong(mtl->nummiptex); mtl->nummiptex = littlelong (mtl->nummiptex); for (i=0 ; idataofs[i] = littlelong(mtl->dataofs[i]); } // // marksurfaces // for (i=0 ; ilumps[lump].filelen; ofs = header->lumps[lump].fileofs; if (length % size) error ("loadbspfile: odd lump size"); memcpy (dest, (byte *)header + ofs, length); return length / size; } /* ============= loadbspfile ============= */ void loadbspfile (char *filename) { int i; // // load the file header // loadfile (filename, (void **)&header); // swap the header for (i=0 ; i< sizeof(dheader_t)/4 ; i++) ((int *)header)[i] = littlelong ( ((int *)header)[i]); if (header->version != bspversion) error ("%s is version %i, not %i", filename, i, bspversion); nummodels = copylump (lump_models, dmodels, sizeof(dmodel_t)); numvertexes = copylump (lump_vertexes, dvertexes, sizeof(dvertex_t)); numplanes = copylump (lump_planes, dplanes, sizeof(dplane_t)); numleafs = copylump (lump_leafs, dleafs, sizeof(dleaf_t)); numnodes = copylump (lump_nodes, dnodes, sizeof(dnode_t)); numtexinfo = copylump (lump_texinfo, texinfo, sizeof(texinfo_t)); numclipnodes = copylump (lump_clipnodes, dclipnodes, sizeof(dclipnode_t)); numfaces = copylump (lump_faces, dfaces, sizeof(dface_t)); nummarksurfaces = copylump (lump_marksurfaces, dmarksurfaces, sizeof(dmarksurfaces[0])); numsurfedges = copylump (lump_surfedges, dsurfedges, sizeof(dsurfedges[0])); numedges = copylump (lump_edges, dedges, sizeof(dedge_t)); texdatasize = copylump (lump_textures, dtexdata, 1); visdatasize = copylump (lump_visibility, dvisdata, 1); lightdatasize = copylump (lump_lighting, dlightdata, 1); entdatasize = copylump (lump_entities, dentdata, 1); free (header); // everything has been copied out // // swap everything // swapbspfile (false); } //============================================================================ file *wadfile; dheader_t outheader; void addlump (int lumpnum, void *data, int len) { lump_t *lump; lump = &header->lumps[lumpnum]; lump->fileofs = littlelong( ftell(wadfile) ); lump->filelen = littlelong(len); safewrite (wadfile, data, (len+3)&~3); } /* ============= writebspfile swaps the bsp file in place, so it should not be referenced again ============= */ void writebspfile (char *filename) { header = &outheader; memset (header, 0, sizeof(dheader_t)); swapbspfile (true); header->version = littlelong (bspversion); wadfile = safeopenwrite (filename); safewrite (wadfile, header, sizeof(dheader_t)); // overwritten later addlump (lump_planes, dplanes, numplanes*sizeof(dplane_t)); addlump (lump_leafs, dleafs, numleafs*sizeof(dleaf_t)); addlump (lump_vertexes, dvertexes, numvertexes*sizeof(dvertex_t)); addlump (lump_nodes, dnodes, numnodes*sizeof(dnode_t)); addlump (lump_texinfo, texinfo, numtexinfo*sizeof(texinfo_t)); addlump (lump_faces, dfaces, numfaces*sizeof(dface_t)); addlump (lump_clipnodes, dclipnodes, numclipnodes*sizeof(dclipnode_t)); addlump (lump_marksurfaces, dmarksurfaces, nummarksurfaces*sizeof(dmarksurfaces[0])); addlump (lump_surfedges, dsurfedges, numsurfedges*sizeof(dsurfedges[0])); addlump (lump_edges, dedges, numedges*sizeof(dedge_t)); addlump (lump_models, dmodels, nummodels*sizeof(dmodel_t)); addlump (lump_lighting, dlightdata, lightdatasize); addlump (lump_visibility, dvisdata, visdatasize); addlump (lump_entities, dentdata, entdatasize); addlump (lump_textures, dtexdata, texdatasize); fseek (wadfile, 0, seek_set); safewrite (wadfile, header, sizeof(dheader_t)); fclose (wadfile); } //============================================================================ /* ============= printbspfilesizes dumps info about current file ============= */ void printbspfilesizes (void) { printf ("%5i planes %6i\n" ,numplanes, (int)(numplanes*sizeof(dplane_t))); printf ("%5i vertexes %6i\n" ,numvertexes, (int)(numvertexes*sizeof(dvertex_t))); printf ("%5i nodes %6i\n" ,numnodes, (int)(numnodes*sizeof(dnode_t))); printf ("%5i texinfo %6i\n" ,numtexinfo, (int)(numtexinfo*sizeof(texinfo_t))); printf ("%5i faces %6i\n" ,numfaces, (int)(numfaces*sizeof(dface_t))); printf ("%5i clipnodes %6i\n" ,numclipnodes, (int)(numclipnodes*sizeof(dclipnode_t))); printf ("%5i leafs %6i\n" ,numleafs, (int)(numleafs*sizeof(dleaf_t))); printf ("%5i marksurfaces %6i\n" ,nummarksurfaces, (int)(nummarksurfaces*sizeof(dmarksurfaces[0]))); printf ("%5i surfedges %6i\n" ,numsurfedges, (int)(numsurfedges*sizeof(dmarksurfaces[0]))); printf ("%5i edges %6i\n" ,numedges, (int)(numedges*sizeof(dedge_t))); if (!texdatasize) printf (" 0 textures 0\n"); else printf ("%5i textures %6i\n",((dmiptexlump_t*)dtexdata)->nummiptex, texdatasize); printf (" lightdata %6i\n", lightdatasize); printf (" visdata %6i\n", visdatasize); printf (" entdata %6i\n", entdatasize); }