From 24a9b48127dae9e257b6fb8dc05f47ddb9880b3d Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 19 Sep 2002 05:35:17 +0000 Subject: [PATCH] rewrite bspfile.[ch] so they work off an object rather than global data. The direct filesystem access is also removed. Fix up qfvis and qflight to compile with the new bspfile. qfvis seems to work, qflight probably doesn't --- include/QF/bspfile.h | 73 +++--- libs/util/bspfile.c | 379 ++++++++++++++------------------ tools/qflight/include/light.h | 2 + tools/qflight/source/entities.c | 6 +- tools/qflight/source/ltface.c | 18 +- tools/qflight/source/qflight.c | 23 +- tools/qflight/source/trace.c | 8 +- tools/qfvis/include/vis.h | 2 + tools/qfvis/source/qfvis.c | 62 +++--- tools/qfvis/source/soundphs.c | 20 +- 10 files changed, 285 insertions(+), 308 deletions(-) diff --git a/include/QF/bspfile.h b/include/QF/bspfile.h index 070023d39..c74f7f22d 100644 --- a/include/QF/bspfile.h +++ b/include/QF/bspfile.h @@ -29,6 +29,7 @@ #define __bspfile_h_ #include "QF/qtypes.h" +#include "QF/quakeio.h" // upper design bounds @@ -231,59 +232,55 @@ typedef struct { #define ANGLE_UP -1 #define ANGLE_DOWN -2 -// the utilities get to be lazy and just use large static arrays +typedef struct bsp_s { + int nummodels; + dmodel_t *models; -extern int nummodels; -extern dmodel_t *dmodels; + int visdatasize; + byte *visdata; -extern int visdatasize; -extern byte *dvisdata; + int lightdatasize; + byte *lightdata; -extern int lightdatasize; -extern byte *dlightdata; + int texdatasize; + byte *texdata; // (dmiptexlump_t) -extern int texdatasize; -extern byte *dtexdata; // (dmiptexlump_t) + int entdatasize; + char *entdata; -extern int entdatasize; -extern char *dentdata; + int numleafs; + dleaf_t *leafs; -extern int numleafs; -extern dleaf_t *dleafs; + int numplanes; + dplane_t *planes; -extern int numplanes; -extern dplane_t *dplanes; + int numvertexes; + dvertex_t *vertexes; -extern int numvertexes; -extern dvertex_t *dvertexes; + int numnodes; + dnode_t *nodes; -extern int numnodes; -extern dnode_t *dnodes; + int numtexinfo; + texinfo_t *texinfo; -extern int numtexinfo; -extern texinfo_t *texinfo; + int numfaces; + dface_t *faces; -extern int numfaces; -extern dface_t *dfaces; + int numclipnodes; + dclipnode_t *clipnodes; -extern int numclipnodes; -extern dclipnode_t *dclipnodes; + int numedges; + dedge_t *edges; -extern int numedges; -extern dedge_t *dedges; + int nummarksurfaces; + unsigned short *marksurfaces; -extern int nummarksurfaces; -extern unsigned short *dmarksurfaces; + int numsurfedges; + int *surfedges; +} bsp_t; -extern int numsurfedges; -extern int *dsurfedges; - -void DecompressVis (byte *in, byte *decompressed); -int CompressVis (byte *vis, byte *dest); - -void LoadBSPFile (const char *filename); -void WriteBSPFile (const char *filename); -void PrintBSPFileSizes (void); +bsp_t *LoadBSPFile (QFile *file, int size); +void WriteBSPFile (bsp_t *bsp, QFile *file); #endif #endif // __bspfile_h_ diff --git a/libs/util/bspfile.c b/libs/util/bspfile.c index aa3be125b..8e3a35dc1 100644 --- a/libs/util/bspfile.c +++ b/libs/util/bspfile.c @@ -49,248 +49,178 @@ static const char rcsid[] = #include "QF/quakefs.h" #include "QF/sys.h" -int nummodels; -dmodel_t *dmodels; - -int visdatasize; -byte *dvisdata; - -int lightdatasize; -byte *dlightdata; - -int texdatasize; -byte *dtexdata; - -int entdatasize; -char *dentdata; - -int numleafs; -dleaf_t *dleafs; - -int numplanes; -dplane_t *dplanes; - -int numvertexes; -dvertex_t *dvertexes; - -int numnodes; -dnode_t *dnodes; - -int numtexinfo; -texinfo_t *texinfo; - -int numfaces; -dface_t *dfaces; - -int numclipnodes; -dclipnode_t *dclipnodes; - -int numedges; -dedge_t *dedges; - -int nummarksurfaces; -unsigned short *dmarksurfaces; - -int numsurfedges; -int *dsurfedges; - -dheader_t *header; -dheader_t outheader; -QFile *wadfile; - - -/* - SwapBSPFile - - Byte swaps all data in a bsp file. -*/ static void -SwapBSPFile (qboolean todisk) +swap_bsp (bsp_t *bsp, int todisk) { int c, i, j; dmiptexlump_t *mtl; dmodel_t *d; // models - for (i = 0; i < nummodels; i++) { - d = &dmodels[i]; + for (i=0 ; inummodels ; i++) { + d = &bsp->models[i]; - for (j = 0; j < MAX_MAP_HULLS; j++) + for (j=0 ; jheadnode[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]); + 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; i < numvertexes ; i++) { - for (j = 0; j < 3; j++) - dvertexes[i].point[j] = LittleFloat (dvertexes[i].point[j]); + for (i=0 ; inumvertexes ; i++) { + for (j=0 ; j<3 ; j++) + bsp->vertexes[i].point[j] = LittleFloat (bsp->vertexes[i].point[j]); } // planes - for (i = 0; i < numplanes; i++) { - for (j = 0; j < 3; j++) - dplanes[i].normal[j] = LittleFloat (dplanes[i].normal[j]); - dplanes[i].dist = LittleFloat (dplanes[i].dist); - dplanes[i].type = LittleLong (dplanes[i].type); + for (i=0 ; inumplanes ; i++) { + for (j=0 ; j<3 ; j++) + bsp->planes[i].normal[j] = LittleFloat (bsp->planes[i].normal[j]); + bsp->planes[i].dist = LittleFloat (bsp->planes[i].dist); + bsp->planes[i].type = LittleLong (bsp->planes[i].type); } // texinfos - for (i = 0; i < numtexinfo; i++) { - for (j = 0; j < 8; j++) - texinfo[i].vecs[0][j] = LittleFloat (texinfo[i].vecs[0][j]); - texinfo[i].miptex = LittleLong (texinfo[i].miptex); - texinfo[i].flags = LittleLong (texinfo[i].flags); + for (i=0 ; inumtexinfo ; i++) { + for (j=0 ; j<8 ; j++) + bsp->texinfo[i].vecs[0][j] = LittleFloat (bsp->texinfo[i].vecs[0][j]); + bsp->texinfo[i].miptex = LittleLong (bsp->texinfo[i].miptex); + bsp->texinfo[i].flags = LittleLong (bsp->texinfo[i].flags); } // faces - for (i = 0; i < numfaces; i++) { - dfaces[i].texinfo = LittleShort (dfaces[i].texinfo); - dfaces[i].planenum = LittleShort (dfaces[i].planenum); - dfaces[i].side = LittleShort (dfaces[i].side); - dfaces[i].lightofs = LittleLong (dfaces[i].lightofs); - dfaces[i].firstedge = LittleLong (dfaces[i].firstedge); - dfaces[i].numedges = LittleShort (dfaces[i].numedges); + for (i=0 ; inumfaces ; i++) { + bsp->faces[i].texinfo = LittleShort (bsp->faces[i].texinfo); + bsp->faces[i].planenum = LittleShort (bsp->faces[i].planenum); + bsp->faces[i].side = LittleShort (bsp->faces[i].side); + bsp->faces[i].lightofs = LittleLong (bsp->faces[i].lightofs); + bsp->faces[i].firstedge = LittleLong (bsp->faces[i].firstedge); + bsp->faces[i].numedges = LittleShort (bsp->faces[i].numedges); } // nodes - for (i = 0; i < numnodes; i++) { - dnodes[i].planenum = LittleLong (dnodes[i].planenum); - for (j = 0; j < 3; j++) { - dnodes[i].mins[j] = LittleShort (dnodes[i].mins[j]); - dnodes[i].maxs[j] = LittleShort (dnodes[i].maxs[j]); + for (i=0 ; inumnodes ; i++) { + bsp->nodes[i].planenum = LittleLong (bsp->nodes[i].planenum); + for (j=0 ; j<3 ; j++) { + bsp->nodes[i].mins[j] = LittleShort (bsp->nodes[i].mins[j]); + bsp->nodes[i].maxs[j] = LittleShort (bsp->nodes[i].maxs[j]); } - dnodes[i].children[0] = LittleShort (dnodes[i].children[0]); - dnodes[i].children[1] = LittleShort (dnodes[i].children[1]); - dnodes[i].firstface = LittleShort (dnodes[i].firstface); - dnodes[i].numfaces = LittleShort (dnodes[i].numfaces); + bsp->nodes[i].children[0] = LittleShort (bsp->nodes[i].children[0]); + bsp->nodes[i].children[1] = LittleShort (bsp->nodes[i].children[1]); + bsp->nodes[i].firstface = LittleShort (bsp->nodes[i].firstface); + bsp->nodes[i].numfaces = LittleShort (bsp->nodes[i].numfaces); } // leafs - for (i = 0; i < numleafs; i++) { - dleafs[i].contents = LittleLong (dleafs[i].contents); - for (j = 0; j < 3; j++) { - dleafs[i].mins[j] = LittleShort (dleafs[i].mins[j]); - dleafs[i].maxs[j] = LittleShort (dleafs[i].maxs[j]); + for (i=0 ; inumleafs ; i++) { + bsp->leafs[i].contents = LittleLong (bsp->leafs[i].contents); + for (j=0 ; j<3 ; j++) { + bsp->leafs[i].mins[j] = LittleShort (bsp->leafs[i].mins[j]); + bsp->leafs[i].maxs[j] = LittleShort (bsp->leafs[i].maxs[j]); } - dleafs[i].firstmarksurface = LittleShort (dleafs[i].firstmarksurface); - dleafs[i].nummarksurfaces = LittleShort (dleafs[i].nummarksurfaces); - dleafs[i].visofs = LittleLong (dleafs[i].visofs); + bsp->leafs[i].firstmarksurface = LittleShort (bsp->leafs[i].firstmarksurface); + bsp->leafs[i].nummarksurfaces = LittleShort (bsp->leafs[i].nummarksurfaces); + bsp->leafs[i].visofs = LittleLong (bsp->leafs[i].visofs); } // clipnodes - for (i = 0; i < numclipnodes; i++) { - dclipnodes[i].planenum = LittleLong (dclipnodes[i].planenum); - dclipnodes[i].children[0] = LittleShort (dclipnodes[i].children[0]); - dclipnodes[i].children[1] = LittleShort (dclipnodes[i].children[1]); + for (i=0 ; inumclipnodes ; i++) { + bsp->clipnodes[i].planenum = LittleLong (bsp->clipnodes[i].planenum); + bsp->clipnodes[i].children[0] = LittleShort (bsp->clipnodes[i].children[0]); + bsp->clipnodes[i].children[1] = LittleShort (bsp->clipnodes[i].children[1]); } // miptex - if (texdatasize) { - mtl = (dmiptexlump_t *) dtexdata; + if (bsp->texdatasize) { + mtl = (dmiptexlump_t *)bsp->texdata; if (todisk) c = mtl->nummiptex; else - c = LittleLong (mtl->nummiptex); + c = LittleLong(mtl->nummiptex); mtl->nummiptex = LittleLong (mtl->nummiptex); - for (i = 0; i < c; i++) - mtl->dataofs[i] = LittleLong (mtl->dataofs[i]); + for (i=0 ; idataofs[i] = LittleLong(mtl->dataofs[i]); } // marksurfaces - for (i = 0; i < nummarksurfaces; i++) - dmarksurfaces[i] = LittleShort (dmarksurfaces[i]); + for (i=0 ; inummarksurfaces ; i++) + bsp->marksurfaces[i] = LittleShort (bsp->marksurfaces[i]); // surfedges - for (i = 0; i < numsurfedges; i++) - dsurfedges[i] = LittleLong (dsurfedges[i]); + for (i=0 ; inumsurfedges ; i++) + bsp->surfedges[i] = LittleLong (bsp->surfedges[i]); // edges - for (i = 0; i < numedges; i++) { - dedges[i].v[0] = LittleShort (dedges[i].v[0]); - dedges[i].v[1] = LittleShort (dedges[i].v[1]); + for (i=0 ; inumedges ; i++) { + bsp->edges[i].v[0] = LittleShort (bsp->edges[i].v[0]); + bsp->edges[i].v[1] = LittleShort (bsp->edges[i].v[1]); } } -static void -AddLump (int lumpnum, void *data, int len) +bsp_t * +LoadBSPFile (QFile *file, int size) { - lump_t *lump; + dheader_t *header; + bsp_t *bsp; - lump = &header->lumps[lumpnum]; - - lump->fileofs = LittleLong (Qtell (wadfile)); - lump->filelen = LittleLong (len); - Qwrite (wadfile, data, (len + 3) &~3); -} + header = malloc (size); + Qread (file, header, size); -static int -CopyLump (int lump, void **dest, int size) -{ - int length, ofs; + if (LittleLong (header->version) != BSPVERSION) + Sys_Error ("version %i, not %i", LittleLong (header->version), + BSPVERSION); - length = header->lumps[lump].filelen; - ofs = header->lumps[lump].fileofs; - - if (length % size) - Sys_Error ("LoadBSPFile: odd lump size"); - - *dest = malloc (length); - if (!dest) - Sys_Error ("LoadBSPFile: out of memory"); - memcpy (*dest, (byte *) header + ofs, length); +#undef SET_LUMP +#define SET_LUMP(l,n) \ +do { \ + bsp->num##n = LittleLong (header->lumps[l].filelen); \ + bsp->n = malloc (bsp->num##n); \ + memcpy (bsp->n, (byte *) header + LittleLong (header->lumps[l].fileofs), \ + bsp->num##n); \ + bsp->num##n /= sizeof (bsp->n[0]); \ +} while (0) - return length / size; -} + bsp = malloc (sizeof (bsp_t)); + SET_LUMP (LUMP_PLANES, planes); + SET_LUMP (LUMP_LEAFS, leafs); + SET_LUMP (LUMP_VERTEXES, vertexes); + SET_LUMP (LUMP_NODES, nodes); + SET_LUMP (LUMP_TEXINFO, texinfo); + SET_LUMP (LUMP_FACES, faces); + SET_LUMP (LUMP_CLIPNODES, clipnodes); + SET_LUMP (LUMP_MARKSURFACES, marksurfaces); + SET_LUMP (LUMP_SURFEDGES, surfedges); + SET_LUMP (LUMP_EDGES, edges); + SET_LUMP (LUMP_MODELS, models); -extern int LoadFile (const char *filename, void **bufferptr); +#undef SET_LUMP +#define SET_LUMP(l,n) \ +do { \ + bsp->n##size = LittleLong (header->lumps[l].filelen); \ + bsp->n = malloc (bsp->n##size); \ + memcpy (bsp->n, (byte *) header + LittleLong (header->lumps[l].fileofs), \ + bsp->n##size); \ + bsp->n##size /= sizeof (bsp->n[0]); \ +} while (0) -void -LoadBSPFile (const 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) - Sys_Error ("%s is version %i, not %i", filename, i, BSPVERSION); -//FIXME casting to (void**) isn't really portable :( - nummodels = CopyLump (LUMP_MODELS, (void **)&dmodels, sizeof (dmodel_t)); - numvertexes = CopyLump (LUMP_VERTEXES, (void **)&dvertexes, sizeof (dvertex_t)); - numplanes = CopyLump (LUMP_PLANES, (void **)&dplanes, sizeof (dplane_t)); - numleafs = CopyLump (LUMP_LEAFS, (void **)&dleafs, sizeof (dleaf_t)); - numnodes = CopyLump (LUMP_NODES, (void **)&dnodes, sizeof (dnode_t)); - numtexinfo = CopyLump (LUMP_TEXINFO, (void **)&texinfo, sizeof (texinfo_t)); - numclipnodes = CopyLump (LUMP_CLIPNODES, (void **)&dclipnodes, sizeof (dclipnode_t)); - numfaces = CopyLump (LUMP_FACES, (void **)&dfaces, sizeof (dface_t)); - nummarksurfaces = CopyLump (LUMP_MARKSURFACES, (void **)&dmarksurfaces, - sizeof (dmarksurfaces[0])); - numsurfedges = CopyLump (LUMP_SURFEDGES, (void **)&dsurfedges, - sizeof (dsurfedges[0])); - numedges = CopyLump (LUMP_EDGES, (void **)&dedges, sizeof (dedge_t)); - - texdatasize = CopyLump (LUMP_TEXTURES, (void **)&dtexdata, 1); - visdatasize = CopyLump (LUMP_VISIBILITY, (void **)&dvisdata, 1); - lightdatasize = CopyLump (LUMP_LIGHTING, (void **)&dlightdata, 1); - entdatasize = CopyLump (LUMP_ENTITIES, (void **)&dentdata, 1); + SET_LUMP (LUMP_LIGHTING, lightdata); + SET_LUMP (LUMP_VISIBILITY, visdata); + SET_LUMP (LUMP_ENTITIES, entdata); + SET_LUMP (LUMP_TEXTURES, texdata); free (header); - - SwapBSPFile (false); + + swap_bsp (bsp, 0); + return bsp; } /* @@ -299,41 +229,74 @@ LoadBSPFile (const char *filename) Swaps the bsp file in place, so it should not be referenced again */ void -WriteBSPFile (const char *filename) +WriteBSPFile (bsp_t *bsp, QFile *file) { - header = &outheader; + int size; + dheader_t *header; + byte *data; + + size = sizeof (dheader_t); + size += bsp->nummodels * sizeof (dmodel_t); + size += bsp->visdatasize; + size += bsp->lightdatasize; + size += bsp->texdatasize; + size += bsp->entdatasize; + size += bsp->numleafs * sizeof (dleaf_t); + size += bsp->numplanes * sizeof (dplane_t); + size += bsp->numvertexes * sizeof (dvertex_t); + size += bsp->numnodes * sizeof (dnode_t); + size += bsp->numtexinfo * sizeof (texinfo_t); + size += bsp->numfaces * sizeof (dface_t); + size += bsp->numclipnodes * sizeof (dclipnode_t); + size += bsp->numedges * sizeof (dedge_t); + size += bsp->nummarksurfaces * sizeof (unsigned short); + size += bsp->numsurfedges * sizeof (int); + + header = malloc (size); memset (header, 0, sizeof (dheader_t)); - - SwapBSPFile (true); + + swap_bsp (bsp, 1); + +#undef SET_LUMP +#define SET_LUMP(l,n) \ +do { \ + bsp->num##n *= sizeof (bsp->n[0]); \ + header->lumps[l].fileofs = LittleLong (data - (byte *) header); \ + header->lumps[l].filelen = LittleLong (bsp->num##n); \ + memcpy (data, bsp->n, bsp->num##n); \ + data += bsp->num##n; \ +} while (0) header->version = LittleLong (BSPVERSION); - - wadfile = Qopen (filename, "wb"); - if (!wadfile) - Sys_Error ("Error opening %s", filename); - - Qwrite (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)); + data = (byte *) &header[1]; + SET_LUMP (LUMP_PLANES, planes); + SET_LUMP (LUMP_LEAFS, leafs); + SET_LUMP (LUMP_VERTEXES, vertexes); + SET_LUMP (LUMP_NODES, nodes); + SET_LUMP (LUMP_TEXINFO, texinfo); + SET_LUMP (LUMP_FACES, faces); + SET_LUMP (LUMP_CLIPNODES, clipnodes); + SET_LUMP (LUMP_MARKSURFACES, marksurfaces); + SET_LUMP (LUMP_SURFEDGES, surfedges); + SET_LUMP (LUMP_EDGES, edges); + SET_LUMP (LUMP_MODELS, models); - AddLump (LUMP_LIGHTING, dlightdata, lightdatasize); - AddLump (LUMP_VISIBILITY, dvisdata, visdatasize); - AddLump (LUMP_ENTITIES, dentdata, entdatasize); - AddLump (LUMP_TEXTURES, dtexdata, texdatasize); - - Qseek (wadfile, 0, SEEK_SET); - Qwrite (wadfile, header, sizeof (dheader_t)); - Qclose (wadfile); +#undef SET_LUMP +#define SET_LUMP(l,n) \ +do { \ + bsp->n##size *= sizeof (bsp->n[0]); \ + header->lumps[l].fileofs = LittleLong (data - (byte *) header); \ + header->lumps[l].filelen = LittleLong (bsp->n##size); \ + memcpy (data, bsp->n, bsp->n##size); \ + data += bsp->n##size; \ +} while (0) + + SET_LUMP (LUMP_LIGHTING, lightdata); + SET_LUMP (LUMP_VISIBILITY, visdata); + SET_LUMP (LUMP_ENTITIES, entdata); + SET_LUMP (LUMP_TEXTURES, texdata); + + Qwrite (file, header, size); + free (header); } diff --git a/tools/qflight/include/light.h b/tools/qflight/include/light.h index c5f27f6fb..e36d56eb7 100644 --- a/tools/qflight/include/light.h +++ b/tools/qflight/include/light.h @@ -59,4 +59,6 @@ byte *GetFileSpace (int size); void TransformSample (vec3_t in, vec3_t out); void RotateSample (vec3_t in, vec3_t out); +extern struct bsp_s *bsp; + #endif// __light_h diff --git a/tools/qflight/source/entities.c b/tools/qflight/source/entities.c index 498fcb4af..7dae7829d 100644 --- a/tools/qflight/source/entities.c +++ b/tools/qflight/source/entities.c @@ -130,7 +130,7 @@ LoadEntities (void) epair_t *epair; int i; - data = dentdata; + data = bsp->entdata; // start parsing num_entities = 0; @@ -279,7 +279,7 @@ WriteEntitiesToString (void) epair_t *ep; int i; - buf = dentdata; + buf = bsp->entdata; end = buf; *end = 0; @@ -305,5 +305,5 @@ WriteEntitiesToString (void) if (end > buf + MAX_MAP_ENTSTRING) fprintf (stderr, "Entity text too long"); } - entdatasize = end - buf + 1; + bsp->entdatasize = end - buf + 1; } diff --git a/tools/qflight/source/ltface.c b/tools/qflight/source/ltface.c index e4dd20ea6..cc39d3ada 100644 --- a/tools/qflight/source/ltface.c +++ b/tools/qflight/source/ltface.c @@ -140,7 +140,7 @@ CalcFaceVectors (lightinfo_t *l) float distscale; vec_t dist, len; - tex = &texinfo[l->face->texinfo]; + tex = &bsp->texinfo[l->face->texinfo]; // convert from float to vec_t for (i = 0; i < 2; i++) @@ -210,14 +210,14 @@ CalcFaceExtents (lightinfo_t *l) mins[0] = mins[1] = 999999; maxs[0] = maxs[1] = -99999; - tex = &texinfo[s->texinfo]; + tex = &bsp->texinfo[s->texinfo]; for (i = 0; i < s->numedges; i++) { - e = dsurfedges[s->firstedge + i]; + e = bsp->surfedges[s->firstedge + i]; if (e >= 0) - v = dvertexes + dedges[e].v[0]; + v = bsp->vertexes + bsp->edges[e].v[0]; else - v = dvertexes + dedges[-e].v[1]; + v = bsp->vertexes + bsp->edges[-e].v[1]; for (j = 0; j < 2; j++) { val = v->point[0] * tex->vecs[j][0] + @@ -458,14 +458,14 @@ LightFace (int surfnum) vec_t total; vec_t *light; - f = dfaces + surfnum; + f = bsp->faces + surfnum; // some surfaces don't need lightmaps f->lightofs = -1; for (j = 0; j < MAXLIGHTMAPS; j++) f->styles[j] = 255; - if (texinfo[f->texinfo].flags & TEX_SPECIAL) + if (bsp->texinfo[f->texinfo].flags & TEX_SPECIAL) // non-lit texture return; @@ -474,8 +474,8 @@ LightFace (int surfnum) l.face = f; // rotate plane - VectorCopy (dplanes[f->planenum].normal, l.facenormal); - l.facedist = dplanes[f->planenum].dist; + VectorCopy (bsp->planes[f->planenum].normal, l.facenormal); + l.facedist = bsp->planes[f->planenum].dist; if (f->side) { VectorSubtract (vec3_origin, l.facenormal, l.facenormal); l.facedist = -l.facedist; diff --git a/tools/qflight/source/qflight.c b/tools/qflight/source/qflight.c index fdc20b625..5938ee54a 100644 --- a/tools/qflight/source/qflight.c +++ b/tools/qflight/source/qflight.c @@ -59,6 +59,7 @@ static const char rcsid[] = #include "options.h" options_t options; +bsp_t *bsp; char *bspfile; @@ -100,7 +101,7 @@ LightThread (void *junk) LOCK; i = bspfileface++; UNLOCK; - if (i >= numfaces) + if (i >= bsp->numfaces) return; LightFace (i); @@ -110,21 +111,22 @@ LightThread (void *junk) void LightWorld (void) { - filebase = file_p = dlightdata; - file_end = filebase + MAX_MAP_LIGHTING; + filebase = file_p = bsp->lightdata; + file_end = filebase + MAX_MAP_LIGHTING; RunThreadsOn (LightThread); - lightdatasize = file_p - filebase; + bsp->lightdatasize = file_p - filebase; if (options.verbosity >= 0) - printf ("lightdatasize: %i\n", lightdatasize); + printf ("lightdatasize: %i\n", bsp->lightdatasize); } int main (int argc, char **argv) { double start, stop; + QFile *f; start = Sys_DoubleTime (); @@ -142,15 +144,20 @@ main (int argc, char **argv) COM_StripExtension (bspfile, bspfile); COM_DefaultExtension (bspfile, ".bsp"); - LoadBSPFile (bspfile); + f = Qopen (bspfile, "rb"); + bsp = LoadBSPFile (f, Qfilesize (f)); + Qclose (f); LoadEntities (); - MakeTnodes (&dmodels[0]); + MakeTnodes (&bsp->models[0]); LightWorld (); WriteEntitiesToString (); - WriteBSPFile (bspfile); + + f = Qopen (bspfile, "wb"); + WriteBSPFile (bsp, f); + Qclose (f); stop = Sys_DoubleTime (); diff --git a/tools/qflight/source/trace.c b/tools/qflight/source/trace.c index 205a7c039..8eaba67f2 100644 --- a/tools/qflight/source/trace.c +++ b/tools/qflight/source/trace.c @@ -91,8 +91,8 @@ MakeTnode (int nodenum) t = tnode_p++; - node = dnodes + nodenum; - plane = dplanes + node->planenum; + node = bsp->nodes + nodenum; + plane = bsp->planes + node->planenum; t->type = plane->type; VectorCopy (plane->normal, t->normal); @@ -100,7 +100,7 @@ MakeTnode (int nodenum) for (i = 0; i < 2; i++) { if (node->children[i] < 0) - t->children[i] = dleafs[-node->children[i] - 1].contents; + t->children[i] = bsp->leafs[-node->children[i] - 1].contents; else { t->children[i] = tnode_p - tnodes; MakeTnode (node->children[i]); @@ -116,7 +116,7 @@ MakeTnode (int nodenum) void MakeTnodes (dmodel_t *bm) { - tnode_p = tnodes = malloc (numnodes * sizeof (tnode_t)); + tnode_p = tnodes = malloc (bsp->numnodes * sizeof (tnode_t)); MakeTnode (0); } diff --git a/tools/qfvis/include/vis.h b/tools/qfvis/include/vis.h index a44cce366..228ebeced 100644 --- a/tools/qfvis/include/vis.h +++ b/tools/qfvis/include/vis.h @@ -133,4 +133,6 @@ void BasePortalVis (void); void PortalFlow (portal_t *portal); void CalcAmbientSounds (void); +extern struct bsp_s *bsp; + #endif// __vis_h diff --git a/tools/qfvis/source/qfvis.c b/tools/qfvis/source/qfvis.c index d11e4d4ca..49e2c7319 100644 --- a/tools/qfvis/source/qfvis.c +++ b/tools/qfvis/source/qfvis.c @@ -66,6 +66,8 @@ static const char rcsid[] = pthread_mutex_t *my_mutex; #endif +bsp_t *bsp; + options_t options; int numportals; @@ -451,19 +453,18 @@ LeafFlow (int leafnum) vismap_p += i; if (vismap_p > vismap_end) { - int d = dest - dvisdata; - int p = vismap_p - dvisdata; - int e = vismap_end - dvisdata; - - visdatasize = p; - vismap = dvisdata = realloc (dvisdata, visdatasize); - dest = dvisdata + d; - vismap_p = dvisdata + p; - vismap_end = dvisdata + e; + int d = dest - bsp->visdata; + int p = vismap_p - bsp->visdata; + int e = vismap_end - bsp->visdata; + bsp->visdatasize = p; + vismap = bsp->visdata = realloc (bsp->visdata, bsp->visdatasize); + dest = bsp->visdata + d; + vismap_p = bsp->visdata + p; + vismap_end = bsp->visdata + e; fprintf (stderr, "Vismap grown\n"); } - dleafs[leafnum + 1].visofs = dest - vismap; // leaf 0 is a common solid + bsp->leafs[leafnum + 1].visofs = dest - vismap; // leaf 0 is a common solid memcpy (dest, compressed, i); } @@ -504,19 +505,19 @@ CalcPortalVis (void) if (pthread_attr_setstacksize (&attrib, 0x100000) == -1) fprintf (stderr, "pthread_attr_setstacksize failed\n"); for (i = 0; i < options.threads; i++) { - if (pthread_create (&work_threads[i], &attrib, LeafThread, + if (pthread_create (&work_threads[i], &attrib, LeafThread, (void *) i) == -1) fprintf (stderr, "pthread_create failed\n"); } for (i = 0; i < options.threads; i++) { - if (pthread_join (work_threads[i], &status) == -1) + if (pthread_join (work_threads[i], &status) == -1) fprintf (stderr, "pthread_join failed\n"); } if (pthread_mutex_destroy (my_mutex) == -1) - fprintf (stderr, "pthread_mutex_destroy failed\n"); - } + fprintf (stderr, "pthread_mutex_destroy failed\n"); + } #else LeafThread (0); #endif @@ -564,16 +565,16 @@ Findpassages (winding_t *source, winding_t *pass) { double length; float d; - int counts[3]; + int counts[3]; int i, j, k, l; plane_t plane; - qboolean fliptest; - sep_t *sep, *list; + qboolean fliptest; + sep_t *sep, *list; vec3_t v1, v2; list = NULL; - // check all combinations + // check all combinations for (i = 0; i < source->numpoints; i++) { l = (i + 1) % source->numpoints; VectorSubtract (source->points[l], source->points[i], v1); @@ -763,8 +764,8 @@ LoadPortals (char *name) originalvismapsize = portalleafs * ((portalleafs + 7) / 8); - vismap = vismap_p = dvisdata; - vismap_end = vismap + visdatasize; + vismap = vismap_p = bsp->visdata; + vismap_end = vismap + bsp->visdatasize; for (i = 0, portal = portals; i < numportals; i++) { if (fscanf (f, "%i %i %i ", &numpoints, &leafnums[0], @@ -782,13 +783,13 @@ LoadPortals (char *name) winding->numpoints = numpoints; for (j = 0; j < numpoints; j++) { - double v[3]; - int k; + double v[3]; + int k; // scanf into double, then assign to vec_t if (fscanf (f, "(%lf %lf %lf ) ", &v[0], &v[1], &v[2]) != 3) fprintf (stderr, "LoadPortals: reading portal %i\n", i); - + for (k = 0; k < 3; k++) winding->points[j][k] = v[k]; } @@ -831,6 +832,7 @@ main (int argc, char **argv) { double start, stop; dstring_t *portalfile = dstring_new (); + QFile *f; start = Sys_DoubleTime (); @@ -845,8 +847,10 @@ main (int argc, char **argv) COM_StripExtension (options.bspfile, options.bspfile); COM_DefaultExtension (options.bspfile, ".bsp"); - - LoadBSPFile (options.bspfile); + + f = Qopen (options.bspfile, "rb"); + bsp = LoadBSPFile (f, Qfilesize (f)); + Qclose (f); portalfile->size = strlen (options.bspfile) + 1; dstring_adjust (portalfile); @@ -862,14 +866,16 @@ main (int argc, char **argv) if (options.verbosity >= 0) printf ("c_chains: %i\n", c_chains); - visdatasize = vismap_p - dvisdata; + bsp->visdatasize = vismap_p - bsp->visdata; if (options.verbosity >= 0) - printf ("visdatasize:%i compressed from %i\n", visdatasize, + printf ("visdatasize:%i compressed from %i\n", bsp->visdatasize, originalvismapsize); CalcAmbientSounds (); - WriteBSPFile (options.bspfile); + f = Qopen (options.bspfile, "wb"); + WriteBSPFile (bsp, f); + Qclose (f); stop = Sys_DoubleTime (); diff --git a/tools/qfvis/source/soundphs.c b/tools/qfvis/source/soundphs.c index d9321e840..9ee1e25cf 100644 --- a/tools/qfvis/source/soundphs.c +++ b/tools/qfvis/source/soundphs.c @@ -75,12 +75,12 @@ SurfaceBBox (dface_t *s, vec3_t mins, vec3_t maxs) maxs[0] = maxs[1] = -99999; for (i = 0; i < s->numedges; i++) { - e = dsurfedges[s->firstedge + i]; + e = bsp->surfedges[s->firstedge + i]; if (e >= 0) - vi = dedges[e].v[0]; + vi = bsp->edges[e].v[0]; else - vi = dedges[-e].v[1]; - v = dvertexes[vi].point; + vi = bsp->edges[-e].v[1]; + v = bsp->vertexes[vi].point; for (j = 0; j < 3; j++) { if (v[j] < mins[j]) @@ -105,7 +105,7 @@ CalcAmbientSounds (void) miptex_t *miptex; for (i = 0; i < portalleafs; i++) { - leaf = &dleafs[i + 1]; + leaf = &bsp->leafs[i + 1]; // clear ambients for (j = 0; j < NUM_AMBIENTS; j++) @@ -118,13 +118,13 @@ CalcAmbientSounds (void) continue; // check this leaf for sound textures - hit = &dleafs[j + 1]; + hit = &bsp->leafs[j + 1]; for (k = 0; k < hit->nummarksurfaces; k++) { - surf = &dfaces[dmarksurfaces[hit->firstmarksurface + k]]; - info = &texinfo[surf->texinfo]; - ofs = ((dmiptexlump_t *) dtexdata)->dataofs[info->miptex]; - miptex = (miptex_t *) (&dtexdata[ofs]); + surf = &bsp->faces[bsp->marksurfaces[hit->firstmarksurface + k]]; + info = &bsp->texinfo[surf->texinfo]; + ofs = ((dmiptexlump_t *) bsp->texdata)->dataofs[info->miptex]; + miptex = (miptex_t *) (&bsp->texdata[ofs]); if (!strncasecmp (miptex->name, "*water", 6)) ambient_type = AMBIENT_WATER;