diff --git a/tools/quake3/common/inout.cpp b/tools/quake3/common/inout.cpp index 6a5a181e..7b95f950 100644 --- a/tools/quake3/common/inout.cpp +++ b/tools/quake3/common/inout.cpp @@ -194,7 +194,6 @@ void xml_Winding( const char *msg, const Vector3 p[], int numpoints, bool die ){ char buf[WINDING_BUFSIZE]; char smlbuf[128]; char level[2]; - int i; node = xmlNewNode( NULL, (const xmlChar*)"windingmsg" ); xmlNodeAddContent( node, (const xmlChar*)msg ); @@ -203,11 +202,11 @@ void xml_Winding( const char *msg, const Vector3 p[], int numpoints, bool die ){ xmlSetProp( node, (const xmlChar*)"level", (const xmlChar *)level ); // a 'winding' node sprintf( buf, "%i ", numpoints ); - for ( i = 0; i < numpoints; i++ ) + for ( int i = 0; i < numpoints; ++i ) { sprintf( smlbuf, "(%g %g %g)", p[i][0], p[i][1], p[i][2] ); // don't overflow - if ( strlen( buf ) + strlen( smlbuf ) > WINDING_BUFSIZE ) { + if ( strlen( buf ) + strlen( smlbuf ) >= WINDING_BUFSIZE ) { break; } strcat( buf, smlbuf ); diff --git a/tools/quake3/common/scriplib.cpp b/tools/quake3/common/scriplib.cpp index 92377bab..11629297 100644 --- a/tools/quake3/common/scriplib.cpp +++ b/tools/quake3/common/scriplib.cpp @@ -315,11 +315,9 @@ void MatchToken( const char *match ) { template void Parse1DMatrix( int x, T *m ) { - int i; - MatchToken( "(" ); - for ( i = 0 ; i < x ; i++ ) { + for ( int i = 0; i < x; ++i ) { GetToken( false ); m[i] = atof( token ); } @@ -330,11 +328,9 @@ template void Parse1DMatrix( int x, float *m ); template void Parse1DMatrix( int x, double *m ); void Parse2DMatrix( int y, int x, float *m ) { - int i; - MatchToken( "(" ); - for ( i = 0 ; i < y ; i++ ) { + for ( int i = 0; i < y; ++i ) { Parse1DMatrix( x, m + i * x ); } @@ -342,11 +338,9 @@ void Parse2DMatrix( int y, int x, float *m ) { } void Parse3DMatrix( int z, int y, int x, float *m ) { - int i; - MatchToken( "(" ); - for ( i = 0 ; i < z ; i++ ) { + for ( int i = 0; i < z; ++i ) { Parse2DMatrix( y, x, m + i * x * y ); } @@ -355,10 +349,8 @@ void Parse3DMatrix( int z, int y, int x, float *m ) { void Write1DMatrix( FILE *f, int x, float *m ) { - int i; - fprintf( f, "( " ); - for ( i = 0 ; i < x ; i++ ) { + for ( int i = 0; i < x; ++i ) { if ( m[i] == (int)m[i] ) { fprintf( f, "%i ", (int)m[i] ); } @@ -370,10 +362,8 @@ void Write1DMatrix( FILE *f, int x, float *m ) { } void Write2DMatrix( FILE *f, int y, int x, float *m ) { - int i; - fprintf( f, "( " ); - for ( i = 0 ; i < y ; i++ ) { + for ( int i = 0; i < y; ++i ) { Write1DMatrix( f, x, m + i * x ); fprintf( f, " " ); } @@ -382,10 +372,8 @@ void Write2DMatrix( FILE *f, int y, int x, float *m ) { void Write3DMatrix( FILE *f, int z, int y, int x, float *m ) { - int i; - fprintf( f, "(\n" ); - for ( i = 0 ; i < z ; i++ ) { + for ( int i = 0; i < z; ++i ) { Write2DMatrix( f, y, x, m + i * ( x * y ) ); } fprintf( f, ")\n" ); diff --git a/tools/quake3/common/stringfixedsize.h b/tools/quake3/common/stringfixedsize.h index 203684f7..7caf4bc8 100644 --- a/tools/quake3/common/stringfixedsize.h +++ b/tools/quake3/common/stringfixedsize.h @@ -42,8 +42,8 @@ public: } std::size_t write( const char* buffer, std::size_t length ) override { if( m_length + length < SIZE ){ - for( auto i = length; i != 0; --i ) - m_string[m_length++] = *buffer++; + std::copy_n( buffer, length, m_string + m_length ); + m_length += length; strClear( &m_string[m_length] ); } else{ diff --git a/tools/quake3/q3map2/map.cpp b/tools/quake3/q3map2/map.cpp index ed2d71d8..d3ac9623 100644 --- a/tools/quake3/q3map2/map.cpp +++ b/tools/quake3/q3map2/map.cpp @@ -300,10 +300,10 @@ void SnapPlaneImproved( Plane3f& plane, int numPoints, const Vector3 *points ){ if ( SnapNormal( plane.normal() ) ) { if ( numPoints > 0 ) { // Adjust the dist so that the provided points don't drift away. - Vector3 center( 0 ); - for ( int i = 0; i < numPoints; i++ ) + DoubleVector3 center( 0 ); + for ( const Vector3& point : Span( points, numPoints ) ) { - center += points[i]; + center += point; } center /= numPoints; plane.dist() = vector3_dot( plane.normal(), center ); @@ -1281,8 +1281,8 @@ void AdjustBrushesForOrigin( entity_t *ent ){ /* walk patch list */ for ( parseMesh_t *p = ent->patches; p != NULL; p = p->next ) { - for ( int i = 0; i < ( p->mesh.width * p->mesh.height ); i++ ) - p->mesh.verts[ i ].xyz -= ent->originbrush_origin; + for ( bspDrawVert_t& vert : Span( p->mesh.verts, p->mesh.width * p->mesh.height ) ) + vert.xyz -= ent->originbrush_origin; } } @@ -1330,11 +1330,9 @@ void SetEntityBounds( entity_t *e ){ */ void LoadEntityIndexMap( entity_t *e ){ - int i, size, numLayers, w, h; + int numLayers, w, h; const char *indexMapFilename, *shader; byte *pixels; - unsigned int *pixels32; - indexMap_t *im; /* this only works with bmodel ents */ @@ -1371,12 +1369,13 @@ void LoadEntityIndexMap( entity_t *e ){ /* handle tga image */ if ( path_extension_is( indexMapFilename, "tga" ) ) { /* load it */ + unsigned int *pixels32; Load32BitImage( indexMapFilename, &pixels32, &w, &h ); /* convert to bytes */ - size = w * h; + const int size = w * h; pixels = safe_malloc( size ); - for ( i = 0; i < size; i++ ) + for ( int i = 0; i < size; i++ ) { pixels[ i ] = ( ( pixels32[ i ] & 0xFF ) * numLayers ) / 256; if ( pixels[ i ] >= numLayers ) { @@ -1396,8 +1395,8 @@ void LoadEntityIndexMap( entity_t *e ){ //% Sys_Printf( "-------------------------------" ); /* fix up out-of-range values */ - size = w * h; - for ( i = 0; i < size; i++ ) + const int size = w * h; + for ( int i = 0; i < size; i++ ) { if ( pixels[ i ] >= numLayers ) { pixels[ i ] = numLayers - 1; @@ -1422,7 +1421,7 @@ void LoadEntityIndexMap( entity_t *e ){ } /* create a new index map */ - im = safe_malloc( sizeof( *im ) ); + indexMap_t *im = safe_malloc( sizeof( *im ) ); new ( im ) indexMap_t{}; // placement new /* set it up */ @@ -1437,7 +1436,7 @@ void LoadEntityIndexMap( entity_t *e ){ if( mapEnt->read_keyvalue( offset, "_offsets", "offsets" ) ){ /* value is a space-separated set of numbers */ /* get each value */ - for ( i = 0; i < 256 && !strEmpty( offset ); i++ ) + for ( int i = 0; i < 256 && !strEmpty( offset ); ++i ) { const char *space = strchr( offset, ' ' ); if ( space == NULL ) { diff --git a/tools/quake3/q3map2/mesh.cpp b/tools/quake3/q3map2/mesh.cpp index 154a78db..ac3a8d4a 100644 --- a/tools/quake3/q3map2/mesh.cpp +++ b/tools/quake3/q3map2/mesh.cpp @@ -94,10 +94,8 @@ void FreeMesh( mesh_t *m ) { } void PrintMesh( mesh_t *m ) { - int i, j; - - for ( i = 0 ; i < m->height ; i++ ) { - for ( j = 0 ; j < m->width ; j++ ) { + for ( int i = 0; i < m->height; ++i ) { + for ( int j = 0; j < m->width; ++j ) { Sys_Printf( "(%5.2f %5.2f %5.2f) " , m->verts[i * m->width + j].xyz[0] , m->verts[i * m->width + j].xyz[1] @@ -109,14 +107,11 @@ void PrintMesh( mesh_t *m ) { mesh_t *CopyMesh( mesh_t *mesh ) { - mesh_t *out; - int size; - - out = safe_malloc( sizeof( *out ) ); + mesh_t *out = safe_malloc( sizeof( *out ) ); out->width = mesh->width; out->height = mesh->height; - size = out->width * out->height * sizeof( *out->verts ); + const int size = out->width * out->height * sizeof( *out->verts ); out->verts = safe_malloc( size ); memcpy( out->verts, mesh->verts, size ); @@ -130,16 +125,13 @@ mesh_t *CopyMesh( mesh_t *mesh ) { */ mesh_t *TransposeMesh( mesh_t *in ) { - int w, h; - mesh_t *out; - - out = safe_malloc( sizeof( *out ) ); + mesh_t *out = safe_malloc( sizeof( *out ) ); out->width = in->height; out->height = in->width; out->verts = safe_malloc( out->width * out->height * sizeof( bspDrawVert_t ) ); - for ( h = 0 ; h < in->height ; h++ ) { - for ( w = 0 ; w < in->width ; w++ ) { + for ( int h = 0; h < in->height; ++h ) { + for ( int w = 0; w < in->width; ++w ) { out->verts[ w * in->height + h ] = in->verts[ h * in->width + w ]; } } @@ -150,14 +142,12 @@ mesh_t *TransposeMesh( mesh_t *in ) { } void InvertMesh( mesh_t *in ) { - int w, h; - bspDrawVert_t temp; - - for ( h = 0 ; h < in->height ; h++ ) { - for ( w = 0 ; w < in->width / 2 ; w++ ) { - temp = in->verts[ h * in->width + w ]; - in->verts[ h * in->width + w ] = in->verts[ h * in->width + in->width - 1 - w ]; - in->verts[ h * in->width + in->width - 1 - w ] = temp; + for ( int h = 0; h < in->height; ++h ) { + for ( int w = 0; w < in->width / 2; ++w ) { + std::swap( + in->verts[ h * in->width + w ], + in->verts[ h * in->width + in->width - 1 - w ] + ); } } } @@ -275,12 +265,9 @@ void MakeMeshNormals( mesh_t in ){ */ void PutMeshOnCurve( mesh_t in ) { - int i, j, m; - - // put all the aproximating points on the curve - for ( i = 0 ; i < in.width ; i++ ) { - for ( j = 1 ; j < in.height ; j += 2 ) { + for ( int i = 0; i < in.width; ++i ) { + for ( int j = 1; j < in.height; j += 2 ) { const int idx = j * in.width + i; const int idprev = ( j + 1 ) * in.width + i; const int idnext = ( j - 1 ) * in.width + i; @@ -289,7 +276,7 @@ void PutMeshOnCurve( mesh_t in ) { /* ydnar: interpolating st coords */ in.verts[idx].st = vector2_mid( vector2_mid( in.verts[idx].st, in.verts[idprev].st ), vector2_mid( in.verts[idx].st, in.verts[idnext].st ) ); - for ( m = 0; m < MAX_LIGHTMAPS; m++ ) + for ( int m = 0; m < MAX_LIGHTMAPS; ++m ) { in.verts[idx].lightmap[ m ] = vector2_mid( vector2_mid( in.verts[idx].lightmap[ m ], in.verts[idprev].lightmap[ m ] ), vector2_mid( in.verts[idx].lightmap[ m ], in.verts[idnext].lightmap[ m ] ) ); @@ -297,15 +284,15 @@ void PutMeshOnCurve( mesh_t in ) { } } - for ( j = 0 ; j < in.height ; j++ ) { - for ( i = 1 ; i < in.width ; i += 2 ) { + for ( int j = 0; j < in.height; ++j ) { + for ( int i = 1; i < in.width; i += 2 ) { const int idx = j * in.width + i; in.verts[idx].xyz = vector3_mid( vector3_mid( in.verts[idx].xyz, in.verts[idx + 1].xyz ), vector3_mid( in.verts[idx].xyz, in.verts[idx - 1].xyz ) ); /* ydnar: interpolating st coords */ in.verts[idx].st = vector2_mid( vector2_mid( in.verts[idx].st, in.verts[idx + 1].st ), vector2_mid( in.verts[idx].st, in.verts[idx - 1].st ) ); - for ( m = 0; m < MAX_LIGHTMAPS; m++ ) + for ( int m = 0; m < MAX_LIGHTMAPS; ++m ) { in.verts[idx].lightmap[ m ] = vector2_mid( vector2_mid( in.verts[idx].lightmap[ m ], in.verts[idx + 1].lightmap[ m ] ), vector2_mid( in.verts[idx].lightmap[ m ], in.verts[idx - 1].lightmap[ m ] ) ); @@ -538,7 +525,7 @@ mesh_t *SubdivideMesh2( mesh_t in, int iterations ){ LerpDrawVert( &expand[ j + 1 ][ i ], &expand[ j + 2 ][ i ], &next ); LerpDrawVert( &prev, &next, &mid ); - for ( k = out.height - 1; k > j + 3; k-- ) + for ( k = out.height - 1; k > j + 3; k-- ) expand[ k ][ i ] = expand[ k - 2 ][ i ]; expand[ j + 1 ][ i ] = prev; expand[ j + 2 ][ i ] = mid; diff --git a/tools/quake3/q3map2/minimap.cpp b/tools/quake3/q3map2/minimap.cpp index 9b3cc312..8d1bfc8e 100644 --- a/tools/quake3/q3map2/minimap.cpp +++ b/tools/quake3/q3map2/minimap.cpp @@ -51,13 +51,11 @@ struct minimap_t static minimap_t minimap; bool BrushIntersectionWithLine( const bspBrush_t& brush, const Vector3& start, const Vector3& dir, float *t_in, float *t_out ){ - int i; bool in = false, out = false; - bspBrushSide_t *sides = &bspBrushSides[brush.firstSide]; - for ( i = 0; i < brush.numSides; ++i ) + for ( const bspBrushSide_t& side : Span( &bspBrushSides[brush.firstSide], brush.numSides ) ) { - const bspPlane_t& p = bspPlanes[sides[i].planeNum]; + const bspPlane_t& p = bspPlanes[side.planeNum]; float sn = vector3_dot( start, p.normal() ); float dn = vector3_dot( dir, p.normal() ); if ( dn == 0 ) { diff --git a/tools/quake3/q3map2/model.cpp b/tools/quake3/q3map2/model.cpp index b0461cac..0b88fc35 100644 --- a/tools/quake3/q3map2/model.cpp +++ b/tools/quake3/q3map2/model.cpp @@ -238,8 +238,7 @@ struct AssModel } void forEachFace( std::function visitor ) const override { - for ( size_t t = 0; t < m_mesh->mNumFaces; ++t ){ - const aiFace& face = m_mesh->mFaces[t]; + for ( const aiFace& face : Span( m_mesh->mFaces, m_mesh->mNumFaces ) ){ // if( face.mNumIndices == 3 ) Vector3 xyz[3]; Vector2 st[3]; @@ -1236,8 +1235,7 @@ void InsertModel( const char *name, int skin, int frame, const Matrix4& transfor /* copy indexes */ { size_t idCopied = 0; - for ( size_t t = 0; t < mesh->mNumFaces; ++t ){ - const aiFace& face = mesh->mFaces[t]; + for ( const aiFace& face : Span( mesh->mFaces, mesh->mNumFaces ) ){ // if( face.mNumIndices == 3 ) for ( size_t i = 0; i < 3; i++ ){ ds->indexes[idCopied++] = face.mIndices[i]; diff --git a/tools/quake3/q3map2/patch.cpp b/tools/quake3/q3map2/patch.cpp index bd5d4121..c46dae8c 100644 --- a/tools/quake3/q3map2/patch.cpp +++ b/tools/quake3/q3map2/patch.cpp @@ -196,8 +196,6 @@ static void ExpandMaxIterations( int *maxIterations, int maxError, const Vector3 void ParsePatch( bool onlyLights ){ float info[ 5 ]; - int i, j, k; - parseMesh_t *pm; mesh_t m; bspDrawVert_t *verts; bool degenerate; @@ -213,24 +211,25 @@ void ParsePatch( bool onlyLights ){ Parse1DMatrix( 5, info ); m.width = info[0]; m.height = info[1]; - m.verts = verts = safe_malloc( m.width * m.height * sizeof( m.verts[0] ) ); + const int size = ( m.width * m.height ); + m.verts = verts = safe_malloc( size * sizeof( m.verts[0] ) ); if ( m.width < 0 || m.width > MAX_PATCH_SIZE || m.height < 0 || m.height > MAX_PATCH_SIZE ) { Error( "ParsePatch: bad size" ); } MatchToken( "(" ); - for ( j = 0; j < m.width ; j++ ) + for ( int j = 0; j < m.width; ++j ) { MatchToken( "(" ); - for ( i = 0; i < m.height ; i++ ) + for ( int i = 0; i < m.height; ++i ) { Parse1DMatrix( 5, verts[ i * m.width + j ].xyz.data() ); /* ydnar: fix colors */ - for ( k = 0; k < MAX_LIGHTMAPS; k++ ) + for ( auto& color : verts[ i * m.width + j ].color ) { - verts[ i * m.width + j ].color[ k ].set( 255 ); + color.set( 255 ); } } MatchToken( ")" ); @@ -257,12 +256,11 @@ void ParsePatch( bool onlyLights ){ /* ydnar: delete and warn about degenerate patches */ - j = ( m.width * m.height ); Vector4 delta( 0, 0, 0, 0 ); degenerate = true; /* find first valid vector */ - for ( i = 1; i < j && delta[ 3 ] == 0; i++ ) + for ( int i = 1; i < size && delta[ 3 ] == 0; ++i ) { delta.vec3() = m.verts[ 0 ].xyz - m.verts[ i ].xyz; delta[ 3 ] = VectorNormalize( delta.vec3() ); @@ -275,7 +273,7 @@ void ParsePatch( bool onlyLights ){ else { /* if all vectors match this or are zero, then this is a degenerate patch */ - for ( i = 1; i < j && degenerate; i++ ) + for ( int i = 1; i < size && degenerate; ++i ) { Vector4 delta2( m.verts[ 0 ].xyz - m.verts[ i ].xyz, 0 ); delta2[ 3 ] = VectorNormalize( delta2.vec3() ); @@ -302,9 +300,9 @@ void ParsePatch( bool onlyLights ){ /* find longest curve on the mesh */ longestCurve = 0.0f; maxIterations = 0; - for ( j = 0; j + 2 < m.width; j += 2 ) + for ( int j = 0; j + 2 < m.width; j += 2 ) { - for ( i = 0; i + 2 < m.height; i += 2 ) + for ( int i = 0; i + 2 < m.height; i += 2 ) { ExpandLongestCurve( &longestCurve, verts[ i * m.width + j ].xyz, verts[ i * m.width + ( j + 1 ) ].xyz, verts[ i * m.width + ( j + 2 ) ].xyz ); /* row */ ExpandLongestCurve( &longestCurve, verts[ i * m.width + j ].xyz, verts[ ( i + 1 ) * m.width + j ].xyz, verts[ ( i + 2 ) * m.width + j ].xyz ); /* col */ @@ -314,7 +312,7 @@ void ParsePatch( bool onlyLights ){ } /* allocate patch mesh */ - pm = safe_calloc( sizeof( *pm ) ); + parseMesh_t *pm = safe_calloc( sizeof( *pm ) ); /* ydnar: add entity/brush numbering */ pm->entityNum = mapEnt->mapEntityNum; @@ -343,10 +341,6 @@ void ParsePatch( bool onlyLights ){ */ static void GrowGroup_r( parseMesh_t *pm, int patchNum, int patchCount, parseMesh_t **meshes, byte *bordering, byte *group ){ - int i; - const byte *row; - - /* early out check */ if ( group[ patchNum ] ) { return; @@ -355,14 +349,14 @@ static void GrowGroup_r( parseMesh_t *pm, int patchNum, int patchCount, parseMes /* set it */ group[ patchNum ] = 1; - row = bordering + patchNum * patchCount; + const byte *row = bordering + patchNum * patchCount; /* check maximums */ value_maximize( pm->longestCurve, meshes[ patchNum ]->longestCurve ); value_maximize( pm->maxIterations, meshes[ patchNum ]->maxIterations ); /* walk other patches */ - for ( i = 0; i < patchCount; i++ ) + for ( int i = 0; i < patchCount; ++i ) { if ( row[ i ] ) { GrowGroup_r( pm, i, patchCount, meshes, bordering, group ); diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index dea0c383..1cc32a0f 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -1741,7 +1741,6 @@ void MakeEntityDecals( entity_t *e ); std::array TextureAxisFromPlane( const plane_t& plane ); /* vis.c */ -fixedWinding_t *NewFixedWinding( int points ); int VisMain( Args& args ); /* visflow.c */ diff --git a/tools/quake3/q3map2/shaders.cpp b/tools/quake3/q3map2/shaders.cpp index 34d05d2e..5795e50b 100644 --- a/tools/quake3/q3map2/shaders.cpp +++ b/tools/quake3/q3map2/shaders.cpp @@ -40,13 +40,6 @@ */ void ColorMod( colorMod_t *cm, int numVerts, bspDrawVert_t *drawVerts ){ - int i, j; - float c; - Color4f mult, add; - bspDrawVert_t *dv; - colorMod_t *cm2; - - /* dummy check */ if ( cm == NULL || numVerts < 1 || drawVerts == NULL ) { return; @@ -54,83 +47,81 @@ void ColorMod( colorMod_t *cm, int numVerts, bspDrawVert_t *drawVerts ){ /* walk vertex list */ - for ( i = 0; i < numVerts; i++ ) + for ( bspDrawVert_t& dv : Span( drawVerts, numVerts ) ) { - /* get vertex */ - dv = &drawVerts[ i ]; - /* walk colorMod list */ - for ( cm2 = cm; cm2 != NULL; cm2 = cm2->next ) + for ( ; cm != NULL; cm = cm->next ) { + float c; /* default */ - mult.set( 1 ); - add.set( 0 ); + Color4f mult( 1, 1, 1, 1 ); + Color4f add( 0, 0, 0, 0 ); - const Vector3 cm2_vec3 = vector3_from_array( cm2->data ); + const Vector3 cm_vec3 = vector3_from_array( cm->data ); /* switch on type */ - switch ( cm2->type ) + switch ( cm->type ) { case EColorMod::ColorSet: mult.rgb().set( 0 ); - add.rgb() = cm2_vec3 * 255.0f; + add.rgb() = cm_vec3 * 255.0f; break; case EColorMod::AlphaSet: mult.alpha() = 0.0f; - add.alpha() = cm2->data[ 0 ] * 255.0f; + add.alpha() = cm->data[ 0 ] * 255.0f; break; case EColorMod::ColorScale: - mult.rgb() = cm2_vec3; + mult.rgb() = cm_vec3; break; case EColorMod::AlphaScale: - mult.alpha() = cm2->data[ 0 ]; + mult.alpha() = cm->data[ 0 ]; break; case EColorMod::ColorDotProduct: - c = vector3_dot( dv->normal, cm2_vec3 ); + c = vector3_dot( dv.normal, cm_vec3 ); mult.rgb().set( c ); break; case EColorMod::ColorDotProductScale: - c = vector3_dot( dv->normal, cm2_vec3 ); - c = ( c - cm2->data[3] ) / ( cm2->data[4] - cm2->data[3] ); + c = vector3_dot( dv.normal, cm_vec3 ); + c = ( c - cm->data[3] ) / ( cm->data[4] - cm->data[3] ); mult.rgb().set( c ); break; case EColorMod::AlphaDotProduct: - mult.alpha() = vector3_dot( dv->normal, cm2_vec3 ); + mult.alpha() = vector3_dot( dv.normal, cm_vec3 ); break; case EColorMod::AlphaDotProductScale: - c = vector3_dot( dv->normal, cm2_vec3 ); - c = ( c - cm2->data[3] ) / ( cm2->data[4] - cm2->data[3] ); + c = vector3_dot( dv.normal, cm_vec3 ); + c = ( c - cm->data[3] ) / ( cm->data[4] - cm->data[3] ); mult.alpha() = c; break; case EColorMod::ColorDotProduct2: - c = vector3_dot( dv->normal, cm2_vec3 ); + c = vector3_dot( dv.normal, cm_vec3 ); c *= c; mult.rgb().set( c ); break; case EColorMod::ColorDotProduct2Scale: - c = vector3_dot( dv->normal, cm2_vec3 ); + c = vector3_dot( dv.normal, cm_vec3 ); c *= c; - c = ( c - cm2->data[3] ) / ( cm2->data[4] - cm2->data[3] ); + c = ( c - cm->data[3] ) / ( cm->data[4] - cm->data[3] ); mult.rgb().set( c ); break; case EColorMod::AlphaDotProduct2: - mult.alpha() = vector3_dot( dv->normal, cm2_vec3 ); + mult.alpha() = vector3_dot( dv.normal, cm_vec3 ); mult.alpha() *= mult.alpha(); break; case EColorMod::AlphaDotProduct2Scale: - c = vector3_dot( dv->normal, cm2_vec3 ); + c = vector3_dot( dv.normal, cm_vec3 ); c *= c; - c = ( c - cm2->data[3] ) / ( cm2->data[4] - cm2->data[3] ); + c = ( c - cm->data[3] ) / ( cm->data[4] - cm->data[3] ); mult.alpha() = c; break; @@ -139,9 +130,9 @@ void ColorMod( colorMod_t *cm, int numVerts, bspDrawVert_t *drawVerts ){ } /* apply mod */ - for ( j = 0; j < MAX_LIGHTMAPS; j++ ) + for ( auto& color : dv.color ) { - dv->color[ j ] = color_to_byte( mult * static_cast>( dv->color[ j ] ) + add ); + color = color_to_byte( mult * static_cast>( color ) + add ); } } } @@ -253,11 +244,8 @@ bool ApplySurfaceParm( const char *name, int *contentFlags, int *surfaceFlags, i } /* check custom info parms */ - for ( int i = 0; i < numCustSurfaceParms; i++ ) + for ( const surfaceParm_t& sp : Span( custSurfaceParms, numCustSurfaceParms ) ) { - /* get surfaceparm */ - const surfaceParm_t& sp = custSurfaceParms[ i ]; - /* match? */ if ( striEqual( name, sp.name ) ) { /* clear and set flags */ @@ -314,33 +302,21 @@ void BeginMapShaderFile( const char *mapFile ){ */ void WriteMapShaderFile( void ){ - FILE *file; - shaderInfo_t *si; - int i, num; - - /* dummy check */ if ( mapShaderFile.empty() ) { return; } /* are there any custom shaders? */ - for ( i = 0, num = 0; i < numShaderInfo; i++ ) - { - if ( shaderInfo[ i ].custom ) { - break; - } - } - if ( i == numShaderInfo ) { + if( std::none_of( shaderInfo, shaderInfo + numShaderInfo, []( const shaderInfo_t& si ){ return si.custom; } ) ) return; - } /* note it */ Sys_FPrintf( SYS_VRB, "--- WriteMapShaderFile ---\n" ); Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile.c_str() ); /* open shader file */ - file = fopen( mapShaderFile.c_str(), "wt" ); + FILE *file = fopen( mapShaderFile.c_str(), "wt" ); if ( file == NULL ) { Sys_Warning( "Unable to open map shader file %s for writing\n", mapShaderFile.c_str() ); return; @@ -354,20 +330,18 @@ void WriteMapShaderFile( void ){ mapName.c_str() ); /* walk the shader list */ - for ( i = 0, num = 0; i < numShaderInfo; i++ ) + int num = 0; + for ( const shaderInfo_t& si : Span( shaderInfo, numShaderInfo ) ) { - /* get the shader and print it */ - si = &shaderInfo[ i ]; - if ( !si->custom || strEmptyOrNull( si->shaderText ) ) { - continue; + if ( si.custom && !strEmptyOrNull( si.shaderText ) ) { + num++; + + /* print it to the file */ + fprintf( file, "%s%s\n", si.shader.c_str(), si.shaderText ); + //Sys_Printf( "%s%s\n", si.shader.c_str(), si.shaderText ); /* FIXME: remove debugging code */ + + Sys_FPrintf( SYS_VRB, "." ); } - num++; - - /* print it to the file */ - fprintf( file, "%s%s\n", si->shader.c_str(), si->shaderText ); - //Sys_Printf( "%s%s\n", si->shader, si->shaderText ); /* FIXME: remove debugging code */ - - Sys_FPrintf( SYS_VRB, "." ); } /* close the shader */ diff --git a/tools/quake3/q3map2/surface.cpp b/tools/quake3/q3map2/surface.cpp index 9afeaded..5c0355f7 100644 --- a/tools/quake3/q3map2/surface.cpp +++ b/tools/quake3/q3map2/surface.cpp @@ -150,9 +150,6 @@ mapDrawSurface_t *CloneSurface( mapDrawSurface_t *src, shaderInfo_t *si ){ */ mapDrawSurface_t *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si ){ - mapDrawSurface_t *ds; - - /* dummy check */ if ( src == NULL || si == NULL ) { return NULL; @@ -165,7 +162,7 @@ mapDrawSurface_t *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si ){ } /* make a copy */ - ds = CloneSurface( src, si ); + mapDrawSurface_t *ds = CloneSurface( src, si ); if ( ds == NULL ) { return NULL; } @@ -187,17 +184,13 @@ mapDrawSurface_t *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si ){ */ mapDrawSurface_t *MakeSkyboxSurface( mapDrawSurface_t *src ){ - int i; - mapDrawSurface_t *ds; - - /* dummy check */ if ( src == NULL ) { return NULL; } /* make a copy */ - ds = CloneSurface( src, src->shaderInfo ); + mapDrawSurface_t *ds = CloneSurface( src, src->shaderInfo ); if ( ds == NULL ) { return NULL; } @@ -206,9 +199,9 @@ mapDrawSurface_t *MakeSkyboxSurface( mapDrawSurface_t *src ){ ds->parent = src; /* scale the surface vertexes */ - for ( i = 0; i < ds->numVerts; i++ ) + for ( bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) { - matrix4_transform_point( skyboxTransform, ds->verts[ i ].xyz ); + matrix4_transform_point( skyboxTransform, vert.xyz ); /* debug code */ //% bspDrawVerts[ bspDrawSurfaces[ ds->outputNum ].firstVert + i ].color[ 0 ][ 1 ] = 0; @@ -318,12 +311,12 @@ void TidyEntitySurfaces( entity_t *e ){ static Vector2 CalcSurfaceTextureBias( const mapDrawSurface_t *ds ){ /* walk the verts and determine min/max st values */ Vector2 mins( 999999, 999999 ), maxs( -999999, -999999 ), bias; - for ( int i = 0; i < ds->numVerts; i++ ) + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) { for ( int j = 0; j < 2; j++ ) { - value_minimize( mins[ j ], ds->verts[ i ].st[ j ] ); - value_maximize( maxs[ j ], ds->verts[ i ].st[ j ] ); + value_minimize( mins[ j ], vert.st[ j ] ); + value_maximize( maxs[ j ], vert.st[ j ] ); } } @@ -390,10 +383,9 @@ Vector3 CalcLightmapAxis( const Vector3& normal ){ #define PLANAR_EPSILON 0.5f //% 0.126f 0.25f void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ - int i, bestAxis; Plane3f plane; shaderInfo_t *si; - static Vector3 axii[ 6 ] = + static const Vector3 axii[ 6 ] = { { 0, 0, -1 }, { 0, 0, 1 }, @@ -431,8 +423,8 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ /* set surface bounding box */ ds->minmax.clear(); - for ( i = 0; i < ds->numVerts; i++ ) - ds->minmax.extend( ds->verts[ i ].xyz ); + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) + ds->minmax.extend( vert.xyz ); /* try to get an existing plane */ if ( ds->planeNum >= 0 ) { @@ -443,11 +435,11 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ else { plane = { 0, 0, 0, 0 }; - for ( i = 0; i < ds->numVerts; i++ ) + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) { - if ( ds->verts[ i ].normal != g_vector3_identity ) { - plane.normal() = ds->verts[ i ].normal; - plane.dist() = vector3_dot( ds->verts[ i ].xyz, plane.normal() ); + if ( vert.normal != g_vector3_identity ) { + plane.normal() = vert.normal; + plane.dist() = vector3_dot( vert.xyz, plane.normal() ); break; } } @@ -464,10 +456,10 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ ds->planar = true; /* test each vert */ - for ( i = 0; i < ds->numVerts; i++ ) + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) { /* point-plane test */ - if ( fabs( plane3_distance_to_point( plane, ds->verts[ i ].xyz ) ) > PLANAR_EPSILON ) { + if ( fabs( plane3_distance_to_point( plane, vert.xyz ) ) > PLANAR_EPSILON ) { //% if( ds->planeNum >= 0 ) //% { //% Sys_Warning( "Planar surface marked unplanar (%f > %f)\n", fabs( dist ), PLANAR_EPSILON ); @@ -519,8 +511,10 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ else { /* find best lightmap axis */ + int bestAxis; for ( bestAxis = 0; bestAxis < 6; bestAxis++ ) { + int i; for ( i = 0; i < ds->numVerts; i++ ) { //% Sys_Printf( "Comparing %1.3f %1.3f %1.3f to %1.3f %1.3f %1.3f\n", @@ -573,14 +567,11 @@ void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ */ void ClassifyEntitySurfaces( entity_t *e ){ - int i; - - /* note it */ Sys_FPrintf( SYS_VRB, "--- ClassifyEntitySurfaces ---\n" ); /* walk the surface list */ - for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ ) + for ( int i = e->firstDrawSurf; i < numMapDrawSurfs; ++i ) { FinishSurface( &mapDrawSurfs[ i ] ); ClassifySurfaces( 1, &mapDrawSurfs[ i ] ); @@ -655,21 +646,21 @@ shaderInfo_t *GetIndexedShader( const shaderInfo_t *parent, const indexMap_t *im /* determine min/max index */ byte minShaderIndex = 255; byte maxShaderIndex = 0; - for ( int i = 0; i < numPoints; i++ ) + for ( const byte index : Span( shaderIndexes, numPoints ) ) { - value_minimize( minShaderIndex, shaderIndexes[ i ] ); - value_maximize( maxShaderIndex, shaderIndexes[ i ] ); + value_minimize( minShaderIndex, index ); + value_maximize( maxShaderIndex, index ); } /* set alpha inline */ - for ( int i = 0; i < numPoints; i++ ) + for ( byte& index : Span( shaderIndexes, numPoints ) ) { /* straight rip from terrain.c */ - if ( shaderIndexes[ i ] < maxShaderIndex ) { - shaderIndexes[ i ] = 0; + if ( index < maxShaderIndex ) { + index = 0; } else{ - shaderIndexes[ i ] = 255; + index = 255; } } @@ -843,13 +834,13 @@ mapDrawSurface_t *DrawSurfaceForSide( const entity_t *e, const brush_t& b, const dv->normal = mapplanes[ s.planenum ].normal(); /* ydnar: set color */ - for ( int k = 0; k < MAX_LIGHTMAPS; k++ ) + for ( auto& color : dv->color ) { - dv->color[ k ].set( 255 ); + color.set( 255 ); /* ydnar: gs mods: handle indexed shader blending */ if( indexed ) - dv->color[ k ].alpha() = shaderIndexes[ j ]; + color.alpha() = shaderIndexes[ j ]; } } @@ -873,7 +864,7 @@ mapDrawSurface_t *DrawSurfaceForSide( const entity_t *e, const brush_t& b, const */ mapDrawSurface_t *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh ){ - int i, k, numVerts; + int i, numVerts; Plane3f plane; bool planar; mapDrawSurface_t *ds; @@ -999,8 +990,8 @@ mapDrawSurface_t *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh ds->lightmapVecs[ 2 ] = plane.normal(); /* push this normal to all verts (ydnar 2003-02-14: bad idea, small patches get screwed up) */ - for ( i = 0; i < ds->numVerts; i++ ) - ds->verts[ i ].normal = plane.normal(); + for ( bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) + vert.normal = plane.normal(); } /* walk the verts to do special stuff */ @@ -1023,13 +1014,13 @@ mapDrawSurface_t *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh } /* ydnar: set color */ - for ( k = 0; k < MAX_LIGHTMAPS; k++ ) + for ( auto& color : dv->color ) { - dv->color[ k ].set( 255 ); + color.set( 255 ); /* ydnar: gs mods: handle indexed shader blending */ if( indexed ) - dv->color[ k ].alpha() = shaderIndexes[ i ]; + color.alpha() = shaderIndexes[ i ]; } /* ydnar: offset */ @@ -1096,30 +1087,22 @@ mapDrawSurface_t *DrawSurfaceForFlare( int entNum, const Vector3& origin, const */ mapDrawSurface_t *DrawSurfaceForShader( const char *shader ){ - int i; - shaderInfo_t *si; - mapDrawSurface_t *ds; - - /* get shader */ - si = ShaderInfoForShader( shader ); + shaderInfo_t *si = ShaderInfoForShader( shader ); /* find existing surface */ - for ( i = 0; i < numMapDrawSurfs; i++ ) + for ( mapDrawSurface_t& ds : Span( mapDrawSurfs, numMapDrawSurfs ) ) { - /* get surface */ - ds = &mapDrawSurfs[ i ]; - /* check it */ - if ( ds->shaderInfo == si ) { - return ds; + if ( ds.shaderInfo == si ) { + return &ds; } } /* create a new surface */ - ds = AllocDrawSurface( ESurfaceType::Shader ); + mapDrawSurface_t *ds = AllocDrawSurface( ESurfaceType::Shader ); ds->entityNum = 0; - ds->shaderInfo = ShaderInfoForShader( shader ); + ds->shaderInfo = si; /* return to sender */ return ds; @@ -1135,8 +1118,8 @@ mapDrawSurface_t *DrawSurfaceForShader( const char *shader ){ static void AddSurfaceFlare( mapDrawSurface_t *ds, const Vector3& entityOrigin ){ Vector3 origin( 0 ); /* find centroid */ - for ( int i = 0; i < ds->numVerts; i++ ) - origin += ds->verts[ i ].xyz; + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) + origin += vert.xyz; origin /= ds->numVerts; origin += entityOrigin; @@ -1227,21 +1210,17 @@ void SubdivideFaceSurfaces( entity_t *e ){ /* note it */ Sys_FPrintf( SYS_VRB, "--- SubdivideFaceSurfaces ---\n" ); - /* walk the list of surfaces */ - const int numBaseDrawSurfs = numMapDrawSurfs; - for ( int i = e->firstDrawSurf; i < numBaseDrawSurfs; i++ ) + /* walk the list of original surfaces, numMapDrawSurfs may increase in the process */ + for ( mapDrawSurface_t& ds : Span( mapDrawSurfs + e->firstDrawSurf, numMapDrawSurfs ) ) { - /* get surface */ - mapDrawSurface_t *ds = &mapDrawSurfs[ i ]; - /* only subdivide brush sides */ - if ( ds->type != ESurfaceType::Face || ds->mapBrush == NULL || ds->sideRef == NULL || ds->sideRef->side == NULL ) { + if ( ds.type != ESurfaceType::Face || ds.mapBrush == NULL || ds.sideRef == NULL || ds.sideRef->side == NULL ) { continue; } /* get bits */ - const brush_t *brush = ds->mapBrush; - const side_t& side = *ds->sideRef->side; + const brush_t *brush = ds.mapBrush; + const side_t& side = *ds.sideRef->side; /* check subdivision for shader */ const shaderInfo_t *si = side.shaderInfo; @@ -1261,11 +1240,11 @@ void SubdivideFaceSurfaces( entity_t *e ){ } /* preserve fog num */ - const int fogNum = ds->fogNum; + const int fogNum = ds.fogNum; /* make a winding and free the surface */ - winding_t w = WindingFromDrawSurf( ds ); - ClearSurface( ds ); + winding_t w = WindingFromDrawSurf( &ds ); + ClearSurface( &ds ); /* subdivide it */ SubdivideFace_r( e, *brush, side, w, fogNum, subdivisions ); @@ -1675,7 +1654,7 @@ int AddReferenceToLeaf( mapDrawSurface_t *ds, node_t *node ){ */ int AddReferenceToTree_r( mapDrawSurface_t *ds, node_t *node, bool skybox ){ - int i, refs = 0; + int refs = 0; /* dummy check */ @@ -1699,8 +1678,8 @@ int AddReferenceToTree_r( mapDrawSurface_t *ds, node_t *node, bool skybox ){ } /* increase the leaf bounds */ - for ( i = 0; i < ds->numVerts; i++ ) - node->minmax.extend( ds->verts[ i ].xyz ); + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) + node->minmax.extend( vert.xyz ); } /* add a reference */ @@ -1914,10 +1893,10 @@ int FilterFaceIntoTree( mapDrawSurface_t *ds, tree_t& tree ){ #define FILTER_SUBDIVISION 8 static int FilterPatchIntoTree( mapDrawSurface_t *ds, tree_t& tree ){ - int x, y, refs = 0; + int refs = 0; - for ( y = 0; y + 2 < ds->patchHeight; y += 2 ) - for ( x = 0; x + 2 < ds->patchWidth; x += 2 ) + for ( int y = 0; y + 2 < ds->patchHeight; y += 2 ) + for ( int x = 0; x + 2 < ds->patchWidth; x += 2 ) { Vector3 *points[9]; points[0] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 0 )].xyz; @@ -1943,15 +1922,13 @@ static int FilterPatchIntoTree( mapDrawSurface_t *ds, tree_t& tree ){ */ static int FilterTrianglesIntoTree( mapDrawSurface_t *ds, tree_t& tree ){ - int i, refs; - + int refs = 0; /* ydnar: gs mods: this was creating bogus triangles before */ - refs = 0; - for ( i = 0; i < ds->numIndexes; i += 3 ) + for ( int i = 0; i < ds->numIndexes; i += 3 ) { /* error check */ - if ( ds->indexes[ i ] >= ds->numVerts || + if ( ds->indexes[ i + 0 ] >= ds->numVerts || ds->indexes[ i + 1 ] >= ds->numVerts || ds->indexes[ i + 2 ] >= ds->numVerts ) { Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts ); @@ -1959,15 +1936,15 @@ static int FilterTrianglesIntoTree( mapDrawSurface_t *ds, tree_t& tree ){ /* make a triangle winding and filter it into the tree */ winding_t w{ - ds->verts[ ds->indexes[ i ] ].xyz, + ds->verts[ ds->indexes[ i + 0 ] ].xyz, ds->verts[ ds->indexes[ i + 1 ] ].xyz, ds->verts[ ds->indexes[ i + 2 ] ].xyz }; refs += FilterWindingIntoTree_r( w, ds, tree.headnode ); } /* use point filtering as well */ - for ( i = 0; i < ds->numVerts; i++ ) - refs += FilterPointIntoTree_r( ds->verts[ i ].xyz, ds, tree.headnode ); + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) + refs += FilterPointIntoTree_r( vert.xyz, ds, tree.headnode ); return refs; } @@ -1995,7 +1972,7 @@ static int FilterFoliageIntoTree( mapDrawSurface_t *ds, tree_t& tree ){ for ( i = 0; i < ds->numIndexes; i += 3 ) { /* error check */ - if ( ds->indexes[ i ] >= ds->numVerts || + if ( ds->indexes[ i + 0 ] >= ds->numVerts || ds->indexes[ i + 1 ] >= ds->numVerts || ds->indexes[ i + 2 ] >= ds->numVerts ) { Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts ); @@ -2003,7 +1980,7 @@ static int FilterFoliageIntoTree( mapDrawSurface_t *ds, tree_t& tree ){ /* make a triangle winding and filter it into the tree */ winding_t w{ - instance->xyz + ds->verts[ ds->indexes[ i ] ].xyz, + instance->xyz + ds->verts[ ds->indexes[ i + 0 ] ].xyz, instance->xyz + ds->verts[ ds->indexes[ i + 1 ] ].xyz, instance->xyz + ds->verts[ ds->indexes[ i + 2 ] ].xyz }; refs += FilterWindingIntoTree_r( w, ds, tree.headnode ); @@ -2043,10 +2020,10 @@ void EmitDrawVerts( const mapDrawSurface_t *ds, bspDrawSurface_t& out ){ /* copy the verts */ out.firstVert = bspDrawVerts.size(); out.numVerts = ds->numVerts; - for ( int i = 0; i < ds->numVerts; i++ ) + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) { /* allocate a new vert */ /* copy it */ - bspDrawVert_t& dv = bspDrawVerts.emplace_back( ds->verts[ i ] ); + bspDrawVert_t& dv = bspDrawVerts.emplace_back( vert ); /* offset? */ if ( offset != 0.0f ) { @@ -2062,8 +2039,8 @@ void EmitDrawVerts( const mapDrawSurface_t *ds, bspDrawSurface_t& out ){ /* debug color? */ if ( debugSurfaces ) { - for ( int k = 0; k < MAX_LIGHTMAPS; k++ ) - dv.color[ k ].rgb() = debugColors[ ( ds - mapDrawSurfs ) % 12 ]; + for ( auto& color : dv.color ) + color.rgb() = debugColors[ ( ds - mapDrawSurfs ) % 12 ]; } } } @@ -2219,7 +2196,6 @@ void EmitFlareSurface( mapDrawSurface_t *ds ){ */ void EmitPatchSurface( entity_t *e, mapDrawSurface_t *ds ){ - int i, j; int surfaceFlags, contentFlags; /* vortex: _patchMeta support */ @@ -2228,13 +2204,13 @@ void EmitPatchSurface( entity_t *e, mapDrawSurface_t *ds ){ /* invert the surface if necessary */ if ( ds->backSide || ds->shaderInfo->invert ) { /* walk the verts, flip the normal */ - for ( i = 0; i < ds->numVerts; i++ ) - vector3_negate( ds->verts[ i ].normal ); + for ( bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) + vector3_negate( vert.normal ); /* walk the verts again, but this time reverse their order */ - for ( j = 0; j < ds->patchHeight; j++ ) + for ( int j = 0; j < ds->patchHeight; j++ ) { - for ( i = 0; i < ( ds->patchWidth / 2 ); i++ ) + for ( int i = 0; i < ( ds->patchWidth / 2 ); i++ ) { std::swap( ds->verts[ j * ds->patchWidth + i ], ds->verts[ j * ds->patchWidth + ( ds->patchWidth - i - 1 ) ] ); @@ -2277,7 +2253,7 @@ void EmitPatchSurface( entity_t *e, mapDrawSurface_t *ds ){ out.fogNum = ds->fogNum; /* RBSP */ - for ( i = 0; i < MAX_LIGHTMAPS; i++ ) + for ( int i = 0; i < MAX_LIGHTMAPS; i++ ) { out.lightmapNum[ i ] = -3; out.lightmapStyles[ i ] = LS_NONE; @@ -2443,8 +2419,8 @@ void EmitTriangleSurface( mapDrawSurface_t *ds ){ } /* walk the verts, flip the normal */ - for ( i = 0; i < ds->numVerts; i++ ) - vector3_negate( ds->verts[ i ].normal ); + for ( bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) + vector3_negate( vert.normal ); /* invert facing */ vector3_negate( ds->lightmapVecs[ 2 ] ); @@ -2564,7 +2540,7 @@ static void EmitFaceSurface( mapDrawSurface_t *ds ){ */ static void MakeDebugPortalSurfs_r( const node_t *node, shaderInfo_t *si ){ - int i, k, c, s; + int i, c, s; const portal_t *p; mapDrawSurface_t *ds; bspDrawVert_t *dv; @@ -2621,10 +2597,10 @@ static void MakeDebugPortalSurfs_r( const node_t *node, shaderInfo_t *si ){ dv->xyz = w[ i ]; dv->normal = p->plane.normal(); dv->st = { 0, 0 }; - for ( k = 0; k < MAX_LIGHTMAPS; k++ ) + for ( auto& color : dv->color ) { - dv->color[ k ].rgb() = debugColors[ c % 12 ]; - dv->color[ k ].alpha() = 32; + color.rgb() = debugColors[ c % 12 ]; + color.alpha() = 32; } } } @@ -2729,9 +2705,9 @@ void BiasSurfaceTextures( mapDrawSurface_t *ds ){ const Vector2 bias = CalcSurfaceTextureBias( ds ); /* bias the texture coordinates */ - for ( int i = 0; i < ds->numVerts; i++ ) + for ( bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) { - ds->verts[ i ].st -= bias; + vert.st -= bias; } } @@ -2752,13 +2728,10 @@ int AddSurfaceModelsToTriangle_r( mapDrawSurface_t *ds, const surfaceModel_t& mo /* subdivide calc */ { - int i; - - /* find the longest edge and split it */ max = -1; float maxDist = 0.0f; - for ( i = 0; i < 3; i++ ) + for ( int i = 0; i < 3; ++i ) { /* get dist */ const float dist = vector3_length_squared( tri[ i ]->xyz - tri[ ( i + 1 ) % 3 ]->xyz ); @@ -2905,12 +2878,12 @@ int AddSurfaceModels( mapDrawSurface_t *ds ){ alpha = 0.0f; /* walk verts */ - for ( i = 0; i < ds->numVerts; i++ ) + for ( const bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) { - centroid.xyz += ds->verts[ i ].xyz; - centroid.normal += ds->verts[ i ].normal; - centroid.st += ds->verts[ i ].st; - alpha += ds->verts[ i ].color[ 0 ].alpha(); + centroid.xyz += vert.xyz; + centroid.normal += vert.normal; + centroid.st += vert.st; + alpha += vert.color[ 0 ].alpha(); } /* average */ @@ -3065,21 +3038,21 @@ static void VolumeColorMods( entity_t *e, mapDrawSurface_t *ds ){ } /* iterate verts */ - for ( int i = 0; i < ds->numVerts; i++ ) + for ( bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) { /* iterate planes */ size_t j; - for ( j = 0; j < b->sides.size(); j++ ) + for ( j = 0; j < b->sides.size(); ++j ) { /* point-plane test */ - if ( plane3_distance_to_point( mapplanes[ b->sides[ j ].planenum ].plane, ds->verts[ i ].xyz ) > 1.0f ) { + if ( plane3_distance_to_point( mapplanes[ b->sides[ j ].planenum ].plane, vert.xyz ) > 1.0f ) { break; } } /* apply colormods */ if ( j == b->sides.size() ) { - ColorMod( b->contentShader->colorMod, 1, &ds->verts[ i ] ); + ColorMod( b->contentShader->colorMod, 1, &vert ); } } } @@ -3095,9 +3068,6 @@ static void VolumeColorMods( entity_t *e, mapDrawSurface_t *ds ){ */ void FilterDrawsurfsIntoTree( entity_t *e, tree_t& tree ){ - int i, j; - mapDrawSurface_t *ds; - shaderInfo_t *si; int refs; int numSurfs, numRefs, numSkyboxSurfaces; bool sb; @@ -3110,16 +3080,16 @@ void FilterDrawsurfsIntoTree( entity_t *e, tree_t& tree ){ numSurfs = 0; numRefs = 0; numSkyboxSurfaces = 0; - for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ ) + for ( int i = e->firstDrawSurf; i < numMapDrawSurfs; ++i ) { /* get surface and try to early out */ - ds = &mapDrawSurfs[ i ]; + mapDrawSurface_t *ds = &mapDrawSurfs[ i ]; if ( ds->numVerts == 0 && ds->type != ESurfaceType::Flare && ds->type != ESurfaceType::Shader ) { continue; } /* get shader */ - si = ds->shaderInfo; + shaderInfo_t *si = ds->shaderInfo; /* ydnar: skybox surfaces are special */ if ( ds->skybox ) { @@ -3135,8 +3105,8 @@ void FilterDrawsurfsIntoTree( entity_t *e, tree_t& tree ){ refs = 0; /* apply texture coordinate mods */ - for ( j = 0; j < ds->numVerts; j++ ) - TCMod( si->mod, ds->verts[ j ].st ); + for ( bspDrawVert_t& vert : Span( ds->verts, ds->numVerts ) ) + TCMod( si->mod, vert.st ); /* ydnar: apply shader colormod */ ColorMod( ds->shaderInfo->colorMod, ds->numVerts, ds->verts ); diff --git a/tools/quake3/q3map2/surface_fur.cpp b/tools/quake3/q3map2/surface_fur.cpp index 1ac1f23a..fa06cbbe 100644 --- a/tools/quake3/q3map2/surface_fur.cpp +++ b/tools/quake3/q3map2/surface_fur.cpp @@ -46,44 +46,35 @@ */ void Fur( mapDrawSurface_t *ds ){ - int i, j, k, numLayers; - float offset, fade, a; - mapDrawSurface_t *fur; - bspDrawVert_t *dv; - - /* dummy check */ if ( ds == NULL || ds->fur || ds->shaderInfo->furNumLayers < 1 ) { return; } /* get basic info */ - numLayers = ds->shaderInfo->furNumLayers; - offset = ds->shaderInfo->furOffset; - fade = ds->shaderInfo->furFade * 255.0f; + const int numLayers = ds->shaderInfo->furNumLayers; + const float offset = ds->shaderInfo->furOffset; + const float fade = ds->shaderInfo->furFade * 255.0f; /* debug code */ //% Sys_FPrintf( SYS_VRB, "Fur(): layers: %d offset: %f fade: %f %s\n", //% numLayers, offset, fade, ds->shaderInfo->shader ); /* initial offset */ - for ( j = 0; j < ds->numVerts; j++ ) + for ( bspDrawVert_t& dv : Span( ds->verts, ds->numVerts ) ) { - /* get surface vert */ - dv = &ds->verts[ j ]; - /* offset is scaled by original vertex alpha */ - a = (float) dv->color[ 0 ].alpha() / 255.0; + const float a = dv.color[ 0 ].alpha() / 255.0; /* offset it */ - dv->xyz += dv->normal * ( offset * a ); + dv.xyz += dv.normal * ( offset * a ); } /* wash, rinse, repeat */ - for ( i = 1; i < numLayers; i++ ) + for ( int i = 1; i < numLayers; ++i ) { /* clone the surface */ - fur = CloneSurface( ds, ds->shaderInfo ); + mapDrawSurface_t *fur = CloneSurface( ds, ds->shaderInfo ); if ( fur == NULL ) { return; } @@ -92,25 +83,21 @@ void Fur( mapDrawSurface_t *ds ){ fur->fur = true; /* walk the verts */ - for ( j = 0; j < fur->numVerts; j++ ) + for ( int j = 0; j < fur->numVerts; ++j ) { - /* get surface vert */ - dv = &ds->verts[ j ]; - /* offset is scaled by original vertex alpha */ - a = (float) dv->color[ 0 ].alpha() / 255.0; + const float a = ds->verts[ j ].color[ 0 ].alpha() / 255.0; /* get fur vert */ - dv = &fur->verts[ j ]; + bspDrawVert_t& dv = fur->verts[ j ]; /* offset it */ - dv->xyz += dv->normal * ( offset * a * i ); + dv.xyz += dv.normal * ( offset * a * i ); /* fade alpha */ - for ( k = 0; k < MAX_LIGHTMAPS; k++ ) + for ( auto& color : dv.color ) { - a = (float) dv->color[ k ].alpha() - fade; - dv->color[ k ].alpha() = color_to_byte( a ); + color.alpha() = color_to_byte( color.alpha() - fade ); } } } diff --git a/tools/quake3/q3map2/surface_meta.cpp b/tools/quake3/q3map2/surface_meta.cpp index 0ff008ca..1681ac80 100644 --- a/tools/quake3/q3map2/surface_meta.cpp +++ b/tools/quake3/q3map2/surface_meta.cpp @@ -648,8 +648,8 @@ void MaxAreaFaceSurface( mapDrawSurface_t *ds ){ void FanFaceSurface( mapDrawSurface_t *ds ){ int i, k, a, b, c; Color4f color[ MAX_LIGHTMAPS ]; - for ( k = 0; k < MAX_LIGHTMAPS; k++ ) - color[k].set( 0 ); + for ( auto& co : color ) + co.set( 0 ); bspDrawVert_t *verts, *centroid, *dv; double iv; @@ -855,29 +855,24 @@ void EmitMetaStats(){ */ void MakeEntityMetaTriangles( entity_t *e ){ - int i, f, fOld, start; - mapDrawSurface_t *ds; - - /* note it */ Sys_FPrintf( SYS_VRB, "--- MakeEntityMetaTriangles ---\n" ); /* init pacifier */ - fOld = -1; - start = I_FloatTime(); + int fOld = -1; + const int start = I_FloatTime(); /* walk the list of surfaces in the entity */ - for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ ) + for ( int i = e->firstDrawSurf; i < numMapDrawSurfs; ++i ) { /* print pacifier */ - f = 10 * ( i - e->firstDrawSurf ) / ( numMapDrawSurfs - e->firstDrawSurf ); - if ( f != fOld ) { + if ( const int f = 10 * ( i - e->firstDrawSurf ) / ( numMapDrawSurfs - e->firstDrawSurf ); f != fOld ) { fOld = f; Sys_FPrintf( SYS_VRB, "%d...", f ); } /* get surface */ - ds = &mapDrawSurfs[ i ]; + mapDrawSurface_t *ds = &mapDrawSurfs[ i ]; if ( ds->numVerts <= 0 ) { continue; } diff --git a/tools/quake3/q3map2/tjunction.cpp b/tools/quake3/q3map2/tjunction.cpp index 0277bd97..982bff14 100644 --- a/tools/quake3/q3map2/tjunction.cpp +++ b/tools/quake3/q3map2/tjunction.cpp @@ -201,12 +201,12 @@ int AddEdge( bspDrawVert_t& dv1, bspDrawVert_t& dv2, bool createNonAxial ) { adds a surface's edges */ -void AddSurfaceEdges( mapDrawSurface_t *ds ){ - for ( int i = 0; i < ds->numVerts; i++ ) +void AddSurfaceEdges( mapDrawSurface_t& ds ){ + for ( int i = 0; i < ds.numVerts; i++ ) { /* save the edge number in the lightmap field so we don't need to look it up again */ - ds->verts[i].lightmap[ 0 ][ 0 ] = - AddEdge( ds->verts[ i ], ds->verts[ ( i + 1 ) % ds->numVerts ], false ); + ds.verts[i].lightmap[ 0 ][ 0 ] = + AddEdge( ds.verts[ i ], ds.verts[ ( i + 1 ) % ds.numVerts ], false ); } } @@ -249,14 +249,12 @@ bool ColinearEdge( const Vector3& v1, const Vector3& v2, const Vector3& v3 ){ brush tjunctions ==================== */ -void AddPatchEdges( mapDrawSurface_t *ds ) { - int i; - - for ( i = 0 ; i < ds->patchWidth - 2; i += 2 ) { +void AddPatchEdges( mapDrawSurface_t& ds ) { + for ( int i = 0; i < ds.patchWidth - 2; i += 2 ) { { - bspDrawVert_t& v1 = ds->verts[ i ]; - bspDrawVert_t& v2 = ds->verts[ i + 1 ]; - bspDrawVert_t& v3 = ds->verts[ i + 2 ]; + bspDrawVert_t& v1 = ds.verts[ i + 0 ]; + bspDrawVert_t& v2 = ds.verts[ i + 1 ]; + bspDrawVert_t& v3 = ds.verts[ i + 2 ]; // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3 if ( ColinearEdge( v1.xyz, v2.xyz, v3.xyz ) ) { @@ -264,9 +262,9 @@ void AddPatchEdges( mapDrawSurface_t *ds ) { } } { - bspDrawVert_t& v1 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i ]; - bspDrawVert_t& v2 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i + 1 ]; - bspDrawVert_t& v3 = ds->verts[ ( ds->patchHeight - 1 ) * ds->patchWidth + i + 2 ]; + bspDrawVert_t& v1 = ds.verts[ ( ds.patchHeight - 1 ) * ds.patchWidth + i + 0 ]; + bspDrawVert_t& v2 = ds.verts[ ( ds.patchHeight - 1 ) * ds.patchWidth + i + 1 ]; + bspDrawVert_t& v3 = ds.verts[ ( ds.patchHeight - 1 ) * ds.patchWidth + i + 2 ]; // if v2 is on the v1 to v3 line, add an edge from v1 to v3 if ( ColinearEdge( v1.xyz, v2.xyz, v3.xyz ) ) { @@ -275,11 +273,11 @@ void AddPatchEdges( mapDrawSurface_t *ds ) { } } - for ( i = 0 ; i < ds->patchHeight - 2 ; i += 2 ) { + for ( int i = 0; i < ds.patchHeight - 2; i += 2 ) { { - bspDrawVert_t& v1 = ds->verts[ i * ds->patchWidth ]; - bspDrawVert_t& v2 = ds->verts[ ( i + 1 ) * ds->patchWidth ]; - bspDrawVert_t& v3 = ds->verts[ ( i + 2 ) * ds->patchWidth ]; + bspDrawVert_t& v1 = ds.verts[ ( i + 0 ) * ds.patchWidth ]; + bspDrawVert_t& v2 = ds.verts[ ( i + 1 ) * ds.patchWidth ]; + bspDrawVert_t& v3 = ds.verts[ ( i + 2 ) * ds.patchWidth ]; // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3 if ( ColinearEdge( v1.xyz, v2.xyz, v3.xyz ) ) { @@ -287,9 +285,9 @@ void AddPatchEdges( mapDrawSurface_t *ds ) { } } { - bspDrawVert_t& v1 = ds->verts[ ( ds->patchWidth - 1 ) + i * ds->patchWidth ]; - bspDrawVert_t& v2 = ds->verts[ ( ds->patchWidth - 1 ) + ( i + 1 ) * ds->patchWidth ]; - bspDrawVert_t& v3 = ds->verts[ ( ds->patchWidth - 1 ) + ( i + 2 ) * ds->patchWidth ]; + bspDrawVert_t& v1 = ds.verts[ ( ds.patchWidth - 1 ) + ( i + 0 ) * ds.patchWidth ]; + bspDrawVert_t& v2 = ds.verts[ ( ds.patchWidth - 1 ) + ( i + 1 ) * ds.patchWidth ]; + bspDrawVert_t& v3 = ds.verts[ ( ds.patchWidth - 1 ) + ( i + 2 ) * ds.patchWidth ]; // if v2 is the midpoint of v1 to v3, add an edge from v1 to v3 if ( ColinearEdge( v1.xyz, v2.xyz, v3.xyz ) ) { @@ -297,8 +295,6 @@ void AddPatchEdges( mapDrawSurface_t *ds ) { } } } - - } @@ -308,7 +304,7 @@ void AddPatchEdges( mapDrawSurface_t *ds ) { ==================== */ #define MAX_SURFACE_VERTS 256 -void FixSurfaceJunctions( mapDrawSurface_t *ds ) { +void FixSurfaceJunctions( mapDrawSurface_t& ds ) { int i, j, k; edgeLine_t *e; edgePoint_t *p; @@ -320,7 +316,7 @@ void FixSurfaceJunctions( mapDrawSurface_t *ds ) { numVerts = 0; - for ( i = 0 ; i < ds->numVerts ; i++ ) + for ( i = 0; i < ds.numVerts; ++i ) { counts[i] = 0; @@ -328,15 +324,15 @@ void FixSurfaceJunctions( mapDrawSurface_t *ds ) { if ( numVerts == MAX_SURFACE_VERTS ) { Error( "MAX_SURFACE_VERTS" ); } - verts[numVerts] = ds->verts[i]; + verts[numVerts] = ds.verts[i]; originals[numVerts] = i; numVerts++; // check to see if there are any t junctions before the next vert - v1 = &ds->verts[i]; - v2 = &ds->verts[ ( i + 1 ) % ds->numVerts ]; + v1 = &ds.verts[i]; + v2 = &ds.verts[ ( i + 1 ) % ds.numVerts ]; - j = (int)ds->verts[i].lightmap[ 0 ][ 0 ]; + j = (int)ds.verts[i].lightmap[ 0 ][ 0 ]; if ( j == -1 ) { continue; // degenerate edge } @@ -408,7 +404,7 @@ void FixSurfaceJunctions( mapDrawSurface_t *ds ) { } } - c_addedVerts += numVerts - ds->numVerts; + c_addedVerts += numVerts - ds.numVerts; c_totalVerts += numVerts; @@ -433,9 +429,9 @@ void FixSurfaceJunctions( mapDrawSurface_t *ds ) { // fine the way it is c_natural++; - ds->numVerts = numVerts; - ds->verts = safe_malloc( numVerts * sizeof( *ds->verts ) ); - memcpy( ds->verts, verts, numVerts * sizeof( *ds->verts ) ); + ds.numVerts = numVerts; + ds.verts = safe_malloc( numVerts * sizeof( *ds.verts ) ); + memcpy( ds.verts, verts, numVerts * sizeof( *ds.verts ) ); return; } @@ -464,11 +460,11 @@ void FixSurfaceJunctions( mapDrawSurface_t *ds ) { } - ds->numVerts = numVerts; - ds->verts = safe_malloc( numVerts * sizeof( *ds->verts ) ); + ds.numVerts = numVerts; + ds.verts = safe_malloc( numVerts * sizeof( *ds.verts ) ); - for ( j = 0 ; j < ds->numVerts ; j++ ) { - ds->verts[j] = verts[ ( j + i ) % ds->numVerts ]; + for ( j = 0 ; j < ds.numVerts ; j++ ) { + ds.verts[j] = verts[ ( j + i ) % ds.numVerts ]; } } @@ -486,69 +482,58 @@ void FixSurfaceJunctions( mapDrawSurface_t *ds ) { int c_broken = 0; -bool FixBrokenSurface( mapDrawSurface_t *ds ){ - bspDrawVert_t *dv1, *dv2, avg; - int i, j, k; - - +bool FixBrokenSurface( mapDrawSurface_t& ds ){ /* dummy check */ - if ( ds == NULL ) { - return false; - } - if ( ds->type != ESurfaceType::Face ) { + if ( ds.type != ESurfaceType::Face ) { return false; } /* check all verts */ - for ( i = 0; i < ds->numVerts; i++ ) + for ( int i = 0; i < ds.numVerts; ++i ) { /* get verts */ - dv1 = &ds->verts[ i ]; - dv2 = &ds->verts[ ( i + 1 ) % ds->numVerts ]; + bspDrawVert_t& dv1 = ds.verts[ i ]; + bspDrawVert_t& dv2 = ds.verts[ ( i + 1 ) % ds.numVerts ]; + bspDrawVert_t avg; /* degenerate edge? */ - avg.xyz = dv1->xyz - dv2->xyz; + avg.xyz = dv1.xyz - dv2.xyz; if ( vector3_length( avg.xyz ) < DEGENERATE_EPSILON ) { Sys_FPrintf( SYS_WRN | SYS_VRBflag, "WARNING: Degenerate T-junction edge found, fixing...\n" ); /* create an average drawvert */ /* ydnar 2002-01-26: added nearest-integer welding preference */ - avg.xyz = SnapWeldVector( dv1->xyz, dv2->xyz ); - avg.normal = VectorNormalized( dv1->normal + dv2->normal ); - avg.st = vector2_mid( dv1->st, dv2->st ); + avg.xyz = SnapWeldVector( dv1.xyz, dv2.xyz ); + avg.normal = VectorNormalized( dv1.normal + dv2.normal ); + avg.st = vector2_mid( dv1.st, dv2.st ); /* lightmap st/colors */ - for ( k = 0; k < MAX_LIGHTMAPS; k++ ) + for ( int k = 0; k < MAX_LIGHTMAPS; ++k ) { - avg.lightmap[ k ] = vector2_mid( dv1->lightmap[ k ], dv2->lightmap[ k ] ); - for ( j = 0; j < 4; j++ ) - avg.color[ k ][ j ] = (int) ( dv1->color[ k ][ j ] + dv2->color[ k ][ j ] ) >> 1; + avg.lightmap[ k ] = vector2_mid( dv1.lightmap[ k ], dv2.lightmap[ k ] ); + for ( int j = 0; j < 4; ++j ) + avg.color[ k ][ j ] = (int) ( dv1.color[ k ][ j ] + dv2.color[ k ][ j ] ) >> 1; } /* ydnar: der... */ - memcpy( dv1, &avg, sizeof( avg ) ); + dv1 = avg; /* move the remaining verts */ - for ( k = i + 2; k < ds->numVerts; k++ ) + for ( int k = i + 2; k < ds.numVerts; ++k ) { - /* get verts */ - dv1 = &ds->verts[ k ]; - dv2 = &ds->verts[ k - 1 ]; - - /* copy */ - memcpy( dv2, dv1, sizeof( bspDrawVert_t ) ); + ds.verts[ k - 1 ] = ds.verts[ k ]; } - ds->numVerts--; + ds.numVerts--; /* after welding, we have to consider the same vertex again, as it now has a new neighbor dv2 */ --i; - /* should ds->numVerts have become 0, then i is now -1. In the next iteration, the loop will abort. */ + /* should ds.numVerts have become 0, then i is now -1. In the next iteration, the loop will abort. */ } } /* one last check and return */ - return ds->numVerts >= 3; + return ds.numVerts >= 3; } @@ -562,12 +547,7 @@ bool FixBrokenSurface( mapDrawSurface_t *ds ){ */ void FixTJunctions( entity_t *ent ){ - int i; - mapDrawSurface_t *ds; - shaderInfo_t *si; int axialEdgeLines; - originalEdge_t *e; - bspDrawVert_t *dv; /* meta mode has its own t-junction code (currently not as good as this code) */ //% if( meta ) @@ -582,17 +562,17 @@ void FixTJunctions( entity_t *ent ){ // this actually creates axial edges, but it // only creates originalEdge_t structures // for non-axial edges - for ( i = ent->firstDrawSurf ; i < numMapDrawSurfs ; i++ ) + for ( int i = ent->firstDrawSurf; i < numMapDrawSurfs; ++i ) { /* get surface and early out if possible */ - ds = &mapDrawSurfs[ i ]; - si = ds->shaderInfo; - if ( ( si->compileFlags & C_NODRAW ) || si->autosprite || si->notjunc || ds->numVerts == 0 ) { + mapDrawSurface_t& ds = mapDrawSurfs[ i ]; + const shaderInfo_t *si = ds.shaderInfo; + if ( ( si->compileFlags & C_NODRAW ) || si->autosprite || si->notjunc || ds.numVerts == 0 ) { continue; } /* ydnar: gs mods: handle the various types of surfaces */ - switch ( ds->type ) + switch ( ds.type ) { /* handle brush faces */ case ESurfaceType::Face: @@ -619,10 +599,8 @@ void FixTJunctions( entity_t *ent ){ // add the non-axial edges, longest first // this gives the most accurate edge description - for ( i = 0 ; i < numOriginalEdges ; i++ ) { - e = &originalEdges[i]; - dv = e->dv[0]; // e might change during AddEdge - dv->lightmap[ 0 ][ 0 ] = AddEdge( *e->dv[ 0 ], *e->dv[ 1 ], true ); + for ( originalEdge_t& e : Span( originalEdges, numOriginalEdges ) ) { // originalEdges might not change during AddEdge( true ) + e.dv[ 0 ]->lightmap[ 0 ][ 0 ] = AddEdge( *e.dv[ 0 ], *e.dv[ 1 ], true ); } Sys_FPrintf( SYS_VRB, "%9d axial edge lines\n", axialEdgeLines ); @@ -630,24 +608,24 @@ void FixTJunctions( entity_t *ent ){ Sys_FPrintf( SYS_VRB, "%9d degenerate edges\n", c_degenerateEdges ); // insert any needed vertexes - for ( i = ent->firstDrawSurf; i < numMapDrawSurfs ; i++ ) + for ( int i = ent->firstDrawSurf; i < numMapDrawSurfs; ++i ) { /* get surface and early out if possible */ - ds = &mapDrawSurfs[ i ]; - si = ds->shaderInfo; - if ( ( si->compileFlags & C_NODRAW ) || si->autosprite || si->notjunc || ds->numVerts == 0 || ds->type != ESurfaceType::Face ) { + mapDrawSurface_t& ds = mapDrawSurfs[ i ]; + const shaderInfo_t *si = ds.shaderInfo; + if ( ( si->compileFlags & C_NODRAW ) || si->autosprite || si->notjunc || ds.numVerts == 0 || ds.type != ESurfaceType::Face ) { continue; } /* ydnar: gs mods: handle the various types of surfaces */ - switch ( ds->type ) + switch ( ds.type ) { /* handle brush faces */ case ESurfaceType::Face: FixSurfaceJunctions( ds ); if ( !FixBrokenSurface( ds ) ) { c_broken++; - ClearSurface( ds ); + ClearSurface( &ds ); } break; diff --git a/tools/quake3/q3map2/vis.cpp b/tools/quake3/q3map2/vis.cpp index 08fd76bb..d073bcc0 100644 --- a/tools/quake3/q3map2/vis.cpp +++ b/tools/quake3/q3map2/vis.cpp @@ -58,15 +58,10 @@ fixedWinding_t *NewFixedWinding( int numpoints ){ void prl( leaf_t *l ){ - int i; - vportal_t *p; - visPlane_t pl; - - for ( i = 0 ; i < l->numportals ; i++ ) + for ( const vportal_t *p : Span( l->portals, l->numportals ) ) { - p = l->portals[i]; - pl = p->plane; - Sys_Printf( "portal %4i to leaf %4i : %7.1f : (%4.1f, %4.1f, %4.1f)\n",(int)( p - portals ),p->leaf,pl.dist(), pl.normal()[0], pl.normal()[1], pl.normal()[2] ); + const visPlane_t pl = p->plane; + Sys_Printf( "portal %4i to leaf %4i : %7.1f : (%4.1f, %4.1f, %4.1f)\n",(int)( p - portals ), p->leaf, pl.dist(), pl.normal()[0], pl.normal()[1], pl.normal()[2] ); } } @@ -82,7 +77,7 @@ void prl( leaf_t *l ){ ============= */ void SortPortals( void ){ - for ( int i = 0 ; i < numportals * 2 ; i++ ) + for ( int i = 0; i < numportals * 2; ++i ) sorted_portals[i] = &portals[i]; if ( !nosort ) { @@ -99,33 +94,25 @@ void SortPortals( void ){ ============== */ int LeafVectorFromPortalVector( byte *portalbits, byte *leafbits ){ - int i, j, leafnum; - vportal_t *p; - int c_leafs; - - - for ( i = 0 ; i < numportals * 2 ; i++ ) + for ( int i = 0; i < numportals * 2; ++i ) { if ( portalbits[i >> 3] & ( 1 << ( i & 7 ) ) ) { - p = portals + i; - leafbits[p->leaf >> 3] |= ( 1 << ( p->leaf & 7 ) ); + const vportal_t& p = portals[i]; + leafbits[p.leaf >> 3] |= ( 1 << ( p.leaf & 7 ) ); } } - for ( j = 0; j < portalclusters; j++ ) + for ( int i = 0; i < portalclusters; ++i ) { - leafnum = j; + int leafnum = i; while ( leafs[leafnum].merged >= 0 ) leafnum = leafs[leafnum].merged; //if the merged leaf is visible then the original leaf is visible if ( leafbits[leafnum >> 3] & ( 1 << ( leafnum & 7 ) ) ) { - leafbits[j >> 3] |= ( 1 << ( j & 7 ) ); + leafbits[i >> 3] |= ( 1 << ( i & 7 ) ); } } - - c_leafs = CountBits( leafbits, portalclusters ); - - return c_leafs; + return CountBits( leafbits, portalclusters ); //c_leafs } @@ -138,13 +125,9 @@ int LeafVectorFromPortalVector( byte *portalbits, byte *leafbits ){ */ static int clustersizehistogram[MAX_MAP_LEAFS] = {0}; void ClusterMerge( int leafnum ){ - leaf_t *leaf; byte portalvector[MAX_PORTALS / 8]; byte uncompressed[MAX_MAP_LEAFS / 8]; - int i, j; int numvis, mergedleafnum; - vportal_t *p; - int pnum; // OR together all the portalvis bits @@ -153,10 +136,9 @@ void ClusterMerge( int leafnum ){ mergedleafnum = leafs[mergedleafnum].merged; memset( portalvector, 0, portalbytes ); - leaf = &leafs[mergedleafnum]; - for ( i = 0; i < leaf->numportals; i++ ) + + for ( const vportal_t *p : Span( leafs[mergedleafnum].portals, leafs[mergedleafnum].numportals ) ) { - p = leaf->portals[i]; if ( p->removed ) { continue; } @@ -164,9 +146,9 @@ void ClusterMerge( int leafnum ){ if ( p->status != EVStatus::Done ) { Error( "portal not done" ); } - for ( j = 0 ; j < portallongs ; j++ ) + for ( int j = 0; j < portallongs; ++j ) ( (long *)portalvector )[j] |= ( (long *)p->portalvis )[j]; - pnum = p - portals; + const int pnum = p - portals; portalvector[pnum >> 3] |= 1 << ( pnum & 7 ); } @@ -259,13 +241,11 @@ void CalcPassagePortalVis( void ){ ================== */ void CalcFastVis( void ){ - int i; - // fastvis just uses mightsee for a very loose bound - for ( i = 0 ; i < numportals * 2 ; i++ ) + for ( vportal_t& p : Span( portals, numportals * 2 ) ) { - portals[i].portalvis = portals[i].portalflood; - portals[i].status = EVStatus::Done; + p.portalvis = p.portalflood; + p.status = EVStatus::Done; } } @@ -358,25 +338,23 @@ void CalcVis( void ){ SetPortalSphere ================== */ -void SetPortalSphere( vportal_t *p ){ - Vector3 total( 0 ); - fixedWinding_t *w; +void SetPortalSphere( vportal_t& p ){ + Vector3 origin( 0 ); - w = p->winding; - for ( int i = 0; i < w->numpoints; i++ ) + for ( const Vector3& point : Span( p.winding->points, p.winding->numpoints ) ) { - total += w->points[i]; + origin += point; } - total /= w->numpoints; + origin /= p.winding->numpoints; double bestr = 0; - for ( int i = 0; i < w->numpoints; i++ ) + for ( const Vector3& point : Span( p.winding->points, p.winding->numpoints ) ) { - value_maximize( bestr, vector3_length( w->points[i] - total ) ); + value_maximize( bestr, vector3_length( point - origin ) ); } - p->origin = total; - p->radius = bestr; + p.origin = origin; + p.radius = bestr; } /* @@ -388,23 +366,21 @@ void SetPortalSphere( vportal_t *p ){ static bool Winding_PlanesConcave( const fixedWinding_t *w1, const fixedWinding_t *w2, const Plane3f& plane1, const Plane3f& plane2 ){ - int i; - if ( !w1 || !w2 ) { return false; } // check if one of the points of winding 1 is at the front of the plane of winding 2 - for ( i = 0; i < w1->numpoints; i++ ) + for ( const Vector3& point : Span( w1->points, w1->numpoints ) ) { - if ( plane3_distance_to_point( plane2, w1->points[i] ) > WCONVEX_EPSILON ) { + if ( plane3_distance_to_point( plane2, point ) > WCONVEX_EPSILON ) { return true; } } // check if one of the points of winding 2 is at the front of the plane of winding 1 - for ( i = 0; i < w2->numpoints; i++ ) + for ( const Vector3& point : Span( w2->points, w2->numpoints ) ) { - if ( plane3_distance_to_point( plane1, w2->points[i] ) > WCONVEX_EPSILON ) { + if ( plane3_distance_to_point( plane1, point ) > WCONVEX_EPSILON ) { return true; } } @@ -418,36 +394,19 @@ static bool Winding_PlanesConcave( const fixedWinding_t *w1, const fixedWinding_ ============ */ static bool TryMergeLeaves( int l1num, int l2num ){ - int i, j, k, n, numportals; - leaf_t *l1, *l2; - vportal_t *p1, *p2; vportal_t *portals[MAX_PORTALS_ON_LEAF]; - for ( k = 0; k < 2; k++ ) + for ( const leaf_t *l1 : { &faceleafs[l1num], &leafs[l1num] } ) { - if ( k ) { - l1 = &leafs[l1num]; - } - else{ - l1 = &faceleafs[l1num]; - } - for ( i = 0; i < l1->numportals; i++ ) + for ( const vportal_t *p1 : Span( l1->portals, l1->numportals ) ) { - p1 = l1->portals[i]; if ( p1->leaf == l2num ) { continue; } - for ( n = 0; n < 2; n++ ) + for ( const leaf_t *l2 : { &faceleafs[l2num], &leafs[l2num] } ) { - if ( n ) { - l2 = &leafs[l2num]; - } - else{ - l2 = &faceleafs[l2num]; - } - for ( j = 0; j < l2->numportals; j++ ) + for ( const vportal_t *p2 : Span( l2->portals, l2->numportals ) ) { - p2 = l2->portals[j]; if ( p2->leaf == l1num ) { continue; } @@ -459,43 +418,31 @@ static bool TryMergeLeaves( int l1num, int l2num ){ } } } - for ( k = 0; k < 2; k++ ) + for ( leaf_t *lfs : { faceleafs, leafs } ) { - if ( k ) { - l1 = &leafs[l1num]; - l2 = &leafs[l2num]; - } - else - { - l1 = &faceleafs[l1num]; - l2 = &faceleafs[l2num]; - } - numportals = 0; + leaf_t& l1 = lfs[l1num]; + leaf_t& l2 = lfs[l2num]; + int numportals = 0; //the leaves can be merged now - for ( i = 0; i < l1->numportals; i++ ) + for ( vportal_t *p1 : Span( l1.portals, l1.numportals ) ) { - p1 = l1->portals[i]; if ( p1->leaf == l2num ) { p1->removed = true; continue; } portals[numportals++] = p1; } - for ( j = 0; j < l2->numportals; j++ ) + for ( vportal_t *p2 : Span( l2.portals, l2.numportals ) ) { - p2 = l2->portals[j]; if ( p2->leaf == l1num ) { p2->removed = true; continue; } portals[numportals++] = p2; } - for ( i = 0; i < numportals; i++ ) - { - l2->portals[i] = portals[i]; - } - l2->numportals = numportals; - l1->merged = l2num; + std::copy_n( portals, numportals, l2.portals ); + l2.numportals = numportals; + l1.merged = l2num; } return true; } @@ -506,18 +453,10 @@ static bool TryMergeLeaves( int l1num, int l2num ){ ============ */ void UpdatePortals( void ){ - int i; - vportal_t *p; - - for ( i = 0; i < numportals * 2; i++ ) - { - p = &portals[i]; - if ( p->removed ) { - continue; - } - while ( leafs[p->leaf].merged >= 0 ) - p->leaf = leafs[p->leaf].merged; - } + for ( vportal_t& p : Span( portals, numportals * 2 ) ) + if ( !p.removed ) + while ( leafs[p.leaf].merged >= 0 ) + p.leaf = leafs[p.leaf].merged; } /* @@ -528,40 +467,31 @@ void UpdatePortals( void ){ ============ */ void MergeLeaves( void ){ - int i, j, nummerges, totalnummerges; - leaf_t *leaf; - vportal_t *p; + int nummerges, totalnummerges = 0; - totalnummerges = 0; do { nummerges = 0; - for ( i = 0; i < portalclusters; i++ ) + for ( int i = 0; i < portalclusters; ++i ) { - leaf = &leafs[i]; + const leaf_t& leaf = leafs[i]; //if this leaf is merged already /* ydnar: vmods: merge all non-hint portals */ - if ( leaf->merged >= 0 && !hint ) { + if ( leaf.merged >= 0 && !hint ) { continue; } - for ( j = 0; j < leaf->numportals; j++ ) + for ( const vportal_t *p : Span( leaf.portals, leaf.numportals ) ) { - p = leaf->portals[j]; - // - if ( p->removed ) { - continue; - } //never merge through hint portals - if ( p->hint ) { - continue; - } - if ( TryMergeLeaves( i, p->leaf ) ) { - UpdatePortals(); - nummerges++; - break; + if ( !p->removed && !p->hint ) { + if ( TryMergeLeaves( i, p->leaf ) ) { + UpdatePortals(); + nummerges++; + break; + } } } } @@ -608,15 +538,15 @@ fixedWinding_t *TryMergeWinding( fixedWinding_t *f1, fixedWinding_t *f2, const V if ( fabs( (*p2)[k] - (*p3)[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME break; } - } //end for + } if ( k == 3 ) { break; } - } //end for + } if ( j < f2->numpoints ) { break; } - } //end for + } if ( i == f1->numpoints ) { return NULL; // no matching edges @@ -715,7 +645,7 @@ void MergeLeafPortals( void ){ hintsmerged++; } p1->hint |= p2->hint; - SetPortalSphere( p1 ); + SetPortalSphere( *p1 ); p2->removed = true; nummerges++; i--; @@ -739,116 +669,30 @@ void MergeLeafPortals( void ){ ============ */ int CountActivePortals( void ){ - int num, hints, j; - vportal_t *p; + int num = 0, hints = 0; - num = 0; - hints = 0; - for ( j = 0; j < numportals * 2; j++ ) + for ( const vportal_t& p : Span( portals, numportals * 2 ) ) { - p = portals + j; - if ( p->removed ) { - continue; + if ( !p.removed ) { + num++; + if ( p.hint ) + hints++; } - if ( p->hint ) { - hints++; - } - num++; } Sys_Printf( "%6d active portals\n", num ); Sys_Printf( "%6d hint portals\n", hints ); return num; } -/* - ============ - WritePortals - ============ - */ -void WriteFloat( FILE *f, float v ); - -void WritePortals( char *filename ){ - int i, j, num; - vportal_t *p; - fixedWinding_t *w; - - // write the file - FILE *pf = SafeOpenWrite( filename, "wt" ); - - num = 0; - for ( j = 0; j < numportals * 2; j++ ) - { - p = portals + j; - if ( p->removed ) { - continue; - } -// if (!p->hint) -// continue; - num++; - } - - fprintf( pf, "%s\n", PORTALFILE ); - fprintf( pf, "%i\n", 0 ); - fprintf( pf, "%i\n", num ); // + numfaces); - fprintf( pf, "%i\n", 0 ); - - for ( j = 0; j < numportals * 2; j++ ) - { - p = portals + j; - if ( p->removed ) { - continue; - } -// if (!p->hint) -// continue; - w = p->winding; - fprintf( pf, "%i %i %i ",w->numpoints, 0, 0 ); - fprintf( pf, "%d ", p->hint ); - for ( i = 0 ; i < w->numpoints ; i++ ) - { - fprintf( pf, "(" ); - WriteFloat( pf, w->points[i][0] ); - WriteFloat( pf, w->points[i][1] ); - WriteFloat( pf, w->points[i][2] ); - fprintf( pf, ") " ); - } - fprintf( pf, "\n" ); - } - - /* - for (j = 0; j < numfaces; j++) - { - p = faces + j; - w = p->winding; - fprintf (pf,"%i %i %i ",w->numpoints, 0, 0); - fprintf (pf, "0 "); - for (i=0 ; inumpoints ; i++) - { - fprintf (pf,"("); - WriteFloat (pf, w->points[i][0]); - WriteFloat (pf, w->points[i][1]); - WriteFloat (pf, w->points[i][2]); - fprintf (pf,") "); - } - fprintf (pf,"\n"); - }*/ - - fclose( pf ); -} - /* ============ LoadPortals ============ */ void LoadPortals( char *name ){ - int i, j, flags; - vportal_t *p; - leaf_t *l; char magic[80]; FILE *f; - int numpoints; - fixedWinding_t *w; - int leafnums[2]; + int numpoints, leafnums[2], flags; if ( strEqual( name, "-" ) ) { f = stdin; @@ -883,8 +727,8 @@ void LoadPortals( char *name ){ portals = safe_calloc( 2 * numportals * sizeof( vportal_t ) ); leafs = safe_calloc( portalclusters * sizeof( leaf_t ) ); - for ( i = 0; i < portalclusters; i++ ) - leafs[i].merged = -1; + for ( leaf_t& leaf : Span( leafs, portalclusters ) ) + leaf.merged = -1; bspVisBytes.resize( VIS_HEADER_SIZE + portalclusters * leafbytes ); @@ -895,7 +739,7 @@ void LoadPortals( char *name ){ ( (int *)bspVisBytes.data() )[0] = portalclusters; ( (int *)bspVisBytes.data() )[1] = leafbytes; - for ( i = 0, p = portals ; i < numportals ; i++ ) + for ( int i = 0; i < numportals; ++i ) { if ( fscanf( f, "%i %i %i ", &numpoints, &leafnums[0], &leafnums[1] ) != 3 ) { Error( "LoadPortals: reading portal %i", i ); @@ -911,13 +755,13 @@ void LoadPortals( char *name ){ Error( "LoadPortals: reading flags" ); } - w = p->winding = NewFixedWinding( numpoints ); + fixedWinding_t *w = NewFixedWinding( numpoints ); w->numpoints = numpoints; - for ( j = 0 ; j < numpoints ; j++ ) + for ( Vector3& point : Span( w->points, w->numpoints ) ) { if ( fscanf( f, "(%f %f %f ) ", - &w->points[j][0], &w->points[j][1], &w->points[j][2] ) != 3 ) { + &point[0], &point[1], &point[2] ) != 3 ) { Error( "LoadPortals: reading portal %i", i ); } } @@ -929,62 +773,62 @@ void LoadPortals( char *name ){ const visPlane_t plane = PlaneFromWinding( w ); // create forward portal - l = &leafs[leafnums[0]]; - if ( l->numportals == MAX_PORTALS_ON_LEAF ) { - Error( "Leaf with too many portals" ); - } - l->portals[l->numportals] = p; - l->numportals++; + { + vportal_t& p = portals[i * 2]; + p.num = i + 1; + p.hint = ((flags & 1) != 0); + p.sky = ((flags & 2) != 0); + p.winding = w; + p.plane = plane3_flipped( plane ); + p.leaf = leafnums[1]; + SetPortalSphere( p ); - p->num = i + 1; - p->hint = ((flags & 1) != 0); - p->sky = ((flags & 2) != 0); - p->winding = w; - p->plane = plane3_flipped( plane ); - p->leaf = leafnums[1]; - SetPortalSphere( p ); - p++; + leaf_t& l = leafs[leafnums[0]]; + if ( l.numportals == MAX_PORTALS_ON_LEAF ) { + Error( "Leaf with too many portals" ); + } + l.portals[l.numportals] = &p; + l.numportals++; + } // create backwards portal - l = &leafs[leafnums[1]]; - if ( l->numportals == MAX_PORTALS_ON_LEAF ) { - Error( "Leaf with too many portals" ); - } - l->portals[l->numportals] = p; - l->numportals++; - - p->num = i + 1; - p->hint = hint; - p->winding = NewFixedWinding( w->numpoints ); - p->winding->numpoints = w->numpoints; - for ( j = 0 ; j < w->numpoints ; j++ ) { - p->winding->points[j] = w->points[w->numpoints - 1 - j]; + vportal_t& p = portals[i * 2 + 1]; + p.num = i + 1; + p.hint = hint; + p.winding = NewFixedWinding( w->numpoints ); + p.winding->numpoints = w->numpoints; + std::reverse_copy( w->points, w->points + w->numpoints, p.winding->points ); + + p.plane = plane; + p.leaf = leafnums[0]; + SetPortalSphere( p ); + + leaf_t& l = leafs[leafnums[1]]; + if ( l.numportals == MAX_PORTALS_ON_LEAF ) { + Error( "Leaf with too many portals" ); + } + l.portals[l.numportals] = &p; + l.numportals++; } - - p->plane = plane; - p->leaf = leafnums[0]; - SetPortalSphere( p ); - p++; - } faces = safe_calloc( 2 * numfaces * sizeof( vportal_t ) ); faceleafs = safe_calloc( portalclusters * sizeof( leaf_t ) ); - for ( i = 0, p = faces; i < numfaces; i++ ) + for ( int i = 0; i < numfaces; ++i ) { if ( fscanf( f, "%i %i ", &numpoints, &leafnums[0] ) != 2 ) { Error( "LoadPortals: reading portal %i", i ); } - w = p->winding = NewFixedWinding( numpoints ); + fixedWinding_t *w = NewFixedWinding( numpoints ); w->numpoints = numpoints; - for ( j = 0 ; j < numpoints ; j++ ) + for ( Vector3& point : Span( w->points, w->numpoints ) ) { if ( fscanf( f, "(%f %f %f ) ", - &w->points[j][0], &w->points[j][1], &w->points[j][2] ) != 3 ) { + &point[0], &point[1], &point[2] ) != 3 ) { Error( "LoadPortals: reading portal %i", i ); } } @@ -992,24 +836,21 @@ void LoadPortals( char *name ){ // silence gcc warning } - // calc plane - const visPlane_t plane = PlaneFromWinding( w ); + vportal_t& p = faces[i]; + p.num = i + 1; + p.winding = w; + // normal pointing out of the leaf + p.plane = plane3_flipped( PlaneFromWinding( w ) ); + p.leaf = -1; + SetPortalSphere( p ); - l = &faceleafs[leafnums[0]]; - l->merged = -1; - if ( l->numportals == MAX_PORTALS_ON_LEAF ) { + leaf_t& l = faceleafs[leafnums[0]]; + l.merged = -1; + if ( l.numportals == MAX_PORTALS_ON_LEAF ) { Error( "Leaf with too many faces" ); } - l->portals[l->numportals] = p; - l->numportals++; - - p->num = i + 1; - p->winding = w; - // normal pointing out of the leaf - p->plane = plane3_flipped( plane ); - p->leaf = -1; - SetPortalSphere( p ); - p++; + l.portals[l.numportals] = &p; + l.numportals++; } fclose( f ); @@ -1116,7 +957,6 @@ int VisMain( Args& args ){ } CountActivePortals(); - /* WritePortals( "maps/hints.prs" );*/ Sys_Printf( "visdatasize:%zu\n", bspVisBytes.size() ); diff --git a/tools/quake3/q3map2/visflow.cpp b/tools/quake3/q3map2/visflow.cpp index 6673db12..36a25c32 100644 --- a/tools/quake3/q3map2/visflow.cpp +++ b/tools/quake3/q3map2/visflow.cpp @@ -54,11 +54,8 @@ */ int CountBits( byte *bits, int numbits ){ - int i; - int c; - - c = 0; - for ( i = 0 ; i < numbits ; i++ ) + int c = 0; + for ( int i = 0; i < numbits; ++i ) if ( bits[i >> 3] & ( 1 << ( i & 7 ) ) ) { c++; } @@ -68,15 +65,13 @@ int CountBits( byte *bits, int numbits ){ void CheckStack( leaf_t *leaf, threaddata_t *thread ){ - pstack_t *p, *p2; - - for ( p = thread->pstack_head.next ; p ; p = p->next ) + for ( pstack_t *p = thread->pstack_head.next; p; p = p->next ) { // Sys_Printf ("="); if ( p->leaf == leaf ) { Error( "CheckStack: leaf recursion" ); } - for ( p2 = thread->pstack_head.next ; p2 != p ; p2 = p2->next ) + for ( pstack_t *p2 = thread->pstack_head.next; p2 != p; p2 = p2->next ) if ( p2->leaf == p->leaf ) { Error( "CheckStack: late leaf recursion" ); } @@ -86,9 +81,7 @@ void CheckStack( leaf_t *leaf, threaddata_t *thread ){ fixedWinding_t *AllocStackWinding( pstack_t *stack ){ - int i; - - for ( i = 0 ; i < 3 ; i++ ) + for ( int i = 0; i < 3; ++i ) { if ( stack->freewindings[i] ) { stack->freewindings[i] = 0; @@ -383,10 +376,9 @@ fixedWinding_t *ClipToSeperators( fixedWinding_t *source, fixedWinding_t *pass, */ void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack ){ pstack_t stack; - vportal_t *p; visPlane_t backplane; leaf_t *leaf; - int i, j, n; + int j, n; long *test, *might, *prevmight, *vis, more; int pnum; @@ -411,9 +403,8 @@ void RecursiveLeafFlow( int leafnum, threaddata_t *thread, pstack_t *prevstack ) vis = (long *)thread->base->portalvis; // check all portals for flowing into other leafs - for ( i = 0; i < leaf->numportals; i++ ) + for ( vportal_t *p : Span( leaf->portals, leaf->numportals ) ) { - p = leaf->portals[i]; if ( p->removed ) { continue; } @@ -1121,7 +1112,7 @@ fixedWinding_t *PassageChopWinding( fixedWinding_t *in, fixedWinding_t *out, con AddSeperators =============== */ -int AddSeperators( fixedWinding_t *source, fixedWinding_t *pass, bool flipclip, visPlane_t *seperators, int maxseperators ){ +int AddSeperators( const fixedWinding_t *source, const fixedWinding_t *pass, bool flipclip, visPlane_t *seperators, int maxseperators ){ int i, j, k, l; int counts[3], numseperators; bool fliptest; @@ -1247,9 +1238,8 @@ int AddSeperators( fixedWinding_t *source, fixedWinding_t *pass, bool flipclip, =============== */ void CreatePassages( int portalnum ){ - int i, j, k, n, numseperators, numsee; - vportal_t *portal, *p, *target; - leaf_t *leaf; + int j, k, n, numseperators, numsee; + vportal_t *portal, *p; passage_t *passage, *lastpassage; visPlane_t seperators[MAX_SEPERATORS * 2]; fixedWinding_t *w; @@ -1268,10 +1258,8 @@ void CreatePassages( int portalnum ){ } lastpassage = NULL; - leaf = &leafs[portal->leaf]; - for ( i = 0; i < leaf->numportals; i++ ) + for ( const vportal_t *target : Span( leafs[portal->leaf].portals, leafs[portal->leaf].numportals ) ) { - target = leaf->portals[i]; if ( target->removed ) { continue; } @@ -1365,22 +1353,15 @@ void CreatePassages( int portalnum ){ } void PassageMemory( void ){ - int i, j, totalmem, totalportals; - vportal_t *portal, *target; - leaf_t *leaf; + int totalmem = 0, totalportals = 0; - totalmem = 0; - totalportals = 0; - for ( i = 0; i < numportals; i++ ) + for ( const vportal_t *portal : Span( sorted_portals, numportals ) ) { - portal = sorted_portals[i]; if ( portal->removed ) { continue; } - leaf = &leafs[portal->leaf]; - for ( j = 0; j < leaf->numportals; j++ ) + for ( const vportal_t *target : Span( leafs[portal->leaf].portals, leafs[portal->leaf].numportals ) ) { - target = leaf->portals[j]; if ( target->removed ) { continue; } @@ -1443,20 +1424,12 @@ void PassageMemory( void ){ ================== */ void SimpleFlood( vportal_t *srcportal, int leafnum ){ - int i; - leaf_t *leaf; - vportal_t *p; - int pnum; - - leaf = &leafs[leafnum]; - - for ( i = 0 ; i < leaf->numportals ; i++ ) + for ( const vportal_t *p : Span( leafs[leafnum].portals, leafs[leafnum].numportals ) ) { - p = leaf->portals[i]; if ( p->removed ) { continue; } - pnum = p - portals; + const int pnum = p - portals; if ( !( srcportal->portalfront[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) { continue; } @@ -1587,23 +1560,16 @@ void BasePortalVis( int portalnum ){ ================== */ void RecursiveLeafBitFlow( int leafnum, byte *mightsee, byte *cansee ){ - vportal_t *p; - leaf_t *leaf; - int i, j; - long more; - int pnum; byte newmight[MAX_PORTALS / 8]; - leaf = &leafs[leafnum]; // check all portals for flowing into other leafs - for ( i = 0 ; i < leaf->numportals ; i++ ) + for ( const vportal_t *p : Span( leafs[leafnum].portals, leafs[leafnum].numportals ) ) { - p = leaf->portals[i]; if ( p->removed ) { continue; } - pnum = p - portals; + const int pnum = p - portals; // if some previous portal can't see it, skip if ( !( mightsee[pnum >> 3] & ( 1 << ( pnum & 7 ) ) ) ) { @@ -1611,12 +1577,12 @@ void RecursiveLeafBitFlow( int leafnum, byte *mightsee, byte *cansee ){ } // if this portal can see some portals we mightsee, recurse - more = 0; - for ( j = 0 ; j < portallongs ; j++ ) + long more = 0; + for ( int i = 0; i < portallongs; ++i ) { - ( (long *)newmight )[j] = ( (long *)mightsee )[j] - & ( (long *)p->portalflood )[j]; - more |= ( (long *)newmight )[j] & ~( (long *)cansee )[j]; + ( (long *)newmight )[i] = ( (long *)mightsee )[i] + & ( (long *)p->portalflood )[i]; + more |= ( (long *)newmight )[i] & ~( (long *)cansee )[i]; } if ( !more ) {