2002-08-25 23:10:57 +00:00
|
|
|
/*
|
|
|
|
bspfile.c
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2002-08-25 23:10:57 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
2002-11-08 17:13:23 +00:00
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2002-08-25 23:10:57 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/bspfile.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/qtypes.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2002-08-25 23:10:57 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2002-09-13 15:07:48 +00:00
|
|
|
static void
|
2010-08-23 02:56:43 +00:00
|
|
|
swap_bsp (bsp_t *bsp, int todisk, void (*cb) (const bsp_t *, void *),
|
|
|
|
void *cbdata)
|
2002-08-25 23:10:57 +00:00
|
|
|
{
|
2010-08-21 23:40:31 +00:00
|
|
|
int c, i, j;
|
|
|
|
dmiptexlump_t *mtl;
|
|
|
|
dmodel_t *d;
|
2002-08-25 23:10:57 +00:00
|
|
|
|
2010-08-22 13:29:26 +00:00
|
|
|
if (bsp->header) {
|
|
|
|
bsp->header->version = LittleLong (bsp->header->version);
|
|
|
|
for (i = 0; i < HEADER_LUMPS; i++) {
|
|
|
|
bsp->header->lumps[i].fileofs =
|
|
|
|
LittleLong (bsp->header->lumps[i].fileofs);
|
|
|
|
bsp->header->lumps[i].filelen =
|
|
|
|
LittleLong (bsp->header->lumps[i].filelen);
|
|
|
|
}
|
2010-08-23 02:56:43 +00:00
|
|
|
if (cb)
|
|
|
|
cb (bsp, cbdata);
|
2010-08-22 13:29:26 +00:00
|
|
|
}
|
|
|
|
|
2010-08-21 23:40:31 +00:00
|
|
|
// models
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->nummodels ; i++) {
|
|
|
|
d = &bsp->models[i];
|
2002-08-25 23:10:57 +00:00
|
|
|
|
2002-09-19 05:35:17 +00:00
|
|
|
for (j=0 ; j<MAX_MAP_HULLS ; j++)
|
2002-08-25 23:10:57 +00:00
|
|
|
d->headnode[j] = LittleLong (d->headnode[j]);
|
|
|
|
|
|
|
|
d->visleafs = LittleLong (d->visleafs);
|
|
|
|
d->firstface = LittleLong (d->firstface);
|
|
|
|
d->numfaces = LittleLong (d->numfaces);
|
|
|
|
|
2002-09-19 05:35:17 +00:00
|
|
|
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]);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertexes
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->numvertexes ; i++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
dvertex_t *vertex = &bsp->vertexes[i];
|
2002-09-19 05:35:17 +00:00
|
|
|
for (j=0 ; j<3 ; j++)
|
2002-11-08 17:13:23 +00:00
|
|
|
vertex->point[j] = LittleFloat (vertex->point[j]);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// planes
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->numplanes ; i++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
dplane_t *plane = &bsp->planes[i];
|
2002-09-19 05:35:17 +00:00
|
|
|
for (j=0 ; j<3 ; j++)
|
2002-11-08 17:13:23 +00:00
|
|
|
plane->normal[j] = LittleFloat (plane->normal[j]);
|
|
|
|
plane->dist = LittleFloat (plane->dist);
|
|
|
|
plane->type = LittleLong (plane->type);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// texinfos
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->numtexinfo ; i++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
texinfo_t *texinfo = &bsp->texinfo[i];
|
2002-09-19 05:35:17 +00:00
|
|
|
for (j=0 ; j<8 ; j++)
|
2002-11-08 17:13:23 +00:00
|
|
|
texinfo->vecs[0][j] = LittleFloat (texinfo->vecs[0][j]);
|
|
|
|
texinfo->miptex = LittleLong (texinfo->miptex);
|
|
|
|
texinfo->flags = LittleLong (texinfo->flags);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// faces
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->numfaces ; i++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
dface_t *face = &bsp->faces[i];
|
|
|
|
face->texinfo = LittleShort (face->texinfo);
|
|
|
|
face->planenum = LittleShort (face->planenum);
|
|
|
|
face->side = LittleShort (face->side);
|
|
|
|
face->lightofs = LittleLong (face->lightofs);
|
|
|
|
face->firstedge = LittleLong (face->firstedge);
|
|
|
|
face->numedges = LittleShort (face->numedges);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nodes
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->numnodes ; i++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
dnode_t *node = &bsp->nodes[i];
|
|
|
|
node->planenum = LittleLong (node->planenum);
|
2002-09-19 05:35:17 +00:00
|
|
|
for (j=0 ; j<3 ; j++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
node->mins[j] = LittleShort (node->mins[j]);
|
|
|
|
node->maxs[j] = LittleShort (node->maxs[j]);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
2002-11-08 17:13:23 +00:00
|
|
|
node->children[0] = LittleShort (node->children[0]);
|
|
|
|
node->children[1] = LittleShort (node->children[1]);
|
|
|
|
node->firstface = LittleShort (node->firstface);
|
|
|
|
node->numfaces = LittleShort (node->numfaces);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// leafs
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->numleafs ; i++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
dleaf_t *leaf = &bsp->leafs[i];
|
|
|
|
leaf->contents = LittleLong (leaf->contents);
|
2002-09-19 05:35:17 +00:00
|
|
|
for (j=0 ; j<3 ; j++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
leaf->mins[j] = LittleShort (leaf->mins[j]);
|
|
|
|
leaf->maxs[j] = LittleShort (leaf->maxs[j]);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
2002-11-08 17:13:23 +00:00
|
|
|
leaf->firstmarksurface = LittleShort (leaf->firstmarksurface);
|
|
|
|
leaf->nummarksurfaces = LittleShort (leaf->nummarksurfaces);
|
|
|
|
leaf->visofs = LittleLong (leaf->visofs);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// clipnodes
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->numclipnodes ; i++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
dclipnode_t *clipnode = &bsp->clipnodes[i];
|
|
|
|
clipnode->planenum = LittleLong (clipnode->planenum);
|
|
|
|
clipnode->children[0] = LittleShort (clipnode->children[0]);
|
|
|
|
clipnode->children[1] = LittleShort (clipnode->children[1]);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// miptex
|
2002-09-19 05:35:17 +00:00
|
|
|
if (bsp->texdatasize) {
|
|
|
|
mtl = (dmiptexlump_t *)bsp->texdata;
|
2002-08-25 23:10:57 +00:00
|
|
|
if (todisk)
|
|
|
|
c = mtl->nummiptex;
|
|
|
|
else
|
2002-09-19 05:35:17 +00:00
|
|
|
c = LittleLong(mtl->nummiptex);
|
2002-08-25 23:10:57 +00:00
|
|
|
mtl->nummiptex = LittleLong (mtl->nummiptex);
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<c ; i++)
|
|
|
|
mtl->dataofs[i] = LittleLong(mtl->dataofs[i]);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// marksurfaces
|
2002-11-08 17:13:23 +00:00
|
|
|
for (i=0 ; i<bsp->nummarksurfaces ; i++) {
|
2010-08-21 23:40:31 +00:00
|
|
|
uint16_t *marksurface = &bsp->marksurfaces[i];
|
2002-11-08 17:13:23 +00:00
|
|
|
*marksurface = LittleShort (*marksurface);
|
|
|
|
}
|
2002-08-25 23:10:57 +00:00
|
|
|
|
|
|
|
// surfedges
|
2002-11-08 17:13:23 +00:00
|
|
|
for (i=0 ; i<bsp->numsurfedges ; i++) {
|
2010-08-21 23:40:31 +00:00
|
|
|
int32_t *surfedge = &bsp->surfedges[i];
|
2002-11-08 17:13:23 +00:00
|
|
|
*surfedge = LittleLong (*surfedge);
|
|
|
|
}
|
2002-08-25 23:10:57 +00:00
|
|
|
|
|
|
|
// edges
|
2002-09-19 05:35:17 +00:00
|
|
|
for (i=0 ; i<bsp->numedges ; i++) {
|
2002-11-08 17:13:23 +00:00
|
|
|
dedge_t *edge = &bsp->edges[i];
|
|
|
|
edge->v[0] = LittleShort (edge->v[0]);
|
|
|
|
edge->v[1] = LittleShort (edge->v[1]);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-19 05:35:17 +00:00
|
|
|
bsp_t *
|
2010-08-23 02:56:43 +00:00
|
|
|
LoadBSPMem (void *mem, size_t mem_size, void (*cb) (const bsp_t *, void *),
|
|
|
|
void *cbdata)
|
2002-08-25 23:10:57 +00:00
|
|
|
{
|
2002-09-19 05:35:17 +00:00
|
|
|
bsp_t *bsp;
|
|
|
|
|
2010-08-23 00:40:01 +00:00
|
|
|
bsp = calloc (sizeof (bsp_t), 1);
|
2002-09-19 05:35:17 +00:00
|
|
|
|
2010-08-23 00:40:01 +00:00
|
|
|
bsp->header = mem;
|
|
|
|
|
|
|
|
if (LittleLong (bsp->header->version) != BSPVERSION)
|
|
|
|
Sys_Error ("version %i, not %i", LittleLong (bsp->header->version),
|
|
|
|
BSPVERSION);
|
2010-08-22 13:29:26 +00:00
|
|
|
|
2002-09-19 05:35:17 +00:00
|
|
|
#undef SET_LUMP
|
|
|
|
#define SET_LUMP(l,n) \
|
|
|
|
do { \
|
2010-08-23 02:56:43 +00:00
|
|
|
size_t size = LittleLong (bsp->header->lumps[l].filelen); \
|
|
|
|
size_t offs = LittleLong (bsp->header->lumps[l].fileofs); \
|
|
|
|
void *data = (byte *) mem + offs; \
|
|
|
|
if (offs >= mem_size || (offs + size) > mem_size) \
|
2010-08-23 00:40:01 +00:00
|
|
|
Sys_Error ("invalid lump"); \
|
2010-08-23 02:56:43 +00:00
|
|
|
if (size % sizeof (bsp->n[0])) \
|
|
|
|
Sys_Error ("funny lump size"); \
|
2010-08-23 00:40:01 +00:00
|
|
|
bsp->n = 0; \
|
|
|
|
if (size) \
|
|
|
|
bsp->n = (void *) data; \
|
|
|
|
bsp->num##n = size / sizeof (bsp->n[0]); \
|
2002-09-19 05:35:17 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
#undef SET_LUMP
|
|
|
|
#define SET_LUMP(l,n) \
|
|
|
|
do { \
|
2010-08-23 02:56:43 +00:00
|
|
|
size_t size = LittleLong (bsp->header->lumps[l].filelen); \
|
|
|
|
size_t offs = LittleLong (bsp->header->lumps[l].fileofs); \
|
|
|
|
void *data = (byte *) mem + offs; \
|
|
|
|
if (offs >= mem_size || (offs + size) > mem_size) \
|
2010-08-23 00:40:01 +00:00
|
|
|
Sys_Error ("invalid lump"); \
|
|
|
|
bsp->n = 0; \
|
|
|
|
if (size) \
|
|
|
|
bsp->n = (void *) data; \
|
|
|
|
bsp->n##size = size; \
|
2002-09-19 05:35:17 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
SET_LUMP (LUMP_LIGHTING, lightdata);
|
|
|
|
SET_LUMP (LUMP_VISIBILITY, visdata);
|
|
|
|
SET_LUMP (LUMP_ENTITIES, entdata);
|
|
|
|
SET_LUMP (LUMP_TEXTURES, texdata);
|
2002-08-25 23:10:57 +00:00
|
|
|
|
2010-08-23 02:56:43 +00:00
|
|
|
swap_bsp (bsp, 0, cb, cbdata);
|
2002-09-19 05:35:17 +00:00
|
|
|
return bsp;
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE bsp_t *
|
2010-08-21 23:40:31 +00:00
|
|
|
LoadBSPFile (QFile *file, size_t size)
|
2003-08-11 22:40:46 +00:00
|
|
|
{
|
|
|
|
void *buf;
|
|
|
|
bsp_t *bsp;
|
|
|
|
|
|
|
|
buf = malloc (size);
|
|
|
|
Qread (file, buf, size);
|
2010-08-23 02:56:43 +00:00
|
|
|
bsp = LoadBSPMem (buf, size, 0, 0);
|
2010-08-23 00:49:45 +00:00
|
|
|
bsp->own_header = 1;
|
2003-08-11 22:40:46 +00:00
|
|
|
return bsp;
|
|
|
|
}
|
|
|
|
|
2002-08-25 23:10:57 +00:00
|
|
|
/*
|
|
|
|
WriteBSPFile
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2010-08-22 13:29:26 +00:00
|
|
|
WriteBSPFile (const bsp_t *bsp, QFile *file)
|
2010-08-21 23:40:31 +00:00
|
|
|
{
|
|
|
|
size_t size;
|
2002-09-19 05:35:17 +00:00
|
|
|
byte *data;
|
2010-08-22 13:29:26 +00:00
|
|
|
bsp_t tbsp;
|
2002-09-19 05:35:17 +00:00
|
|
|
|
2002-09-19 15:28:16 +00:00
|
|
|
#define ROUND(x) (((x) + 3) & ~3)
|
|
|
|
|
|
|
|
size = ROUND (sizeof (dheader_t));
|
|
|
|
size += ROUND (bsp->nummodels * sizeof (dmodel_t));
|
|
|
|
size += ROUND (bsp->visdatasize);
|
|
|
|
size += ROUND (bsp->lightdatasize);
|
|
|
|
size += ROUND (bsp->texdatasize);
|
|
|
|
size += ROUND (bsp->entdatasize);
|
|
|
|
size += ROUND (bsp->numleafs * sizeof (dleaf_t));
|
|
|
|
size += ROUND (bsp->numplanes * sizeof (dplane_t));
|
|
|
|
size += ROUND (bsp->numvertexes * sizeof (dvertex_t));
|
|
|
|
size += ROUND (bsp->numnodes * sizeof (dnode_t));
|
|
|
|
size += ROUND (bsp->numtexinfo * sizeof (texinfo_t));
|
|
|
|
size += ROUND (bsp->numfaces * sizeof (dface_t));
|
|
|
|
size += ROUND (bsp->numclipnodes * sizeof (dclipnode_t));
|
|
|
|
size += ROUND (bsp->numedges * sizeof (dedge_t));
|
2010-08-21 23:40:31 +00:00
|
|
|
size += ROUND (bsp->nummarksurfaces * sizeof (uint16_t));
|
|
|
|
size += ROUND (bsp->numsurfedges * sizeof (uint32_t));
|
2002-09-19 05:35:17 +00:00
|
|
|
|
2010-08-22 13:29:26 +00:00
|
|
|
tbsp.header = calloc (size, 1);
|
2002-09-19 05:35:17 +00:00
|
|
|
|
|
|
|
#undef SET_LUMP
|
|
|
|
#define SET_LUMP(l,n) \
|
|
|
|
do { \
|
2010-08-22 13:29:26 +00:00
|
|
|
tbsp.num##n = bsp->num##n; \
|
|
|
|
if (tbsp.num##n) {\
|
|
|
|
tbsp.n = (void *) data; \
|
|
|
|
tbsp.header->lumps[l].fileofs = data - (byte *) tbsp.header; \
|
|
|
|
tbsp.header->lumps[l].filelen = tbsp.num##n * sizeof (bsp->n[0]); \
|
|
|
|
memcpy (data, bsp->n, tbsp.header->lumps[l].filelen); \
|
|
|
|
data += ROUND (tbsp.header->lumps[l].filelen); \
|
|
|
|
} else {\
|
|
|
|
tbsp.n = 0; \
|
|
|
|
tbsp.header->lumps[l].fileofs = 0; \
|
|
|
|
tbsp.header->lumps[l].filelen = 0; \
|
|
|
|
} \
|
2002-09-19 05:35:17 +00:00
|
|
|
} while (0)
|
2002-08-25 23:10:57 +00:00
|
|
|
|
2010-08-22 13:29:26 +00:00
|
|
|
tbsp.header->version = BSPVERSION;
|
2002-09-19 05:35:17 +00:00
|
|
|
|
2010-08-22 13:29:26 +00:00
|
|
|
data = (byte *) &tbsp.header[1];
|
2002-09-19 05:35:17 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
#undef SET_LUMP
|
|
|
|
#define SET_LUMP(l,n) \
|
|
|
|
do { \
|
2010-08-22 13:29:26 +00:00
|
|
|
tbsp.n##size = bsp->n##size; \
|
|
|
|
if (tbsp.n##size) { \
|
|
|
|
tbsp.n = (void *) data; \
|
|
|
|
tbsp.header->lumps[l].fileofs = data - (byte *) tbsp.header; \
|
|
|
|
tbsp.header->lumps[l].filelen = tbsp.n##size; \
|
|
|
|
memcpy (data, bsp->n, bsp->n##size); \
|
|
|
|
data += ROUND (tbsp.header->lumps[l].filelen); \
|
|
|
|
} else {\
|
|
|
|
tbsp.n = 0; \
|
|
|
|
tbsp.header->lumps[l].fileofs = 0; \
|
|
|
|
tbsp.header->lumps[l].filelen = 0; \
|
|
|
|
} \
|
2002-09-19 05:35:17 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
SET_LUMP (LUMP_LIGHTING, lightdata);
|
|
|
|
SET_LUMP (LUMP_VISIBILITY, visdata);
|
|
|
|
SET_LUMP (LUMP_ENTITIES, entdata);
|
|
|
|
SET_LUMP (LUMP_TEXTURES, texdata);
|
|
|
|
|
2010-08-23 02:56:43 +00:00
|
|
|
swap_bsp (&tbsp, 1, 0, 0);
|
2010-08-22 13:29:26 +00:00
|
|
|
|
|
|
|
Qwrite (file, tbsp.header, size);
|
|
|
|
free (tbsp.header);
|
2002-08-25 23:10:57 +00:00
|
|
|
}
|
2002-09-19 20:39:33 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE bsp_t *
|
2002-09-19 20:39:33 +00:00
|
|
|
BSP_New (void)
|
|
|
|
{
|
|
|
|
return calloc (1, sizeof (bsp_t));
|
|
|
|
}
|
|
|
|
|
2010-08-23 00:04:21 +00:00
|
|
|
VISIBLE void
|
|
|
|
BSP_Free (bsp_t *bsp)
|
|
|
|
{
|
|
|
|
#define FREE(X) \
|
|
|
|
do { \
|
|
|
|
if (bsp->own_##X && bsp->X) \
|
|
|
|
free (bsp->X); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
FREE (models);
|
|
|
|
FREE (visdata);
|
|
|
|
FREE (lightdata);
|
|
|
|
FREE (texdata);
|
|
|
|
FREE (entdata);
|
|
|
|
FREE (leafs);
|
|
|
|
FREE (planes);
|
|
|
|
FREE (vertexes);
|
|
|
|
FREE (nodes);
|
|
|
|
FREE (texinfo);
|
|
|
|
FREE (faces);
|
|
|
|
FREE (clipnodes);
|
|
|
|
FREE (edges);
|
|
|
|
FREE (marksurfaces);
|
|
|
|
FREE (surfedges);
|
|
|
|
FREE (header);
|
|
|
|
|
|
|
|
free (bsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define REALLOC(X) \
|
|
|
|
do { \
|
|
|
|
if (!bsp->own_##X) { \
|
|
|
|
bsp->own_##X = 1; \
|
|
|
|
bsp->X = 0; \
|
|
|
|
} \
|
|
|
|
bsp->X = realloc (bsp->X, (bsp->num##X + 1) * sizeof (bsp->X[0])); \
|
|
|
|
} while (0)
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddPlane (bsp_t *bsp, dplane_t *plane)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (planes);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->planes[bsp->numplanes++] = *plane;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddLeaf (bsp_t *bsp, dleaf_t *leaf)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (leafs);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->leafs[bsp->numleafs++] = *leaf;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddVertex (bsp_t *bsp, dvertex_t *vertex)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (vertexes);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->vertexes[bsp->numvertexes++] = *vertex;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddNode (bsp_t *bsp, dnode_t *node)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (nodes);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->nodes[bsp->numnodes++] = *node;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-09-19 20:39:33 +00:00
|
|
|
BSP_AddTexinfo (bsp_t *bsp, texinfo_t *texinfo)
|
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (texinfo);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->texinfo[bsp->numtexinfo++] = *texinfo;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddFace (bsp_t *bsp, dface_t *face)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (faces);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->faces[bsp->numfaces++] = *face;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddClipnode (bsp_t *bsp, dclipnode_t *clipnode)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (clipnodes);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->clipnodes[bsp->numclipnodes++] = *clipnode;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2010-08-21 23:40:31 +00:00
|
|
|
BSP_AddMarkSurface (bsp_t *bsp, int marksurface)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (marksurfaces);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->marksurfaces[bsp->nummarksurfaces++] = marksurface;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddSurfEdge (bsp_t *bsp, int surfedge)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (surfedges);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->surfedges[bsp->numsurfedges++] = surfedge;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddEdge (bsp_t *bsp, dedge_t *edge)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (edges);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->edges[bsp->numedges++] = *edge;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-11-05 19:12:51 +00:00
|
|
|
BSP_AddModel (bsp_t *bsp, dmodel_t *model)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
REALLOC (models);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->models[bsp->nummodels++] = *model;
|
|
|
|
}
|
|
|
|
|
2010-08-23 00:04:21 +00:00
|
|
|
#define OWN(X) \
|
|
|
|
do { \
|
|
|
|
FREE(X); \
|
|
|
|
bsp->own_##X = 1; \
|
|
|
|
} while (0)
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2010-08-21 23:40:31 +00:00
|
|
|
BSP_AddLighting (bsp_t *bsp, byte *lightdata, size_t lightdatasize)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
OWN (lightdata);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->lightdatasize = lightdatasize;
|
|
|
|
bsp->lightdata = malloc (lightdatasize);
|
|
|
|
memcpy (bsp->lightdata, lightdata, lightdatasize);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2010-08-21 23:40:31 +00:00
|
|
|
BSP_AddVisibility (bsp_t *bsp, byte *visdata, size_t visdatasize)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
OWN (visdata);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->visdatasize = visdatasize;
|
|
|
|
bsp->visdata = malloc (visdatasize);
|
|
|
|
memcpy (bsp->visdata, visdata, visdatasize);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2010-08-21 23:40:31 +00:00
|
|
|
BSP_AddEntities (bsp_t *bsp, char *entdata, size_t entdatasize)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
OWN (entdata);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->entdatasize = entdatasize;
|
|
|
|
bsp->entdata = malloc (entdatasize);
|
|
|
|
memcpy (bsp->entdata, entdata, entdatasize);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2010-08-21 23:40:31 +00:00
|
|
|
BSP_AddTextures (bsp_t *bsp, byte *texdata, size_t texdatasize)
|
2002-09-19 20:39:33 +00:00
|
|
|
{
|
2010-08-23 00:04:21 +00:00
|
|
|
OWN (texdata);
|
2002-09-19 20:39:33 +00:00
|
|
|
bsp->texdatasize = texdatasize;
|
|
|
|
bsp->texdata = malloc (texdatasize);
|
|
|
|
memcpy (bsp->texdata, texdata, texdatasize);
|
|
|
|
}
|