From 8f31d5182e7e09b13e28dbc231ba89c74a4d4e9d Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 16 Mar 2010 08:21:15 +0000 Subject: [PATCH] Tidy up some float/double conversion warnings --- CMakeLists.txt | 1 + include/BezierCurve.hpp | 8 +++-- include/IMap.hpp | 1 + include/IMesh.hpp | 6 ++-- include/ITrackSegment.hpp | 6 ++-- include/IXMLParser.hpp | 2 +- src/CrossoverTrack.cpp | 18 +++++------ src/CurvedTrack.cpp | 14 ++++----- src/Editor.cpp | 16 +++++----- src/Map.cpp | 27 +++++++++++----- src/Mesh.cpp | 16 +++++----- src/Points.cpp | 65 ++++++++++++++++++++------------------- src/SlopeTrack.cpp | 8 ++--- src/StraightTrack.cpp | 15 +++++---- src/Train.cpp | 4 +-- 15 files changed, 114 insertions(+), 93 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3582bda..179acdf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,6 +45,7 @@ include_directories (include ${CMAKE_CURRENT_BINARY_DIR}) if (NOT WIN32) # Unix add_definitions ("-Wall") +# add_definitions ("-Wall -Wconversion -Werror") endif (NOT WIN32) diff --git a/include/BezierCurve.hpp b/include/BezierCurve.hpp index ff79f30..78e88bc 100644 --- a/include/BezierCurve.hpp +++ b/include/BezierCurve.hpp @@ -38,9 +38,13 @@ struct BezierCurve { Vector cur = operator()(0.0), prev; length = 0.0; + + + const T step = static_cast(0.01); + const T min = static_cast(0.1); + const T max = static_cast(1.0); - const T step = 0.01; - for (T t = 0.1; t <= 1.0; t += step) { + for (T t = min; t <= max; t += step) { prev = cur; cur = operator()(t); diff --git a/include/IMap.hpp b/include/IMap.hpp index 3abe1ea..89db4aa 100644 --- a/include/IMap.hpp +++ b/include/IMap.hpp @@ -108,6 +108,7 @@ public: // Get the height above ground at a particular point virtual float heightAt(float x, float y) const = 0; + virtual float heightAt(Point where) const = 0; // Given a tile and an axis, return a vector indicating the slope // along that axis. `level' is set if the slope is the same across diff --git a/include/IMesh.hpp b/include/IMesh.hpp index b5b752b..6dee10c 100644 --- a/include/IMesh.hpp +++ b/include/IMesh.hpp @@ -40,7 +40,7 @@ struct IMeshBuffer { typedef Vector Vertex; typedef Vector Normal; typedef Point TexCoord; - typedef unsigned Index; + typedef size_t Index; virtual ~IMeshBuffer() {} @@ -64,7 +64,7 @@ struct IMeshBuffer { virtual void printStats() const = 0; }; -typedef std::tr1::shared_ptr IMeshBufferPtr; +typedef shared_ptr IMeshBufferPtr; // Generic interface to meshes struct IMesh { @@ -73,7 +73,7 @@ struct IMesh { virtual void render() const = 0; }; -typedef std::tr1::shared_ptr IMeshPtr; +typedef shared_ptr IMeshPtr; IMeshPtr makeMesh(IMeshBufferPtr aBuffer); IMeshBufferPtr makeMeshBuffer(); diff --git a/include/ITrackSegment.hpp b/include/ITrackSegment.hpp index b799e79..73ebe92 100644 --- a/include/ITrackSegment.hpp +++ b/include/ITrackSegment.hpp @@ -69,9 +69,9 @@ namespace track { // Wrappers for the above functions - void transform(double aDelta) const + void transform(float delta) const { - transformer(*this, aDelta); + transformer(*this, delta); } float gradient(float delta) const @@ -102,7 +102,7 @@ struct ITrackSegment : IXMLSerialisable { virtual void setOrigin(int x, int y, float height) = 0; // Get the length of this track segment - virtual double segmentLength(const track::TravelToken& aToken) const = 0; + virtual float segmentLength(const track::TravelToken& aToken) const = 0; // Get a travel token for this track segment starting at a particular // position and moving in a particular direciton diff --git a/include/IXMLParser.hpp b/include/IXMLParser.hpp index 97cccf0..8d6ffa7 100644 --- a/include/IXMLParser.hpp +++ b/include/IXMLParser.hpp @@ -64,7 +64,7 @@ namespace { "Cannot parse colour attribute with value '" + str + "'"); - return makeColour(r / 255.0f, g / 255.0f, b / 255.0f); + return makeRGB(r, g, b); } template diff --git a/src/CrossoverTrack.cpp b/src/CrossoverTrack.cpp index 1938daa..f544f64 100644 --- a/src/CrossoverTrack.cpp +++ b/src/CrossoverTrack.cpp @@ -41,7 +41,7 @@ public: void setOrigin(int x, int y, float h); void render() const; - double segmentLength(const track::TravelToken& aToken) const; + float segmentLength(const track::TravelToken& aToken) const; bool isValidDirection(const track::Direction& aDirection) const; track::Connection nextPosition(const track::TravelToken& aToken) const; void getEndpoints(vector >& aList) const; @@ -58,7 +58,7 @@ public: xml::element toXml() const; private: - void transform(const track::TravelToken& aToken, double aDelta) const; + void transform(const track::TravelToken& aToken, float delta) const; int myX, myY; float height; @@ -108,9 +108,9 @@ void CrossoverTrack::setOrigin(int x, int y, float h ) height = h; } -double CrossoverTrack::segmentLength(const track::TravelToken& aToken) const +float CrossoverTrack::segmentLength(const track::TravelToken& aToken) const { - return 1.0; + return 1.0f; } track::TravelToken @@ -132,20 +132,20 @@ CrossoverTrack::getTravelToken(track::Position aPosition, } void CrossoverTrack::transform(const track::TravelToken& aToken, - double aDelta) const + float delta) const { - assert(aDelta < 1.0); + assert(delta < 1.0); bool backwards = aToken.direction== -axis::X || aToken.direction == -axis::Y; if (backwards) { - aDelta = 1.0 - aDelta; + delta = 1.0f - delta; } track::Direction dir = backwards ? -aToken.direction : aToken.direction; - const double xTrans = dir == axis::X ? aDelta : 0; - const double yTrans = dir == axis::Y ? aDelta : 0; + const double xTrans = dir == axis::X ? delta : 0; + const double yTrans = dir == axis::Y ? delta : 0; glTranslated(static_cast(myX) + xTrans, height, diff --git a/src/CurvedTrack.cpp b/src/CurvedTrack.cpp index 3ad0979..5e07424 100644 --- a/src/CurvedTrack.cpp +++ b/src/CurvedTrack.cpp @@ -43,7 +43,7 @@ public: void render() const; void setOrigin(int x, int y, float h); - double segmentLength(const track::TravelToken& aToken) const; + float segmentLength(const track::TravelToken& aToken) const; Connection nextPosition(const track::TravelToken& aToken) const; bool isValidDirection(const Direction& aDirection) const; @@ -62,7 +62,7 @@ public: void setStateRenderHint() {} private: - void transform(const track::TravelToken& aToken, double aDelta) const; + void transform(const track::TravelToken& aToken, float delta) const; Vector cwEntryVector() const; Vector ccwEntryVector() const; void ensureValidDirection(const Direction& aDirection) const; @@ -110,9 +110,9 @@ CurvedTrack::getTravelToken(track::Position aPosition, return tok; } -void CurvedTrack::transform(const track::TravelToken& aToken, double aDelta) const +void CurvedTrack::transform(const track::TravelToken& aToken, float delta) const { - assert(aDelta < segmentLength(aToken)); + assert(delta < segmentLength(aToken)); glTranslated(static_cast(origin.x), height, @@ -122,7 +122,7 @@ void CurvedTrack::transform(const track::TravelToken& aToken, double aDelta) con bool backwards = aToken.direction == cwEntryVector(); - double ratio = aDelta / segmentLength(aToken); + double ratio = delta / segmentLength(aToken); if (backwards) ratio = 1.0 - ratio; @@ -135,10 +135,10 @@ void CurvedTrack::transform(const track::TravelToken& aToken, double aDelta) con glRotatef(180.0, 0, 1, 0); } -double CurvedTrack::segmentLength(const track::TravelToken& aToken) const +float CurvedTrack::segmentLength(const track::TravelToken& aToken) const { // Assume curve is only through 90 degrees - return M_PI * (static_cast(baseRadius) - 0.5) / 2.0; + return M_PI * (static_cast(baseRadius) - 0.5f) / 2.0f; } // diff --git a/src/Editor.cpp b/src/Editor.cpp index 6ead73f..4440578 100644 --- a/src/Editor.cpp +++ b/src/Editor.cpp @@ -91,7 +91,7 @@ private: }; Editor::Editor(IMapPtr aMap) - : map(aMap), myPosition(4.5, -17.5, -21.5), + : map(aMap), myPosition(4.5f, -17.5f, -21.5f), myTool(TRACK_TOOL), amScrolling(false), amDragging(false) { mySun = makeSunLight(); @@ -461,7 +461,7 @@ void Editor::drawDraggedTrack() } ITrackSegmentPtr track = makeCurvedTrack(startAngle, endAngle, xlen); - track->setOrigin(where.x, where.y, map->heightAt(where.x, where.y)); + track->setOrigin(where.x, where.y, map->heightAt(where)); vector > exits; track->getEndpoints(exits); @@ -531,11 +531,11 @@ void Editor::onMouseMove(IPickBufferPtr pickBuffer, int x, int y, else if (amScrolling) { const float speed = 0.05f; - myPosition.x -= xrel * speed; - myPosition.z -= xrel * speed; + myPosition.x -= static_cast(xrel) * speed; + myPosition.z -= static_cast(xrel) * speed; - myPosition.x += yrel * speed; - myPosition.z -= yrel * speed; + myPosition.x += static_cast(yrel) * speed; + myPosition.z -= static_cast(yrel) * speed; } } @@ -567,10 +567,10 @@ void Editor::onMouseClick(IPickBufferPtr pickBuffer, int x, int y, } } else if (aButton == MOUSE_WHEEL_UP) { - myPosition.y -= 0.5; + myPosition.y -= 0.5f; } else if (aButton == MOUSE_WHEEL_DOWN) { - myPosition.y += 0.5; + myPosition.y += 0.5f; } } diff --git a/src/Map.cpp b/src/Map.cpp index 7490283..21d5ea3 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -110,6 +110,7 @@ public: IStationPtr extendStation(Point aStartPos, Point aFinishPos); float heightAt(float x, float y) const; + float heightAt(Point where) const; Vector slopeAt(Point where, track::Direction axis, bool& level) const; Vector slopeBefore(Point where, @@ -814,15 +815,20 @@ void Map::postRenderSector(IGraphicsPtr aContext, int id, glEnable(GL_BLEND); glDisable(GL_TEXTURE_2D); + + const float blX = static_cast(botLeft.x); + const float blY = static_cast(botLeft.y); + const float trX = static_cast(topRight.x); + const float trY = static_cast(topRight.y); static const float seaLevel = -0.6f; gl::colour(makeRGB(0, 80, 160, 150)); glNormal3f(0.0f, 1.0f, 0.0f); glBegin(GL_QUADS); - glVertex3f(botLeft.x - 0.5f, seaLevel, botLeft.y - 0.5f); - glVertex3f(botLeft.x - 0.5f, seaLevel, topRight.y - 0.5f); - glVertex3f(topRight.x - 0.5f, seaLevel, topRight.y - 0.5f); - glVertex3f(topRight.x - 0.5f, seaLevel, botLeft.y - 0.5f); + glVertex3f(blX - 0.5f, seaLevel, blY - 0.5f); + glVertex3f(blX - 0.5f, seaLevel, trY - 0.5f); + glVertex3f(trX - 0.5f, seaLevel, trY - 0.5f); + glVertex3f(trX - 0.5f, seaLevel, blY - 0.5f); glEnd(); glPopAttrib(); @@ -967,12 +973,17 @@ float Map::heightAt(float x, float y) const const int xFloor = static_cast(floorf(x)); const int yFloor = static_cast(floorf(y)); - if (xFloor < 0 || yFloor < 0 - || xFloor >= myWidth || yFloor >= myDepth) + return heightAt(makePoint(xFloor, yFloor)); +} + +float Map::heightAt(Point where) const +{ + if (where.x < 0 || where.y < 0 + || where.x >= myWidth || where.y >= myDepth) return 0.0f; int indexes[4]; - tileVertices(x, y, indexes); + tileVertices(where.x, where.y, indexes); float avg = 0.0f; for (int i = 0; i < 4; i++) @@ -1118,7 +1129,7 @@ void Map::addScenery(Point where, ISceneryPtr s) else { tileAt(where.x, where.y).scenery = s; s->setPosition(static_cast(where.x), - heightAt(where.x, where.y), + heightAt(where), static_cast(where.y)); } } diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 14d2aef..51ffbb4 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -117,7 +117,7 @@ void MeshBuffer::add(const Vertex& aVertex, const Normal& aNormal) } } - const int index = vertices.size(); + const size_t index = vertices.size(); vertices.push_back(aVertex); normals.push_back(aNormal); indices.push_back(index); @@ -153,7 +153,7 @@ void MeshBuffer::add(const Vertex& aVertex, const Normal& aNormal, } } - const int index = vertices.size(); + const size_t index = vertices.size(); vertices.push_back(aVertex); normals.push_back(aNormal); colours.push_back(aColour); @@ -183,7 +183,7 @@ void MeshBuffer::add(const Vertex& aVertex, const Normal& aNormal, } } - const int index = vertices.size(); + const size_t index = vertices.size(); vertices.push_back(aVertex); normals.push_back(aNormal); texCoords.push_back(aTexCoord); @@ -376,9 +376,9 @@ private: Material material; bool hasMaterial, hasTexture; - int myVertexCount; + size_t myVertexCount; VertexData* myVertexData; - int myIndexCount; + size_t myIndexCount; GLushort* myIndices; }; @@ -437,7 +437,8 @@ void VertexArrayMesh::render() const glNormalPointer(GL_FLOAT, sizeof(VertexData), reinterpret_cast(&myVertexData->nx)); - glDrawElements(GL_TRIANGLES, myIndexCount, GL_UNSIGNED_SHORT, myIndices); + glDrawElements(GL_TRIANGLES, static_cast(myIndexCount), + GL_UNSIGNED_SHORT, myIndices); glPopClientAttrib(); glPopAttrib(); @@ -542,7 +543,8 @@ void VBOMesh::render() const glVertexPointer(3, GL_FLOAT, sizeof(VertexData), reinterpret_cast(0)); - glDrawElements(GL_TRIANGLES, myIndexCount, GL_UNSIGNED_SHORT, 0); + glDrawElements(GL_TRIANGLES, static_cast(myIndexCount), + GL_UNSIGNED_SHORT, 0); glBindBufferARB(GL_ARRAY_BUFFER, 0); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0); diff --git a/src/Points.cpp b/src/Points.cpp index 0375e95..e310c7e 100644 --- a/src/Points.cpp +++ b/src/Points.cpp @@ -34,7 +34,7 @@ public: // ITrackSegment interface void render() const; void setOrigin(int x, int y, float h) { myX = x; myY = y; height = h; } - double segmentLength(const track::TravelToken& aToken) const; + float segmentLength(const track::TravelToken& aToken) const; bool isValidDirection(const track::Direction& aDirection) const; track::Connection nextPosition(const track::TravelToken& aToken) const; void getEndpoints(vector >& aList) const; @@ -50,7 +50,7 @@ public: // IXMLSerialisable interface xml::element toXml() const; private: - void transform(const track::TravelToken& aToken, double aDelta) const; + void transform(const track::TravelToken& aToken, float aDelta) const; void ensureValidDirection(track::Direction aDirection) const; void renderArrow() const; @@ -239,12 +239,12 @@ void Points::render() const glPopMatrix(); } -double Points::segmentLength(const track::TravelToken& aToken) const +float Points::segmentLength(const track::TravelToken& aToken) const { if (aToken.position == displacedEndpoint()) return myCurve.length; else - return 3.0; + return 3.0f; } track::TravelToken Points::getTravelToken(track::Position position, @@ -267,29 +267,29 @@ track::TravelToken Points::getTravelToken(track::Position position, return tok; } -void Points::transform(const track::TravelToken& aToken, double aDelta) const +void Points::transform(const track::TravelToken& aToken, float delta) const { const float len = segmentLength(aToken); - assert(aDelta < len); + assert(delta < len); if (myX == aToken.position.x && myY == aToken.position.y && state == NOT_TAKEN) { if (aToken.direction == myAxis && (myAxis == -axis::X || myAxis == -axis::Y)) - aDelta -= 1.0; + delta -= 1.0f; - const double xTrans = - myAxis == axis::X ? aDelta - : (myAxis == -axis::X ? -aDelta : 0.0); - const double yTrans = - myAxis == axis::Y ? aDelta - : (myAxis == -axis::Y ? -aDelta : 0.0); + const float xTrans = + myAxis == axis::X ? delta + : (myAxis == -axis::X ? -delta : 0.0f); + const float yTrans = + myAxis == axis::Y ? delta + : (myAxis == -axis::Y ? -delta : 0.0f); - glTranslatef(static_cast(myX) + xTrans, + glTranslatef(static_cast(myX) + xTrans, height, - static_cast(myY) + yTrans); + static_cast(myY) + yTrans); if (myAxis == axis::Y || myAxis == -axis::Y) glRotated(-90.0, 0.0, 1.0, 0.0); @@ -297,27 +297,27 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const glTranslated(-0.5, 0.0, 0.0); } else if (aToken.position == straightEndpoint()) { - aDelta = 2.0 - aDelta; + delta = 2.0f - delta; if (aToken.direction == -myAxis && (myAxis == axis::X || myAxis == axis::Y)) - aDelta += 1.0; + delta += 1.0f; - const double xTrans = - myAxis == axis::X ? aDelta - : (myAxis == -axis::X ? -aDelta : 0.0); - const double yTrans = - myAxis == axis::Y ? aDelta - : (myAxis == -axis::Y ? -aDelta : 0.0); + const float xTrans = + myAxis == axis::X ? delta + : (myAxis == -axis::X ? -delta : 0.0f); + const float yTrans = + myAxis == axis::Y ? delta + : (myAxis == -axis::Y ? -delta : 0.0f); - glTranslatef(static_cast(myX) + xTrans, + glTranslatef(static_cast(myX) + xTrans, 0.0, - static_cast(myY) + yTrans); + static_cast(myY) + yTrans); if (myAxis == axis::Y || myAxis == -axis::Y) - glRotated(-90.0, 0.0, 1.0, 0.0); + glRotatef(-90.0f, 0.0f, 1.0f, 0.0f); - glTranslated(-0.5, 0.0, 0.0); + glTranslatef(-0.5f, 0.0f, 0.0f); } else if (aToken.position == displacedEndpoint() || state == TAKEN) { // Curving onto the straight section @@ -326,7 +326,7 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const // We have a slight problem in that the domain of the curve // function is [0,1] but the delta is in [0,len] so we have // to compress the delta into [0,1] here - const float curveDelta = aDelta / len; + const float curveDelta = delta / len; bool backwards = aToken.position == displacedEndpoint(); @@ -362,12 +362,15 @@ void Points::transform(const track::TravelToken& aToken, double aDelta) const else assert(false); - glTranslatef(myX + xTrans, 0.0f, myY + yTrans); + glTranslatef( + static_cast(myX) + xTrans, + 0.0f, + static_cast(myY) + yTrans); if (myAxis == axis::Y || myAxis == -axis::Y) - glRotated(-90.0, 0.0, 1.0, 0.0); + glRotatef(-90.0f, 0.0f, 1.0f, 0.0f); - glTranslated(-0.5, 0.0, 0.0); + glTranslatef(-0.5f, 0.0f, 0.0f); glRotatef(rotate, 0.0f, 1.0f, 0.0f); } diff --git a/src/SlopeTrack.cpp b/src/SlopeTrack.cpp index 8617eb6..bee5bfa 100644 --- a/src/SlopeTrack.cpp +++ b/src/SlopeTrack.cpp @@ -38,7 +38,7 @@ public: // ITrackSegment interface void render() const; void setOrigin(int x, int y, float h); - double segmentLength(const track::TravelToken& token) const; + float segmentLength(const track::TravelToken& token) const; track::TravelToken getTravelToken(track::Position pos, track::Direction dir) const; bool isValidDirection(const track::Direction& dir) const; @@ -57,7 +57,7 @@ public: private: void ensureValidDirection(const track::Direction& dir) const; - void transform(const track::TravelToken& token, double delta) const; + void transform(const track::TravelToken& token, float delta) const; float gradient(const track::TravelToken& token, float delta) const; Point origin; @@ -142,7 +142,7 @@ void SlopeTrack::setOrigin(int x, int y, float h) height = h + yOffset; } -double SlopeTrack::segmentLength(const track::TravelToken& token) const +float SlopeTrack::segmentLength(const track::TravelToken& token) const { return length; } @@ -209,7 +209,7 @@ float SlopeTrack::gradient(const track::TravelToken& token, float delta) const return curve.deriv(delta).y; } -void SlopeTrack::transform(const track::TravelToken& token, double delta) const +void SlopeTrack::transform(const track::TravelToken& token, float delta) const { assert(delta < length && delta >= 0.0f); diff --git a/src/StraightTrack.cpp b/src/StraightTrack.cpp index a8d5e40..bd9024e 100644 --- a/src/StraightTrack.cpp +++ b/src/StraightTrack.cpp @@ -41,9 +41,8 @@ public: void render() const; void setOrigin(int x, int y, float h); - double segmentLength(const track::TravelToken& aToken) const { return 1.0; } + float segmentLength(const track::TravelToken& token) const { return 1.0f; } - Vector offsetForDelta(double aDelta) const; Connection nextPosition(const track::TravelToken& aDirection) const; bool isValidDirection(const Direction& aDirection) const; void getEndpoints(vector >& aList) const; @@ -62,7 +61,7 @@ public: xml::element toXml() const; private: - void transform(const track::TravelToken& aToken, double aDelta) const; + void transform(const track::TravelToken& aToken, float delta) const; void ensureValidDirection(const track::Direction& aDirection) const; Point origin; // Absolute position @@ -104,15 +103,15 @@ StraightTrack::getTravelToken(track::Position aPosition, } void StraightTrack::transform(const track::TravelToken& aToken, - double aDelta) const + float delta) const { - assert(aDelta < 1.0); + assert(delta < 1.0); if (aToken.direction == -direction) - aDelta = 1.0 - aDelta; + delta = 1.0 - delta; - const double xTrans = direction == axis::X ? aDelta : 0; - const double yTrans = direction == axis::Y ? aDelta : 0; + const float xTrans = direction == axis::X ? delta : 0; + const float yTrans = direction == axis::Y ? delta : 0; glTranslated(static_cast(origin.x) + xTrans, height, diff --git a/src/Train.cpp b/src/Train.cpp index facfa40..e0a6e31 100644 --- a/src/Train.cpp +++ b/src/Train.cpp @@ -58,7 +58,7 @@ private: // segmentLength() This delta value ranges from 0 to that length and // indicates how far along the segment the train is ITrackSegmentPtr segment; - double segmentDelta; + float segmentDelta; track::TravelToken travelToken; // Direction train part is travelling along the track @@ -68,7 +68,7 @@ private: bool isLeading; // Handles reversal mid-segment - double movementSign; + float movementSign; bool operator==(const Part& other) const { -- 2.39.2