diff --git a/tools/quake3/q3map2/lightmaps_ydnar.cpp b/tools/quake3/q3map2/lightmaps_ydnar.cpp index 1bbef358..147943b3 100644 --- a/tools/quake3/q3map2/lightmaps_ydnar.cpp +++ b/tools/quake3/q3map2/lightmaps_ydnar.cpp @@ -265,33 +265,6 @@ int ImportLightmapsMain( int argc, char **argv ){ ------------------------------------------------------------------------------- */ -/* - CompareLightSurface() - compare function for qsort() - */ - -static int CompareLightSurface( const void *a, const void *b ){ - shaderInfo_t *asi, *bsi; - - - /* get shaders */ - asi = surfaceInfos[ *( (const int*) a ) ].si; - bsi = surfaceInfos[ *( (const int*) b ) ].si; - - /* dummy check */ - if ( asi == NULL ) { - return -1; - } - if ( bsi == NULL ) { - return 1; - } - - /* compare shader names */ - return strcmp( asi->shader, bsi->shader ); -} - - - /* FinishRawLightmap() allocates a raw lightmap's necessary buffers @@ -304,7 +277,22 @@ void FinishRawLightmap( rawLightmap_t *lm ){ /* sort light surfaces by shader name */ - qsort( &lightSurfaces[ lm->firstLightSurface ], lm->numLightSurfaces, sizeof( int ), CompareLightSurface ); + std::sort( &lightSurfaces[ lm->firstLightSurface ], &lightSurfaces[ lm->firstLightSurface ] + lm->numLightSurfaces, []( const int a, const int b ){ + /* get shaders */ + const shaderInfo_t *asi = surfaceInfos[ a ].si; + const shaderInfo_t *bsi = surfaceInfos[ b ].si; + + /* dummy check */ + if ( asi == NULL ) { + return true; + } + if ( bsi == NULL ) { + return false; + } + + /* compare shader names */ + return strcmp( asi->shader, bsi->shader ) < 0; + } ); /* count clusters */ lm->numLightClusters = 0; @@ -834,102 +822,101 @@ bool AddSurfaceToRawLightmap( int num, rawLightmap_t *lm ){ /* CompareSurfaceInfo() - compare function for qsort() + compare functor for std::sort() */ -static int CompareSurfaceInfo( const void *a, const void *b ){ - surfaceInfo_t *aInfo, *bInfo; - int i; +struct CompareSurfaceInfo +{ + bool operator()( const int a, const int b ) const { + /* get surface info */ + const surfaceInfo_t& aInfo = surfaceInfos[ a ]; + const surfaceInfo_t& bInfo = surfaceInfos[ b ]; - - /* get surface info */ - aInfo = &surfaceInfos[ *( (const int*) a ) ]; - bInfo = &surfaceInfos[ *( (const int*) b ) ]; - - /* model first */ - if ( aInfo->modelindex < bInfo->modelindex ) { - return 1; - } - else if ( aInfo->modelindex > bInfo->modelindex ) { - return -1; - } - - /* then lightmap status */ - if ( aInfo->hasLightmap < bInfo->hasLightmap ) { - return 1; - } - else if ( aInfo->hasLightmap > bInfo->hasLightmap ) { - return -1; - } - - /* 27: then shader! */ - if ( aInfo->si < bInfo->si ) { - return 1; - } - else if ( aInfo->si > bInfo->si ) { - return -1; - } - - - /* then lightmap sample size */ - if ( aInfo->sampleSize < bInfo->sampleSize ) { - return 1; - } - else if ( aInfo->sampleSize > bInfo->sampleSize ) { - return -1; - } - - /* then lightmap axis */ - for ( i = 0; i < 3; i++ ) - { - if ( aInfo->axis[ i ] < bInfo->axis[ i ] ) { - return 1; + /* model first */ + if ( aInfo.modelindex < bInfo.modelindex ) { + return false; } - else if ( aInfo->axis[ i ] > bInfo->axis[ i ] ) { - return -1; + else if ( aInfo.modelindex > bInfo.modelindex ) { + return true; } - } - /* then plane */ - if ( aInfo->plane == NULL && bInfo->plane != NULL ) { - return 1; - } - else if ( aInfo->plane != NULL && bInfo->plane == NULL ) { - return -1; - } - else if ( aInfo->plane != NULL && bInfo->plane != NULL ) { - for ( i = 0; i < 3; i++ ) + /* then lightmap status */ + if ( aInfo.hasLightmap < bInfo.hasLightmap ) { + return false; + } + else if ( aInfo.hasLightmap > bInfo.hasLightmap ) { + return true; + } + + /* 27: then shader! */ + if ( aInfo.si < bInfo.si ) { + return false; + } + else if ( aInfo.si > bInfo.si ) { + return true; + } + + + /* then lightmap sample size */ + if ( aInfo.sampleSize < bInfo.sampleSize ) { + return false; + } + else if ( aInfo.sampleSize > bInfo.sampleSize ) { + return true; + } + + /* then lightmap axis */ + for ( int i = 0; i < 3; i++ ) { - if ( aInfo->plane->normal()[ i ] < bInfo->plane->normal()[ i ] ) { - return 1; + if ( aInfo.axis[ i ] < bInfo.axis[ i ] ) { + return false; } - else if ( aInfo->plane->normal()[ i ] > bInfo->plane->normal()[ i ] ) { - return -1; + else if ( aInfo.axis[ i ] > bInfo.axis[ i ] ) { + return true; } } - if ( aInfo->plane->dist() < bInfo->plane->dist() ) { - return 1; + + /* then plane */ + if ( aInfo.plane == NULL && bInfo.plane != NULL ) { + return false; } - else if ( aInfo->plane->dist() > bInfo->plane->dist() ) { - return -1; + else if ( aInfo.plane != NULL && bInfo.plane == NULL ) { + return true; + } + else if ( aInfo.plane != NULL && bInfo.plane != NULL ) { + for ( int i = 0; i < 3; i++ ) + { + if ( aInfo.plane->normal()[ i ] < bInfo.plane->normal()[ i ] ) { + return false; + } + else if ( aInfo.plane->normal()[ i ] > bInfo.plane->normal()[ i ] ) { + return true; + } + } + if ( aInfo.plane->dist() < bInfo.plane->dist() ) { + return false; + } + else if ( aInfo.plane->dist() > bInfo.plane->dist() ) { + return true; + } + } + /* then position in world */ + for ( int i = 0; i < 3; i++ ) + { + if ( aInfo.minmax.mins[ i ] < bInfo.minmax.mins[ i ] ) { + return false; + } + else if ( aInfo.minmax.mins[ i ] > bInfo.minmax.mins[ i ] ) { + return true; + } + } + + /* these are functionally identical (this should almost never happen) */ + return false; } - - /* then position in world */ - for ( i = 0; i < 3; i++ ) - { - if ( aInfo->minmax.mins[ i ] < bInfo->minmax.mins[ i ] ) { - return 1; - } - else if ( aInfo->minmax.mins[ i ] > bInfo->minmax.mins[ i ] ) { - return -1; - } - } - - /* these are functionally identical (this should almost never happen) */ - return 0; -} +}; @@ -1079,7 +1066,7 @@ void SetupSurfaceLightmaps( void ){ maxMapDistance = vector3_length( g_mapMinmax.maxs - g_mapMinmax.mins ); /* sort the surfaces info list */ - qsort( sortSurfaces, numBSPDrawSurfaces, sizeof( int ), CompareSurfaceInfo ); + std::sort( sortSurfaces, sortSurfaces + numBSPDrawSurfaces, CompareSurfaceInfo() ); /* allocate a list of surfaces that would go into raw lightmaps */ numLightSurfaces = 0; @@ -2205,64 +2192,53 @@ static void FindOutLightmaps( rawLightmap_t *lm, bool fastAllocate ){ /* - CompareRawLightmap() - compare function for qsort() + CompareRawLightmap + compare functor for std::sort() */ -static int CompareRawLightmap( const void *a, const void *b ){ - rawLightmap_t *alm, *blm; - surfaceInfo_t *aInfo, *bInfo; - int i, min, diff; +struct CompareRawLightmap +{ + bool operator()( const int a, const int b ) const { + int diff; + /* get lightmaps */ + const rawLightmap_t& alm = rawLightmaps[ a ]; + const rawLightmap_t& blm = rawLightmaps[ b ]; - /* get lightmaps */ - alm = &rawLightmaps[ *( (const int*) a ) ]; - blm = &rawLightmaps[ *( (const int*) b ) ]; + /* get min number of surfaces */ + const int min = std::min( alm.numLightSurfaces, blm.numLightSurfaces ); - /* get min number of surfaces */ - min = std::min( alm->numLightSurfaces, blm->numLightSurfaces ); + /* iterate */ + for ( int i = 0; i < min; i++ ) + { + /* get surface info */ + const surfaceInfo_t& aInfo = surfaceInfos[ lightSurfaces[ alm.firstLightSurface + i ] ]; + const surfaceInfo_t& bInfo = surfaceInfos[ lightSurfaces[ blm.firstLightSurface + i ] ]; -//#define allocate_bigger_first -#ifdef allocate_bigger_first - /* compare size, allocate bigger first */ - // fastAllocate commit part: can kick fps by unique lightmap/shader combinations*=~2 + bigger compile time - //return -diff; makes packing faster and rough - diff = ( blm->w * blm->h ) - ( alm->w * alm->h ); - if ( diff != 0 ) { - return diff; - } -#endif - /* iterate */ - for ( i = 0; i < min; i++ ) - { - /* get surface info */ - aInfo = &surfaceInfos[ lightSurfaces[ alm->firstLightSurface + i ] ]; - bInfo = &surfaceInfos[ lightSurfaces[ blm->firstLightSurface + i ] ]; - - /* compare shader names */ - diff = strcmp( aInfo->si->shader, bInfo->si->shader ); - if ( diff != 0 ) { - return diff; + /* compare shader names */ + diff = strcmp( aInfo.si->shader, bInfo.si->shader ); + if ( diff != 0 ) { + return diff < 0; + } } - } - /* test style count */ - diff = 0; - for ( i = 0; i < MAX_LIGHTMAPS; i++ ) - diff += blm->styles[ i ] - alm->styles[ i ]; - if ( diff ) { - return diff; + /* test style count */ + diff = 0; + for ( int i = 0; i < MAX_LIGHTMAPS; i++ ) + diff += blm.styles[ i ] - alm.styles[ i ]; + if ( diff != 0 ) { + return diff < 0; + } + + /* compare size */ + diff = ( blm.w * blm.h ) - ( alm.w * alm.h ); + if ( diff != 0 ) { + return diff < 0; + } + /* must be equivalent */ + return false; } -#ifndef allocate_bigger_first - /* compare size */ - diff = ( blm->w * blm->h ) - ( alm->w * alm->h ); - if ( diff != 0 ) { - return diff; - } -#endif - /* must be equivalent */ - return 0; -} +}; void FillOutLightmap( outLightmap_t *olm ){ int x, y; @@ -2947,7 +2923,7 @@ void StoreSurfaceLightmaps( bool fastAllocate ){ /* fill it out and sort it */ for ( i = 0; i < numRawLightmaps; i++ ) sortLightmaps[ i ] = i; - qsort( sortLightmaps, numRawLightmaps, sizeof( int ), CompareRawLightmap ); + std::sort( sortLightmaps, sortLightmaps + numRawLightmaps, CompareRawLightmap() ); Sys_Printf( "%d.", (int) ( I_FloatTime() - timer_start ) ); diff --git a/tools/quake3/q3map2/surface_meta.cpp b/tools/quake3/q3map2/surface_meta.cpp index e6911c7c..b262c82b 100644 --- a/tools/quake3/q3map2/surface_meta.cpp +++ b/tools/quake3/q3map2/surface_meta.cpp @@ -1670,91 +1670,91 @@ static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles, /* - CompareMetaTriangles() - compare function for qsort() + CompareMetaTriangles + compare functor for std::sort() */ -static int CompareMetaTriangles( const void *a_, const void *b_ ){ - auto a = reinterpret_cast( a_ ); - auto b = reinterpret_cast( b_ ); +struct CompareMetaTriangles +{ + bool operator()( const metaTriangle_t& a, const metaTriangle_t& b ) const { + /* shader first */ + if ( a.si < b.si ) { + return false; + } + else if ( a.si > b.si ) { + return true; + } - /* shader first */ - if ( a->si < b->si ) { - return 1; - } - else if ( a->si > b->si ) { - return -1; - } + /* then fog */ + else if ( a.fogNum < b.fogNum ) { + return false; + } + else if ( a.fogNum > b.fogNum ) { + return true; + } - /* then fog */ - else if ( a->fogNum < b->fogNum ) { - return 1; - } - else if ( a->fogNum > b->fogNum ) { - return -1; - } + /* then plane */ + #if 0 + else if ( npDegrees == 0.0f && !a.si->nonplanar && + a.planeNum >= 0 && a.planeNum >= 0 ) { + if ( a.plane.dist() < b.plane.dist() ) { + return false; + } + else if ( a.plane.dist() > b.plane.dist() ) { + return true; + } + else if ( a.plane.normal()[ 0 ] < b.plane.normal()[ 0 ] ) { + return false; + } + else if ( a.plane.normal()[ 0 ] > b.plane.normal()[ 0 ] ) { + return true; + } + else if ( a.plane.normal()[ 1 ] < b.plane.normal()[ 1 ] ) { + return false; + } + else if ( a.plane.normal()[ 1 ] > b.plane.normal()[ 1 ] ) { + return true; + } + else if ( a.plane.normal()[ 2 ] < b.plane.normal()[ 2 ] ) { + return false; + } + else if ( a.plane.normal()[ 2 ] > b.plane.normal()[ 2 ] ) { + return true; + } + } + #endif - /* then plane */ - #if 0 - else if ( npDegrees == 0.0f && !a->si->nonplanar && - a->planeNum >= 0 && a->planeNum >= 0 ) { - if ( a->plane.dist() < b->plane.dist() ) { - return 1; - } - else if ( a->plane.dist() > b->plane.dist() ) { - return -1; - } - else if ( a->plane.normal()[ 0 ] < b->plane.normal()[ 0 ] ) { - return 1; - } - else if ( a->plane.normal()[ 0 ] > b->plane.normal()[ 0 ] ) { - return -1; - } - else if ( a->plane.normal()[ 1 ] < b->plane.normal()[ 1 ] ) { - return 1; - } - else if ( a->plane.normal()[ 1 ] > b->plane.normal()[ 1 ] ) { - return -1; - } - else if ( a->plane.normal()[ 2 ] < b->plane.normal()[ 2 ] ) { - return 1; - } - else if ( a->plane.normal()[ 2 ] > b->plane.normal()[ 2 ] ) { - return -1; - } - } - #endif + /* then position in world */ - /* then position in world */ - - /* find mins */ - Vector3 aMins( 999999, 999999, 999999 ); - Vector3 bMins( 999999, 999999, 999999 ); - for ( int i = 0; i < 3; i++ ) - { - const int av = a->indexes[ i ]; - const int bv = b->indexes[ i ]; - for ( int j = 0; j < 3; j++ ) + /* find mins */ + Vector3 aMins( 999999, 999999, 999999 ); + Vector3 bMins( 999999, 999999, 999999 ); + for ( int i = 0; i < 3; i++ ) { - value_minimize( aMins[ j ], metaVerts[ av ].xyz[ j ] ); - value_minimize( bMins[ j ], metaVerts[ bv ].xyz[ j ] ); + const int av = a.indexes[ i ]; + const int bv = b.indexes[ i ]; + for ( int j = 0; j < 3; j++ ) + { + value_minimize( aMins[ j ], metaVerts[ av ].xyz[ j ] ); + value_minimize( bMins[ j ], metaVerts[ bv ].xyz[ j ] ); + } } - } - /* test it */ - for ( int i = 0; i < 3; i++ ) - { - if ( aMins[ i ] < bMins[ i ] ) { - return 1; + /* test it */ + for ( int i = 0; i < 3; i++ ) + { + if ( aMins[ i ] < bMins[ i ] ) { + return false; + } + else if ( aMins[ i ] > bMins[ i ] ) { + return true; + } } - else if ( aMins[ i ] > bMins[ i ] ) { - return -1; - } - } - /* functionally equivalent */ - return 0; -} + /* functionally equivalent */ + return false; + } +}; @@ -1777,7 +1777,7 @@ void MergeMetaTriangles( void ){ Sys_FPrintf( SYS_VRB, "--- MergeMetaTriangles ---\n" ); /* sort the triangles by shader major, fognum minor */ - qsort( metaTriangles, numMetaTriangles, sizeof( metaTriangle_t ), CompareMetaTriangles ); + std::sort( metaTriangles, metaTriangles + numMetaTriangles, CompareMetaTriangles() ); /* init pacifier */ fOld = -1; diff --git a/tools/quake3/q3map2/tjunction.cpp b/tools/quake3/q3map2/tjunction.cpp index fbcf3652..04997c15 100644 --- a/tools/quake3/q3map2/tjunction.cpp +++ b/tools/quake3/q3map2/tjunction.cpp @@ -557,29 +557,6 @@ bool FixBrokenSurface( mapDrawSurface_t *ds ){ - - - -/* - ================ - EdgeCompare - ================ - */ -int EdgeCompare( const void *elem1, const void *elem2 ) { - const float d1 = reinterpret_cast( elem1 )->length; - const float d2 = reinterpret_cast( elem2 )->length; - - if ( d1 < d2 ) { - return -1; - } - if ( d1 > d2 ) { - return 1; - } - return 0; -} - - - /* FixTJunctions call after the surface list has been pruned @@ -637,8 +614,10 @@ void FixTJunctions( entity_t *ent ){ axialEdgeLines = numEdgeLines; // sort the non-axial edges by length - qsort( originalEdges, numOriginalEdges, sizeof( originalEdges[0] ), EdgeCompare ); - + std::sort( originalEdges, originalEdges + numOriginalEdges, []( const originalEdge_t& a, const originalEdge_t& b ){ + return a.length < b.length; + } ); + // add the non-axial edges, longest first // this gives the most accurate edge description for ( i = 0 ; i < numOriginalEdges ; i++ ) { diff --git a/tools/quake3/q3map2/vis.cpp b/tools/quake3/q3map2/vis.cpp index 5ecd81ea..f2281941 100644 --- a/tools/quake3/q3map2/vis.cpp +++ b/tools/quake3/q3map2/vis.cpp @@ -81,25 +81,15 @@ void prl( leaf_t *l ){ the earlier information. ============= */ -int PComp( const void *a, const void *b ){ - if ( ( *(const vportal_t *const *)a )->nummightsee == ( *(const vportal_t *const *)b )->nummightsee ) { - return 0; - } - if ( ( *(const vportal_t *const *)a )->nummightsee < ( *(const vportal_t *const *)b )->nummightsee ) { - return -1; - } - return 1; -} void SortPortals( void ){ - int i; - - for ( i = 0 ; i < numportals * 2 ; i++ ) + for ( int i = 0 ; i < numportals * 2 ; i++ ) sorted_portals[i] = &portals[i]; - if ( nosort ) { - return; + if ( !nosort ) { + std::sort( sorted_portals, sorted_portals + numportals * 2, []( vportal_t* const & a, vportal_t* const & b ){ + return a->nummightsee < b->nummightsee; + } ); } - qsort( sorted_portals, numportals * 2, sizeof( sorted_portals[0] ), PComp ); }