Skip to content

Commit

Permalink
Bug Fix: Manjaro Compilation on 0.8.x (#808)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
BenjamenMeyer and stephengtuggy authored Nov 26, 2023
1 parent 6906435 commit 8a7ca30
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 67 deletions.
3 changes: 2 additions & 1 deletion engine/src/cmd/script/script_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
64 changes: 39 additions & 25 deletions engine/src/gfx/aligned.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,39 @@
*/


#ifndef __ALIGNED_H
#define __ALIGNED_H
#ifndef VEGA_STRIKE_GFX_ALIGNED_H
#define VEGA_STRIKE_GFX_ALIGNED_H

#include <memory>
#include <cstddef>

#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<a>(x)
#define __alprn(x,a) std::assume_aligned<a>(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<typename T, typename U>
using Rebind = typename std::allocator_traits<T>::template rebind_alloc<U>;

template<typename T, typename = T>
struct HasRebind {
using value_type = T;
template<typename U> struct rebind { using other = HasRebind<U>; };
};

template <typename T, int ALIGN=16> class aligned_allocator : public std::allocator<T>
{
Expand All @@ -59,12 +67,18 @@ template <typename T, int ALIGN=16> class aligned_allocator : public std::alloca
typedef typename std::allocator<T>::value_type value_type;
typedef typename std::allocator<T>::size_type size_type;
typedef typename std::allocator<T>::difference_type difference_type;

static const int _OVERHEAD = (sizeof(T) + ALIGN-1) / ALIGN + sizeof(size_t);

aligned_allocator() {}
template<typename A> explicit aligned_allocator(const A &other) : std::allocator<T>(other) {}


aligned_allocator() = default;

template<typename U> struct rebind {
using value_type = T;
using other = aligned_allocator<U>;
};

template<typename A> explicit aligned_allocator(const A &other) : std::allocator<T>(other) {}

typename std::allocator<T>::pointer address ( typename std::allocator<T>::reference x ) const
{
if (sizeof(T) % ALIGN) {
Expand All @@ -73,7 +87,7 @@ template <typename T, int ALIGN=16> class aligned_allocator : public std::alloca
return __alpn(std::allocator<T>::address(x), ALIGN);
}
}

typename std::allocator<T>::const_pointer address ( typename std::allocator<T>::const_reference x ) const
{
if (sizeof(T) % ALIGN) {
Expand Down Expand Up @@ -216,5 +230,5 @@ typename COLL::pointer coll_start_pointer(COLL &a)
return aligned_allocator_traits<typename COLL::allocator_type>::ifaligned_start_pointer(a.get_allocator(), a[0]);
}

#endif
#endif //VEGA_STRIKE_GFX_ALIGNED_H

75 changes: 37 additions & 38 deletions engine/src/gfx/particle.cpp
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
/*
* 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 <https://www.gnu.org/licenses/>.
*/

#include "particle.h"
#include "gfxlib.h"
Expand Down Expand Up @@ -284,7 +283,7 @@ void ParticleTrail::DrawAndUpdate()
// Must sort
distances.clear();
distances.reserve(nparticles);
{ for (std::vector<QVector, aligned_allocator<QVector> >::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<float, unsigned short> dcomp(distances);
Expand Down Expand Up @@ -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);
Expand All @@ -362,16 +361,16 @@ void ParticleTrail::DrawAndUpdate()
}

// Remove dead particles anywhere
vector< Vector, aligned_allocator<Vector> >::iterator v = particleVel.begin();
vector< QVector, aligned_allocator<QVector> >::iterator loc = particleLoc.begin();
vector< GFXColor, aligned_allocator<GFXColor> >::iterator col = particleColor.begin();
vector< float, aligned_allocator<float> >::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<Vector> >::iterator vlast = particleVel.end() - 1;
vector< QVector, aligned_allocator<QVector> >::iterator loclast = particleLoc.end() - 1;
vector< GFXColor, aligned_allocator<GFXColor> >::iterator collast = particleColor.end() - 1;
vector< float, aligned_allocator<float> >::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;
Expand Down Expand Up @@ -403,10 +402,10 @@ void ParticleTrail::AddParticle( const ParticlePoint &P, const Vector &V, float
return;

if (particleLoc.size() > maxparticles) {
vector< Vector, aligned_allocator<Vector> >::iterator vel = particleVel.begin();
vector< QVector, aligned_allocator<QVector> >::iterator loc = particleLoc.begin();
vector< GFXColor, aligned_allocator<GFXColor> >::iterator col = particleColor.begin();
vector< float, aligned_allocator<float> >::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;
Expand Down
6 changes: 3 additions & 3 deletions engine/src/gfx/particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* along with Vega Strike. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef __PARTICLE_H
#define __PARTICLE_H
#ifndef VEGA_STRIKE_GFX_PARTICLE_H
#define VEGA_STRIKE_GFX_PARTICLE_H

#include <vector>
#include <string>
Expand Down Expand Up @@ -147,5 +147,5 @@ extern ParticleTrail particleTrail;
extern ParticleTrail smokeTrail;
extern ParticleTrail debrisTrail;

#endif
#endif //VEGA_STRIKKE_GFX_PARTICLE_H

0 comments on commit 8a7ca30

Please sign in to comment.