From bff1a50c34d870a814277d1764665377fc24a230 Mon Sep 17 00:00:00 2001 From: cutealien Date: Mon, 1 Jan 2024 15:29:28 +0000 Subject: [PATCH] Minor code cleanup Mostly const fixes in headers to make it easier for users to have more warnings enabled in static code analysis Also updating our own rules a bit (kicking some out we won't need yet). git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6583 dfc29bdd-3216-0410-991c-e03cc46cb475 --- include/IProfiler.h | 6 ++--- include/aabbox3d.h | 2 +- include/dimension2d.h | 2 +- include/heapsort.h | 9 ++++---- include/irrMap.h | 2 +- include/matrix4.h | 10 ++++---- include/plane3d.h | 4 ++-- include/triangle3d.h | 20 ++++++++-------- include/vector3d.h | 12 +++++----- source/Irrlicht/CGeometryCreator.cpp | 24 ++++++++------------ source/Irrlicht/CIrrMeshFileLoader.cpp | 8 ++++--- source/Irrlicht/Irrlicht.ruleset | 3 +++ source/Irrlicht/Irrlicht17.0.vcxproj | 4 ++-- source/Irrlicht/Irrlicht17.0.vcxproj.filters | 6 ----- 14 files changed, 53 insertions(+), 59 deletions(-) diff --git a/include/IProfiler.h b/include/IProfiler.h index 21a5dea4..88f8227f 100644 --- a/include/IProfiler.h +++ b/include/IProfiler.h @@ -280,7 +280,7 @@ protected: void IProfiler::start(s32 id) { - s32 idx = ProfileDatas.binary_search(SProfileData(id)); + const s32 idx = ProfileDatas.binary_search(SProfileData(id)); if ( idx >= 0 && Timer ) { ++ProfileDatas[idx].StartStopCounter; @@ -352,7 +352,7 @@ void IProfiler::add(s32 id, const core::stringw &name, const core::stringw &grou } SProfileData data(id); - s32 idx = ProfileDatas.binary_search(data); + const s32 idx = ProfileDatas.binary_search(data); if ( idx < 0 ) { data.reset(); @@ -422,7 +422,7 @@ bool IProfiler::findGroupIndex(u32 & result, const core::stringw &name) const void IProfiler::resetDataById(s32 id) { - s32 idx = ProfileDatas.binary_search(SProfileData(id)); + const s32 idx = ProfileDatas.binary_search(SProfileData(id)); if ( idx >= 0 ) { resetDataByIndex((u32)idx); diff --git a/include/aabbox3d.h b/include/aabbox3d.h index c3ec0186..78bdee38 100644 --- a/include/aabbox3d.h +++ b/include/aabbox3d.h @@ -207,7 +207,7 @@ class aabbox3d \return Interpolated box. */ aabbox3d getInterpolated(const aabbox3d& other, f32 d) const { - f32 inv = 1.0f - d; + const f32 inv = 1.0f - d; return aabbox3d((other.MinEdge*inv) + (MinEdge*d), (other.MaxEdge*inv) + (MaxEdge*d)); } diff --git a/include/dimension2d.h b/include/dimension2d.h index 7bd96ae3..508e4094 100644 --- a/include/dimension2d.h +++ b/include/dimension2d.h @@ -195,7 +195,7 @@ namespace core \return Interpolated dimension. */ dimension2d getInterpolated(const dimension2d& other, f32 d) const { - f32 inv = (1.0f - d); + const f32 inv = (1.0f - d); return dimension2d( (T)(other.Width*inv + Width*d), (T)(other.Height*inv + Height*d)); } diff --git a/include/heapsort.h b/include/heapsort.h index cbee7c73..0e844e6f 100644 --- a/include/heapsort.h +++ b/include/heapsort.h @@ -45,18 +45,17 @@ inline void heapsort(T* array_, s32 size) // the maximum always +2 and the element always +1 T* virtualArray = array_ - 1; - s32 virtualSize = size + 2; - s32 i; + const s32 virtualSize = size + 2; // build heap - for (i=((size-1)/2); i>=0; --i) + for (s32 i=((size-1)/2); i>=0; --i) heapsink(virtualArray, i+1, virtualSize-1); // sort array, leave out the last element (0) - for (i=size-1; i>0; --i) + for (s32 i=size-1; i>0; --i) { - T t = array_[0]; + const T t = array_[0]; array_[0] = array_[i]; array_[i] = t; heapsink(virtualArray, 1, i + 1); diff --git a/include/irrMap.h b/include/irrMap.h index ff55e438..00c365dd 100644 --- a/include/irrMap.h +++ b/include/irrMap.h @@ -915,7 +915,7 @@ class map //! Returns a Constiterator ConstIterator getConstIterator() const { - Iterator it(getRoot()); + const Iterator it(getRoot()); return it; } diff --git a/include/matrix4.h b/include/matrix4.h index 3b894b1a..04f54a6d 100644 --- a/include/matrix4.h +++ b/include/matrix4.h @@ -710,7 +710,7 @@ namespace core } return *this; #else - CMatrix4 temp ( *this ); + const CMatrix4 temp ( *this ); return setbyproduct_nocheck( temp, other ); #endif } @@ -1186,7 +1186,7 @@ namespace core template inline void CMatrix4::rotateVect( vector3df& vect ) const { - vector3d tmp(static_cast(vect.X), static_cast(vect.Y), static_cast(vect.Z)); + const vector3d tmp(static_cast(vect.X), static_cast(vect.Y), static_cast(vect.Z)); vect.X = static_cast(tmp.X*M[0] + tmp.Y*M[4] + tmp.Z*M[8]); vect.Y = static_cast(tmp.X*M[1] + tmp.Y*M[5] + tmp.Z*M[9]); vect.Z = static_cast(tmp.X*M[2] + tmp.Y*M[6] + tmp.Z*M[10]); @@ -1213,7 +1213,7 @@ namespace core template inline void CMatrix4::inverseRotateVect( vector3df& vect ) const { - vector3d tmp(static_cast(vect.X), static_cast(vect.Y), static_cast(vect.Z)); + const vector3d tmp(static_cast(vect.X), static_cast(vect.Y), static_cast(vect.Z)); vect.X = static_cast(tmp.X*M[0] + tmp.Y*M[1] + tmp.Z*M[2]); vect.Y = static_cast(tmp.X*M[4] + tmp.Y*M[5] + tmp.Z*M[6]); vect.Z = static_cast(tmp.X*M[8] + tmp.Y*M[9] + tmp.Z*M[10]); @@ -1278,7 +1278,7 @@ namespace core transformVect(member, plane.getMemberPoint()); // Transform the normal by the transposed inverse of the matrix - CMatrix4 transposedInverse(*this, EM4CONST_INVERSE_TRANSPOSED); + const CMatrix4 transposedInverse(*this, EM4CONST_INVERSE_TRANSPOSED); vector3df normal = plane.Normal; transposedInverse.rotateVect(normal); plane.setPlane(member, normal.normalize()); @@ -2002,7 +2002,7 @@ namespace core t.normalize(); // axis multiplication by sin - core::vector3df vs(t.crossProduct(f)); + const core::vector3df vs(t.crossProduct(f)); // axis of rotation core::vector3df v(vs); diff --git a/include/plane3d.h b/include/plane3d.h index 6f7c4d7c..0c47af06 100644 --- a/include/plane3d.h +++ b/include/plane3d.h @@ -107,8 +107,8 @@ class plane3d f32 getKnownIntersectionWithLine(const vector3d& linePoint1, const vector3d& linePoint2) const { - vector3d vect = linePoint2 - linePoint1; - T t2 = (f32)Normal.dotProduct(vect); + const vector3d vect = linePoint2 - linePoint1; + const T t2 = (f32)Normal.dotProduct(vect); return (f32)-((Normal.dotProduct(linePoint1) + D) / t2); } diff --git a/include/triangle3d.h b/include/triangle3d.h index 7e0ff81e..4912e7eb 100644 --- a/include/triangle3d.h +++ b/include/triangle3d.h @@ -88,10 +88,10 @@ namespace core \return True if the point is inside the triangle, otherwise false. */ bool isPointInside(const vector3d& p) const { - vector3d af64((f64)pointA.X, (f64)pointA.Y, (f64)pointA.Z); - vector3d bf64((f64)pointB.X, (f64)pointB.Y, (f64)pointB.Z); - vector3d cf64((f64)pointC.X, (f64)pointC.Y, (f64)pointC.Z); - vector3d pf64((f64)p.X, (f64)p.Y, (f64)p.Z); + const vector3d af64((f64)pointA.X, (f64)pointA.Y, (f64)pointA.Z); + const vector3d bf64((f64)pointB.X, (f64)pointB.Y, (f64)pointB.Z); + const vector3d cf64((f64)pointC.X, (f64)pointC.Y, (f64)pointC.Z); + const vector3d pf64((f64)p.X, (f64)p.Y, (f64)p.Z); return (isOnSameSide(pf64, af64, bf64, cf64) && isOnSameSide(pf64, bf64, af64, cf64) && isOnSameSide(pf64, cf64, af64, bf64)); @@ -174,7 +174,7 @@ namespace core const vector3d lineVectf64(lineVect.X, lineVect.Y, lineVect.Z); vector3d outIntersectionf64; - core::triangle3d trianglef64(vector3d((f64)pointA.X, (f64)pointA.Y, (f64)pointA.Z) + const core::triangle3d trianglef64(vector3d((f64)pointA.X, (f64)pointA.Y, (f64)pointA.Z) ,vector3d((f64)pointB.X, (f64)pointB.Y, (f64)pointB.Z) , vector3d((f64)pointC.X, (f64)pointC.Y, (f64)pointC.Z)); const vector3d normalf64 = trianglef64.getNormal().normalize(); @@ -183,8 +183,8 @@ namespace core if ( core::iszero ( t2 = normalf64.dotProduct(lineVectf64) ) ) return false; - f64 d = trianglef64.pointA.dotProduct(normalf64); - f64 t = -(normalf64.dotProduct(linePointf64) - d) / t2; + const f64 d = trianglef64.pointA.dotProduct(normalf64); + const f64 t = -(normalf64.dotProduct(linePointf64) - d) / t2; outIntersectionf64 = linePointf64 + (lineVectf64 * t); outIntersection.X = (T)outIntersectionf64.X; @@ -246,14 +246,14 @@ namespace core const vector3d& a, const vector3d& b) const { vector3d bminusa = b - a; - vector3d cp1 = bminusa.crossProduct(p1 - a); - vector3d cp2 = bminusa.crossProduct(p2 - a); + const vector3d cp1 = bminusa.crossProduct(p1 - a); + const vector3d cp2 = bminusa.crossProduct(p2 - a); f64 res = cp1.dotProduct(cp2); if ( res < 0 ) { // This catches some floating point troubles. // Unfortunately slightly expensive and we don't really know the best epsilon for iszero. - vector3d cp1n = bminusa.normalize().crossProduct((p1 - a).normalize()); + const vector3d cp1n = bminusa.normalize().crossProduct((p1 - a).normalize()); if (core::iszero(cp1n.X, (f64)ROUNDING_ERROR_f32) && core::iszero(cp1n.Y, (f64)ROUNDING_ERROR_f32) && core::iszero(cp1n.Z, (f64)ROUNDING_ERROR_f32) ) diff --git a/include/vector3d.h b/include/vector3d.h index 90406962..5eb9270d 100644 --- a/include/vector3d.h +++ b/include/vector3d.h @@ -243,8 +243,8 @@ namespace core void rotateXZBy(f64 degrees, const vector3d& center=vector3d()) { degrees *= DEGTORAD64; - f64 cs = cos(degrees); - f64 sn = sin(degrees); + const f64 cs = cos(degrees); + const f64 sn = sin(degrees); X -= center.X; Z -= center.Z; set((T)(X*cs - Z*sn), Y, (T)(X*sn + Z*cs)); @@ -258,8 +258,8 @@ namespace core void rotateXYBy(f64 degrees, const vector3d& center=vector3d()) { degrees *= DEGTORAD64; - f64 cs = cos(degrees); - f64 sn = sin(degrees); + const f64 cs = cos(degrees); + const f64 sn = sin(degrees); X -= center.X; Y -= center.Y; set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs), Z); @@ -273,8 +273,8 @@ namespace core void rotateYZBy(f64 degrees, const vector3d& center=vector3d()) { degrees *= DEGTORAD64; - f64 cs = cos(degrees); - f64 sn = sin(degrees); + const f64 cs = cos(degrees); + const f64 sn = sin(degrees); Z -= center.Z; Y -= center.Y; set(X, (T)(Y*cs - Z*sn), (T)(Y*sn + Z*cs)); diff --git a/source/Irrlicht/CGeometryCreator.cpp b/source/Irrlicht/CGeometryCreator.cpp index eb6e772d..e5446c2e 100644 --- a/source/Irrlicht/CGeometryCreator.cpp +++ b/source/Irrlicht/CGeometryCreator.cpp @@ -239,10 +239,8 @@ IMesh* CGeometryCreator::createHillPlaneMesh( vtx.Color.set(255,255,255,255); // create vertices from left-front to right-back - u32 x; - f32 sx=0.f, tsx=0.f; - for (x=0; xVertices.size()) { - c8 textureName[64]; // create texture for this block video::IImage* img = driver->createImage(texture->getColorFormat(), core::dimension2d(core::floor32(blockSize.Width*thRel.X), core::floor32(blockSize.Height*thRel.Y))); texture->copyTo(img, core::position2di(0,0), core::recti( core::position2d(core::floor32(processed.X*thRel.X), core::floor32(processed.Y*thRel.Y)), core::dimension2d(core::floor32(blockSize.Width*thRel.X), core::floor32(blockSize.Height*thRel.Y))), 0); + c8 textureName[64]; sprintf(textureName, "terrain%u_%u", tm, mesh->getMeshBufferCount()); buffer->Material.setTexture(0, driver->addTexture(textureName, img)); @@ -665,7 +663,6 @@ IMesh* CGeometryCreator::createSphereMesh(f32 radius, u32 polyCountX, u32 polyCo const f64 AngleY = core::PI / polyCountY; u32 i=0; - f64 axz; // we don't start at 0. @@ -676,7 +673,7 @@ IMesh* CGeometryCreator::createSphereMesh(f32 radius, u32 polyCountX, u32 polyCo { ay += AngleY; const f64 sinay = sin(ay); - axz = 0; + f64 axz = 0; // calculate the necessary vertices without the doubled one for (u32 xz = 0;xz < polyCountX; ++xz) @@ -754,13 +751,12 @@ IMesh* CGeometryCreator::createCylinderMesh(f32 radius, f32 length, const f32 recTessellation = core::reciprocal((f32)tessellation); const f32 angleStep = (core::PI * 2.f ) * recTessellation; - u32 i; video::S3DVertex v; v.Color = color; buffer->Vertices.reallocate(tessellation*2+2+(closeTop?2:1)); buffer->Indices.reallocate(tessellation*(closeTop?12:9)); f32 tcx = 0.f; - for ( i = 0; i <= tessellation; ++i ) + for (u32 i = 0; i <= tessellation; ++i ) { const f32 angle = angleStep * i; v.Pos.X = radius * cosf(angle); @@ -790,7 +786,7 @@ IMesh* CGeometryCreator::createCylinderMesh(f32 radius, f32 length, } const u32 nonWrappedSize = tessellation*2; - for (i=0; i != nonWrappedSize; i += 2) + for (u32 i=0; i != nonWrappedSize; i += 2) { buffer->Indices.push_back(i + 2); buffer->Indices.push_back(i + 0); @@ -814,7 +810,7 @@ IMesh* CGeometryCreator::createCylinderMesh(f32 radius, f32 length, u32 index = buffer->Vertices.size() - 1; - for ( i = 0; i != nonWrappedSize; i += 2 ) + for (u32 i = 0; i != nonWrappedSize; i += 2 ) { buffer->Indices.push_back(index); buffer->Indices.push_back(i + 0); @@ -836,7 +832,7 @@ IMesh* CGeometryCreator::createCylinderMesh(f32 radius, f32 length, index = buffer->Vertices.size() - 1; - for ( i = 0; i != nonWrappedSize; i += 2 ) + for (u32 i = 0; i != nonWrappedSize; i += 2 ) { buffer->Indices.push_back(i + 1); buffer->Indices.push_back(index); @@ -988,8 +984,8 @@ irr::scene::IMesh* CGeometryCreator::createTorusMesh(irr::f32 majorRadius, irr:: core::swap(angleStart, angleEnd); const f32 radStart = angleStart * core::DEGTORAD; const f32 radEnd = angleEnd * core::DEGTORAD; - const f32 radMajor = radEnd-radStart; - const f32 radStepMajor = radMajor / majorSegments; + const f32 radMajorLen = radEnd-radStart; + const f32 radStepMajor = radMajorLen / majorSegments; const f32 TWO_PI = 2.f*core::PI; const f32 radStepMinor = TWO_PI / minorSegments; diff --git a/source/Irrlicht/CIrrMeshFileLoader.cpp b/source/Irrlicht/CIrrMeshFileLoader.cpp index 10fad47a..952095ac 100644 --- a/source/Irrlicht/CIrrMeshFileLoader.cpp +++ b/source/Irrlicht/CIrrMeshFileLoader.cpp @@ -197,7 +197,6 @@ IMeshBuffer* CIrrMeshFileLoader::readMeshBuffer(io::IXMLReader* reader) if (vertexTypeName1 == vertexType) { buffer = new CDynamicMeshBuffer(irr::video::EVT_STANDARD, itype); - } else if (vertexTypeName2 == vertexType) @@ -209,8 +208,11 @@ IMeshBuffer* CIrrMeshFileLoader::readMeshBuffer(io::IXMLReader* reader) { buffer = new CDynamicMeshBuffer(irr::video::EVT_TANGENTS, itype); } - buffer->getVertexBuffer().reallocate(vertexCount); - buffer->Material = material; + if ( buffer ) + { + buffer->getVertexBuffer().reallocate(vertexCount); + buffer->Material = material; + } } else if (indicesSectionName == nodeName) diff --git a/source/Irrlicht/Irrlicht.ruleset b/source/Irrlicht/Irrlicht.ruleset index 10618656..e3c3f748 100644 --- a/source/Irrlicht/Irrlicht.ruleset +++ b/source/Irrlicht/Irrlicht.ruleset @@ -247,6 +247,7 @@ + @@ -257,6 +258,7 @@ + @@ -274,6 +276,7 @@ + diff --git a/source/Irrlicht/Irrlicht17.0.vcxproj b/source/Irrlicht/Irrlicht17.0.vcxproj index 4c2302df..e33406f7 100644 --- a/source/Irrlicht/Irrlicht17.0.vcxproj +++ b/source/Irrlicht/Irrlicht17.0.vcxproj @@ -162,7 +162,7 @@ true true AllRules.ruleset - AllRules.ruleset + Irrlicht.ruleset @@ -180,7 +180,7 @@ AllRules.ruleset - AllRules.ruleset + Irrlicht.ruleset diff --git a/source/Irrlicht/Irrlicht17.0.vcxproj.filters b/source/Irrlicht/Irrlicht17.0.vcxproj.filters index 3a973850..020e06a7 100644 --- a/source/Irrlicht/Irrlicht17.0.vcxproj.filters +++ b/source/Irrlicht/Irrlicht17.0.vcxproj.filters @@ -1438,12 +1438,6 @@ include - - include\scene - - - include\scene - Irrlicht\scene\sceneNodes