return length / size;
}
+int CopyLump_Allocate( bspHeader_t *header, int lump, void **dest, int size, int *allocationVariable )
+{
+ int length, offset;
+
+ /* get lump length and offset */
+ *allocationVariable = header->lumps[ lump ].length / size;
+ *dest = realloc(*dest, size * *allocationVariable);
+ return CopyLump(header, lump, *dest, size);
+}
/*
/* copy */
in = GetLump( (bspHeader_t*) header, LUMP_BRUSHSIDES );
- out = bspBrushSides;
for( i = 0; i < numBSPBrushSides; i++ )
{
+ AUTOEXPAND_BY_REALLOC(bspBrushSides, i, allocatedBSPBrushSides, 1024);
+ out = &bspBrushSides[i];
out->planeNum = in->planeNum;
out->shaderNum = in->shaderNum;
out->surfaceNum = -1;
in++;
- out++;
}
}
Error( "%s is version %d, not %d", filename, header->version, game->bspVersion );
/* load/convert lumps */
- numBSPShaders = CopyLump( (bspHeader_t*) header, LUMP_SHADERS, bspShaders, sizeof( bspShader_t ) );
+ numBSPShaders = CopyLump_Allocate( (bspHeader_t*) header, LUMP_SHADERS, (void **) &bspShaders, sizeof( bspShader_t ), &allocatedBSPShaders );
- numBSPModels = CopyLump( (bspHeader_t*) header, LUMP_MODELS, bspModels, sizeof( bspModel_t ) );
+ numBSPModels = CopyLump_Allocate( (bspHeader_t*) header, LUMP_MODELS, (void **) &bspModels, sizeof( bspModel_t ), &allocatedBSPModels );
numBSPPlanes = CopyLump( (bspHeader_t*) header, LUMP_PLANES, bspPlanes, sizeof( bspPlane_t ) );
numBSPLeafs = CopyLump( (bspHeader_t*) header, LUMP_LEAFS, bspLeafs, sizeof( bspLeaf_t ) );
- numBSPNodes = CopyLump( (bspHeader_t*) header, LUMP_NODES, bspNodes, sizeof( bspNode_t ) );
+ numBSPNodes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_NODES, (void **) &bspNodes, sizeof( bspNode_t ), &allocatedBSPNodes );
- numBSPLeafSurfaces = CopyLump( (bspHeader_t*) header, LUMP_LEAFSURFACES, bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ) );
+ numBSPLeafSurfaces = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFSURFACES, (void **) &bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ), &allocatedBSPLeafSurfaces );
- numBSPLeafBrushes = CopyLump( (bspHeader_t*) header, LUMP_LEAFBRUSHES, bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ) );
+ numBSPLeafBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFBRUSHES, (void **) &bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ), &allocatedBSPLeafBrushes );
- numBSPBrushes = CopyLump( (bspHeader_t*) header, LUMP_BRUSHES, bspBrushes, sizeof( bspBrush_t ) );
+ numBSPBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_BRUSHES, (void **) &bspBrushes, sizeof( bspBrush_t ), &allocatedBSPLeafBrushes );
CopyBrushSidesLump( header );
Error( "%s is version %d, not %d", filename, header->version, game->bspVersion );
/* load/convert lumps */
- numBSPShaders = CopyLump( (bspHeader_t*) header, LUMP_SHADERS, bspShaders, sizeof( bspShader_t ) );
+ numBSPShaders = CopyLump_Allocate( (bspHeader_t*) header, LUMP_SHADERS, (void **) &bspShaders, sizeof( bspShader_t ), &allocatedBSPShaders );
- numBSPModels = CopyLump( (bspHeader_t*) header, LUMP_MODELS, bspModels, sizeof( bspModel_t ) );
+ numBSPModels = CopyLump_Allocate( (bspHeader_t*) header, LUMP_MODELS, (void **) &bspModels, sizeof( bspModel_t ), &allocatedBSPModels );
numBSPPlanes = CopyLump( (bspHeader_t*) header, LUMP_PLANES, bspPlanes, sizeof( bspPlane_t ) );
numBSPLeafs = CopyLump( (bspHeader_t*) header, LUMP_LEAFS, bspLeafs, sizeof( bspLeaf_t ) );
- numBSPNodes = CopyLump( (bspHeader_t*) header, LUMP_NODES, bspNodes, sizeof( bspNode_t ) );
+ numBSPNodes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_NODES, (void **) &bspNodes, sizeof( bspNode_t ), &allocatedBSPNodes );
- numBSPLeafSurfaces = CopyLump( (bspHeader_t*) header, LUMP_LEAFSURFACES, bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ) );
+ numBSPLeafSurfaces = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFSURFACES, (void **) &bspLeafSurfaces, sizeof( bspLeafSurfaces[ 0 ] ), &allocatedBSPLeafSurfaces );
- numBSPLeafBrushes = CopyLump( (bspHeader_t*) header, LUMP_LEAFBRUSHES, bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ) );
+ numBSPLeafBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_LEAFBRUSHES, (void **) &bspLeafBrushes, sizeof( bspLeafBrushes[ 0 ] ), &allocatedBSPLeafBrushes );
- numBSPBrushes = CopyLump( (bspHeader_t*) header, LUMP_BRUSHES, bspBrushes, sizeof( bspBrush_t ) );
+ numBSPBrushes = CopyLump_Allocate( (bspHeader_t*) header, LUMP_BRUSHES, &bspBrushes, sizeof( bspBrush_t ), &allocatedBSPLeafBrushes );
numBSPBrushSides = CopyLump( (bspHeader_t*) header, LUMP_BRUSHSIDES, bspBrushSides, sizeof( bspBrushSide_t ) );
*/
int numSortShaders;
-mapDrawSurface_t *surfsOnShader[ MAX_MAP_SHADERS ];
+mapDrawSurface_t **surfsOnShader;
+int allocatedSurfsOnShader;
int allocated[ LIGHTMAP_WIDTH ];
/* new shader */
if( j == numSortShaders )
{
- if( numSortShaders >= MAX_MAP_SHADERS )
- Error( "MAX_MAP_SHADERS" );
+ EXPAND_BY_REALLOC(surfsOnShader, numSortShaders, allocatedSurfsOnShader, 1024);
surfsOnShader[ j ] = ds;
ds->nextOnShader = NULL;
numSortShaders++;
/* new shader */
if( j == numSortShaders )
{
- if( numSortShaders >= MAX_MAP_SHADERS )
- Error( "MAX_MAP_SHADERS" );
+ EXPAND_BY_REALLOC(surfsOnShader, numSortShaders, allocatedSurfsOnShader, 1024);
surfsOnShader[ j ] = ds;
ds->nextOnShader = NULL;
numSortShaders++;
#define MAX_LIGHTMAP_SHADERS 256
/* ok to increase these at the expense of more memory */
-#define MAX_MAP_MODELS 0x400
-#define MAX_MAP_BRUSHES 0x10000
#define MAX_MAP_ENTITIES 0x1000 //% 0x800 /* ydnar */
#define MAX_MAP_ENTSTRING 0x80000 //% 0x40000 /* ydnar */
-#define MAX_MAP_SHADERS 0x800 //% 0x400 /* ydnar */
#define MAX_MAP_AREAS 0x100 /* MAX_MAP_AREA_BYTES in q_shared must match! */
#define MAX_MAP_FOGS 30 //& 0x100 /* RBSP (32 - world fog - goggles) */
#define MAX_MAP_PLANES 0x200000 //% 0x20000 /* ydnar for md */
-#define MAX_MAP_NODES 0x20000
-#define MAX_MAP_BRUSHSIDES 0x100000 //% 0x20000 /* ydnar */
#define MAX_MAP_LEAFS 0x20000
-#define MAX_MAP_LEAFFACES 0x100000 //% 0x20000 /* ydnar */
-#define MAX_MAP_LEAFBRUSHES 0x40000
#define MAX_MAP_PORTALS 0x20000
#define MAX_MAP_LIGHTING 0x800000
#define MAX_MAP_LIGHTGRID 0x100000 //% 0x800000 /* ydnar: set to points, not bytes */
int GetLumpElements( bspHeader_t *header, int lump, int size );
void *GetLump( bspHeader_t *header, int lump );
int CopyLump( bspHeader_t *header, int lump, void *dest, int size );
+int CopyLump_Allocate( bspHeader_t *header, int lump, void **dest, int size, int *allocationVariable );
void AddLump( FILE *file, bspHeader_t *header, int lumpNum, const void *data, int length );
void LoadBSPFile( const char *filename );
Q_EXTERN entity_t entities[ MAX_MAP_ENTITIES ];
Q_EXTERN int numBSPModels Q_ASSIGN( 0 );
-Q_EXTERN bspModel_t bspModels[ MAX_MAP_MODELS ];
+Q_EXTERN int allocatedBSPModels Q_ASSIGN( 0 );
+Q_EXTERN bspModel_t* bspModels Q_ASSIGN(NULL);
Q_EXTERN int numBSPShaders Q_ASSIGN( 0 );
-Q_EXTERN bspShader_t bspShaders[ MAX_MAP_MODELS ];
+Q_EXTERN int allocatedBSPShaders Q_ASSIGN( 0 );
+Q_EXTERN bspShader_t* bspShaders Q_ASSIGN(0);
Q_EXTERN int bspEntDataSize Q_ASSIGN( 0 );
Q_EXTERN char bspEntData[ MAX_MAP_ENTSTRING ];
Q_EXTERN bspPlane_t bspPlanes[ MAX_MAP_PLANES ];
Q_EXTERN int numBSPNodes Q_ASSIGN( 0 );
-Q_EXTERN bspNode_t bspNodes[ MAX_MAP_NODES ];
+Q_EXTERN int allocatedBSPNodes Q_ASSIGN( 0 );
+Q_EXTERN bspNode_t* bspNodes Q_ASSIGN(NULL);
Q_EXTERN int numBSPLeafSurfaces Q_ASSIGN( 0 );
-Q_EXTERN int bspLeafSurfaces[ MAX_MAP_LEAFFACES ];
+Q_EXTERN int allocatedBSPLeafSurfaces Q_ASSIGN( 0 );
+Q_EXTERN int* bspLeafSurfaces Q_ASSIGN(NULL);
Q_EXTERN int numBSPLeafBrushes Q_ASSIGN( 0 );
-Q_EXTERN int bspLeafBrushes[ MAX_MAP_LEAFBRUSHES ];
+Q_EXTERN int allocatedBSPLeafBrushes Q_ASSIGN( 0 );
+Q_EXTERN int* bspLeafBrushes Q_ASSIGN(0);
Q_EXTERN int numBSPBrushes Q_ASSIGN( 0 );
-Q_EXTERN bspBrush_t bspBrushes[ MAX_MAP_BRUSHES ];
+Q_EXTERN int allocatedBSPBrushes Q_ASSIGN( 0 );
+Q_EXTERN bspBrush_t* bspBrushes Q_ASSIGN(NULL);
Q_EXTERN int numBSPBrushSides Q_ASSIGN( 0 );
-Q_EXTERN bspBrushSide_t bspBrushSides[ MAX_MAP_BRUSHSIDES ];
+Q_EXTERN int allocatedBSPBrushSides Q_ASSIGN( 0 );
+Q_EXTERN bspBrushSide_t* bspBrushSides Q_ASSIGN(NULL);
Q_EXTERN int numBSPLightBytes Q_ASSIGN( 0 );
Q_EXTERN byte *bspLightBytes Q_ASSIGN( NULL );
Q_EXTERN int numBSPAds Q_ASSIGN( 0 );
Q_EXTERN bspAdvertisement_t bspAds[ MAX_MAP_ADVERTISEMENTS ];
+#define AUTOEXPAND_BY_REALLOC(ptr, reqitem, allocated, def) \
+ do \
+ { \
+ if(reqitem >= allocated) \
+ { \
+ if(allocated == 0) \
+ allocated = def; \
+ while(reqitem >= allocated && allocated) \
+ allocated *= 2; \
+ if(allocated > 2147483647 / sizeof(*ptr)) \
+ { \
+ Error(#ptr " over 2 GB"); \
+ } \
+ ptr = realloc(ptr, sizeof(*ptr) * allocated); \
+ if(!ptr) \
+ Error(#ptr " out of memory"); \
+ } \
+ } \
+ while(0)
+
+#define AUTOEXPAND_BY_REALLOC_BSP(suffix, def) AUTOEXPAND_BY_REALLOC(bsp##suffix, numBSP##suffix, allocatedBSP##suffix, def)
+
/* end marker */
#endif
if( !Q_stricmp( shader, bspShaders[ i ].shader ) )
return i;
}
+
+ // i == numBSPShaders
/* get shaderinfo */
si = ShaderInfoForShader( shader );
/* emit a new shader */
- if( i == MAX_MAP_SHADERS )
- Error( "MAX_MAP_SHADERS" );
+ AUTOEXPAND_BY_REALLOC_BSP(Shaders, 1024);
+
numBSPShaders++;
strcpy( bspShaders[ i ].shader, shader );
bspShaders[ i ].surfaceFlags = si->surfaceFlags;
//% if( b->guard != 0xDEADBEEF )
//% Sys_Printf( "Brush %6d: 0x%08X Guard: 0x%08X Next: 0x%08X Original: 0x%08X Sides: %d\n", b->brushNum, b, b, b->next, b->original, b->numsides );
- if( numBSPLeafBrushes >= MAX_MAP_LEAFBRUSHES )
- Error( "MAX_MAP_LEAFBRUSHES" );
+ AUTOEXPAND_BY_REALLOC_BSP(LeafBrushes, 1024);
bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
numBSPLeafBrushes++;
}
leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
{
- if( numBSPLeafSurfaces >= MAX_MAP_LEAFFACES )
- Error( "MAX_MAP_LEAFFACES" );
+ AUTOEXPAND_BY_REALLOC_BSP(LeafSurfaces, 1024);
bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
numBSPLeafSurfaces++;
}
}
/* emit a node */
- if( numBSPNodes == MAX_MAP_NODES )
- Error( "MAX_MAP_NODES" );
+ AUTOEXPAND_BY_REALLOC_BSP(Nodes, 1024);
n = &bspNodes[ numBSPNodes ];
numBSPNodes++;
for( b = brushes; b != NULL; b = b->next )
{
/* check limits */
- if( numBSPBrushes == MAX_MAP_BRUSHES )
- Error( "MAX_MAP_BRUSHES (%d)", numBSPBrushes );
+ AUTOEXPAND_BY_REALLOC_BSP(Brushes, 1024);
/* get bsp brush */
b->outputNum = numBSPBrushes;
b->sides[ j ].outputNum = -1;
/* check count */
- if( numBSPBrushSides == MAX_MAP_BRUSHSIDES )
- Error( "MAX_MAP_BRUSHSIDES ");
+ AUTOEXPAND_BY_REALLOC_BSP(BrushSides, 1024);
/* emit side */
b->sides[ j ].outputNum = numBSPBrushSides;
/* test limits */
- if( numBSPModels == MAX_MAP_MODELS )
- Error( "MAX_MAP_MODELS" );
+ AUTOEXPAND_BY_REALLOC_BSP(Models, 256);
/* get model and entity */
mod = &bspModels[ numBSPModels ];