From 8a7ca3068770d2c5a519d894bd26dfadc16f2318 Mon Sep 17 00:00:00 2001 From: Benjamen Meyer <1074110+BenjamenMeyer@users.noreply.github.com> Date: Sun, 26 Nov 2023 17:17:15 -0500 Subject: [PATCH] Bug Fix: Manjaro Compilation on 0.8.x (#808) * Bug Fix: Manjaro Compilation on 0.8.x Compiler error on trying to concat a string and int. * Bug Fix: proper sprintf usage Forgot about using a buffer; fixed. * Bug Fix: allocator requires class not a fundamental type * Bug Fix: ManjaroLinux - Builds There is no need to specify the alignment allocator which can only be used by complex types. The GFXColor and QVector types are not sufficiently complex to support it; the Vector type probably is not as well. For now at least, just use `auto` for the iterators * Update engine/src/cmd/script/script_statement.cpp Co-authored-by: Stephen G Tuggy * aligned.h, particle.cpp: Try to use newer, more standard C++ constructs where available * particle.cpp, particle.h: Try using aligned_allocator again * aligned.h: Fix the GCC 13 compile errors for 0.8.x --------- Co-authored-by: Stephen G Tuggy --- engine/src/cmd/script/script_statement.cpp | 3 +- engine/src/gfx/aligned.h | 64 ++++++++++-------- engine/src/gfx/particle.cpp | 75 +++++++++++----------- engine/src/gfx/particle.h | 6 +- 4 files changed, 81 insertions(+), 67 deletions(-) diff --git a/engine/src/cmd/script/script_statement.cpp b/engine/src/cmd/script/script_statement.cpp index 066dee1a23..563d1c680d 100644 --- a/engine/src/cmd/script/script_statement.cpp +++ b/engine/src/cmd/script/script_statement.cpp @@ -77,7 +77,8 @@ void Mission::doIf( missionNode *node, int mode ) int nr_subnodes = node->subnodes.size(); if (nr_subnodes != 3) { - fatalError( node, mode, "an if-statement needs exact three subnodes, not "+nr_subnodes ); + std::string error_message = (boost::format("an if-statement needs exactly three subnodes, not %1%") % nr_subnodes).str(); + fatalError( node, mode, error_message.c_str() ); printf( "nr_of_subnodes: %d\n", nr_subnodes ); assert( 0 ); diff --git a/engine/src/gfx/aligned.h b/engine/src/gfx/aligned.h index faae1fc999..c13c11b311 100644 --- a/engine/src/gfx/aligned.h +++ b/engine/src/gfx/aligned.h @@ -23,31 +23,39 @@ */ -#ifndef __ALIGNED_H -#define __ALIGNED_H +#ifndef VEGA_STRIKE_GFX_ALIGNED_H +#define VEGA_STRIKE_GFX_ALIGNED_H #include #include -#if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7) -#define __alpn(x,a) x -#define __alprn(x,a) x -#define __alp(x) x -#define __alpr(x) x +#if defined(__cpp_lib_assume_aligned) + #define __alpn(x,a) std::assume_aligned(x) + #define __alprn(x,a) std::assume_aligned(x) + #define __alp(x) __alpn(x,16) + #define __alpr(x) __alprn(x,16) +#elif __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7) + #define __alpn(x,a) x + #define __alprn(x,a) x + #define __alp(x) x + #define __alpr(x) x #else -#define __alpn(x,a) ((typeof(x))(__builtin_assume_aligned(x,a))) -#define __alprn(x,a) ((typeof(x) __restrict__)(__builtin_assume_aligned(x,a))) -#define __alp(x) __alpn(x,16) -#define __alpr(x) __alprn(x,16) + #define __alpn(x,a) ((typeof(x))(__builtin_assume_aligned(x,a))) + #define __alprn(x,a) ((typeof(x) __restrict__)(__builtin_assume_aligned(x,a))) + #define __alp(x) __alpn(x,16) + #define __alpr(x) __alprn(x,16) #endif -#if defined(__GNUC__) -#define _ALIGNED(x) __attribute__ ((aligned(x))) -#else -#if defined(_MSC_VER) -#define _ALIGNED(x) __declspec(align(x)) -#endif -#endif +#define _ALIGNED(x) alignas(x) + +template + using Rebind = typename std::allocator_traits::template rebind_alloc; + +template + struct HasRebind { + using value_type = T; + template struct rebind { using other = HasRebind; }; + }; template class aligned_allocator : public std::allocator { @@ -59,12 +67,18 @@ template class aligned_allocator : public std::alloca typedef typename std::allocator::value_type value_type; typedef typename std::allocator::size_type size_type; typedef typename std::allocator::difference_type difference_type; - + static const int _OVERHEAD = (sizeof(T) + ALIGN-1) / ALIGN + sizeof(size_t); - - aligned_allocator() {} - template explicit aligned_allocator(const A &other) : std::allocator(other) {} - + + aligned_allocator() = default; + + template struct rebind { + using value_type = T; + using other = aligned_allocator; + }; + + template explicit aligned_allocator(const A &other) : std::allocator(other) {} + typename std::allocator::pointer address ( typename std::allocator::reference x ) const { if (sizeof(T) % ALIGN) { @@ -73,7 +87,7 @@ template class aligned_allocator : public std::alloca return __alpn(std::allocator::address(x), ALIGN); } } - + typename std::allocator::const_pointer address ( typename std::allocator::const_reference x ) const { if (sizeof(T) % ALIGN) { @@ -216,5 +230,5 @@ typename COLL::pointer coll_start_pointer(COLL &a) return aligned_allocator_traits::ifaligned_start_pointer(a.get_allocator(), a[0]); } -#endif +#endif //VEGA_STRIKE_GFX_ALIGNED_H diff --git a/engine/src/gfx/particle.cpp b/engine/src/gfx/particle.cpp index 612f1ea370..c184998fcd 100644 --- a/engine/src/gfx/particle.cpp +++ b/engine/src/gfx/particle.cpp @@ -1,27 +1,26 @@ -/** -* particle.cpp -* -* Copyright (C) 2001-2002 Daniel Horn -* Copyright (C) 2002-2019 pyramid3d and other Vega Strike Contributors -* Copyright (C) 2019-2022 Stephen G. Tuggy and other Vega Strike Contributors -* -* https://github.com/vegastrike/Vega-Strike-Engine-Source -* -* This file is part of Vega Strike. -* -* Vega Strike is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 2 of the License, or -* (at your option) any later version. -* -* Vega Strike is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with Vega Strike. If not, see . -*/ +/* + * particle.cpp + * + * Copyright (C) 2001-2023 Daniel Horn, pyramid3d, Stephen G. Tuggy, + * and other Vega Strike Contributors + * + * https://github.com/vegastrike/Vega-Strike-Engine-Source + * + * This file is part of Vega Strike. + * + * Vega Strike is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Vega Strike is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Vega Strike. If not, see . + */ #include "particle.h" #include "gfxlib.h" @@ -284,7 +283,7 @@ void ParticleTrail::DrawAndUpdate() // Must sort distances.clear(); distances.reserve(nparticles); - { for (std::vector >::const_iterator it = particleLoc.begin(); it != particleLoc.end(); ++it) { + { for (auto it = particleLoc.begin(); it != particleLoc.end(); ++it) { distances.push_back((campos - *it).MagnitudeSquared()); } } IndexCompare dcomp(distances); @@ -340,7 +339,7 @@ void ParticleTrail::DrawAndUpdate() float mytime = GetElapsedTime(); if (fadeColor) { float fadetimef = pfade * mytime; - GFXColor _ALIGNED(16) fadetime = GFXColor(fadetimef, fadetimef, fadetimef, fadetimef); + _ALIGNED(16) GFXColor fadetime = GFXColor(fadetimef, fadetimef, fadetimef, fadetimef); UpdateColor(particleLoc, particleVel, particleColor, fadetime, mytime); } else { UpdateAlpha(particleLoc, particleVel, particleColor, mytime, pfade); @@ -362,16 +361,16 @@ void ParticleTrail::DrawAndUpdate() } // Remove dead particles anywhere - vector< Vector, aligned_allocator >::iterator v = particleVel.begin(); - vector< QVector, aligned_allocator >::iterator loc = particleLoc.begin(); - vector< GFXColor, aligned_allocator >::iterator col = particleColor.begin(); - vector< float, aligned_allocator >::iterator sz = particleSize.begin(); + auto v = particleVel.begin(); + auto loc = particleLoc.begin(); + auto col = particleColor.begin(); + auto sz = particleSize.begin(); while ( col != particleColor.end() ) { if ( !(col->a > minalpha) ) { - vector< Vector, aligned_allocator >::iterator vlast = particleVel.end() - 1; - vector< QVector, aligned_allocator >::iterator loclast = particleLoc.end() - 1; - vector< GFXColor, aligned_allocator >::iterator collast = particleColor.end() - 1; - vector< float, aligned_allocator >::iterator szlast = particleSize.end() - 1; + auto vlast = particleVel.end() - 1; + auto loclast = particleLoc.end() - 1; + auto collast = particleColor.end() - 1; + auto szlast = particleSize.end() - 1; if (col != collast) { *v = *vlast; *loc = *loclast; @@ -403,10 +402,10 @@ void ParticleTrail::AddParticle( const ParticlePoint &P, const Vector &V, float return; if (particleLoc.size() > maxparticles) { - vector< Vector, aligned_allocator >::iterator vel = particleVel.begin(); - vector< QVector, aligned_allocator >::iterator loc = particleLoc.begin(); - vector< GFXColor, aligned_allocator >::iterator col = particleColor.begin(); - vector< float, aligned_allocator >::iterator sz = particleSize.begin(); + auto vel = particleVel.begin(); + auto loc = particleLoc.begin(); + auto col = particleColor.begin(); + auto sz = particleSize.begin(); size_t off = ( (size_t) rand() ) % particleLoc.size(); vel += off; loc += off; diff --git a/engine/src/gfx/particle.h b/engine/src/gfx/particle.h index 846a15192a..c566830a43 100644 --- a/engine/src/gfx/particle.h +++ b/engine/src/gfx/particle.h @@ -22,8 +22,8 @@ * along with Vega Strike. If not, see . */ -#ifndef __PARTICLE_H -#define __PARTICLE_H +#ifndef VEGA_STRIKE_GFX_PARTICLE_H +#define VEGA_STRIKE_GFX_PARTICLE_H #include #include @@ -147,5 +147,5 @@ extern ParticleTrail particleTrail; extern ParticleTrail smokeTrail; extern ParticleTrail debrisTrail; -#endif +#endif //VEGA_STRIKKE_GFX_PARTICLE_H