Преглед на файлове

Warning, compile & script binding fixes related to MathDefs.h. Use multiple template parameters where necessary to allow e.g. Min() or Max() with mixed int and unsigned values. Silence repeated warnings in MathDefs.h on MSVC.

Lasse Öörni преди 9 години
родител
ревизия
039542e91c

+ 10 - 10
Source/Urho3D/AngelScript/MathAPI.cpp

@@ -52,17 +52,17 @@ static void RegisterMathFunctions(asIScriptEngine* engine)
     engine->RegisterGlobalProperty("const uint M_MIN_UNSIGNED", (void*)&M_MIN_UNSIGNED);
     engine->RegisterGlobalProperty("const uint M_MIN_UNSIGNED", (void*)&M_MIN_UNSIGNED);
     engine->RegisterGlobalProperty("const uint M_MAX_UNSIGNED", (void*)&M_MAX_UNSIGNED);
     engine->RegisterGlobalProperty("const uint M_MAX_UNSIGNED", (void*)&M_MAX_UNSIGNED);
 
 
-    engine->RegisterGlobalFunction("bool Equals(float, float)", asFUNCTION(Equals), asCALL_CDECL);
+    engine->RegisterGlobalFunction("bool Equals(float, float)", asFUNCTION(Equals<float>), asCALL_CDECL);
     engine->RegisterGlobalFunction("bool IsNaN(float)", asFUNCTION(IsNaN), asCALL_CDECL);
     engine->RegisterGlobalFunction("bool IsNaN(float)", asFUNCTION(IsNaN), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float Sin(float)", asFUNCTION(Sin), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float Cos(float)", asFUNCTION(Cos), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float Tan(float)", asFUNCTION(Tan), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float Asin(float)", asFUNCTION(Asin), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float Acos(float)", asFUNCTION(Acos), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float Atan(float)", asFUNCTION(Atan), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float Atan2(float, float)", asFUNCTION(Atan2), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Sin(float)", asFUNCTION(Sin<float>), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Cos(float)", asFUNCTION(Cos<float>), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Tan(float)", asFUNCTION(Tan<float>), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Asin(float)", asFUNCTION(Asin<float>), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Acos(float)", asFUNCTION(Acos<float>), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Atan(float)", asFUNCTION(Atan<float>), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Atan2(float, float)", asFUNCTION(Atan2<float>), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Abs(float)", asFUNCTIONPR(Abs, (float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Abs(float)", asFUNCTIONPR(Abs, (float), float), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float Sign(float)", asFUNCTION(Sign), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float Sign(float)", asFUNCTION(Sign<float>), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Sqrt(float)", asFUNCTION(sqrtf), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Sqrt(float)", asFUNCTION(sqrtf), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Pow(float, float)", asFUNCTION(powf), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Pow(float, float)", asFUNCTION(powf), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Min(float, float)", asFUNCTIONPR(Min, (float, float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Min(float, float)", asFUNCTIONPR(Min, (float, float), float), asCALL_CDECL);
@@ -70,7 +70,7 @@ static void RegisterMathFunctions(asIScriptEngine* engine)
     engine->RegisterGlobalFunction("float Max(float, float)", asFUNCTIONPR(Max, (float, float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Max(float, float)", asFUNCTIONPR(Max, (float, float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("int Max(int, int)", asFUNCTIONPR(Max, (int, int), int), asCALL_CDECL);
     engine->RegisterGlobalFunction("int Max(int, int)", asFUNCTIONPR(Max, (int, int), int), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Clamp(float, float, float)", asFUNCTIONPR(Clamp, (float, float, float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Clamp(float, float, float)", asFUNCTIONPR(Clamp, (float, float, float), float), asCALL_CDECL);
-    engine->RegisterGlobalFunction("float SmoothStep(float, float, float)", asFUNCTION(SmoothStep), asCALL_CDECL);
+    engine->RegisterGlobalFunction("float SmoothStep(float, float, float)", asFUNCTION(SmoothStep<float>), asCALL_CDECL);
     engine->RegisterGlobalFunction("int Clamp(int, int, int)", asFUNCTIONPR(Clamp, (int, int, int), int), asCALL_CDECL);
     engine->RegisterGlobalFunction("int Clamp(int, int, int)", asFUNCTIONPR(Clamp, (int, int, int), int), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Lerp(float, float, float)", asFUNCTIONPR(Lerp, (float, float, float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Lerp(float, float, float)", asFUNCTIONPR(Lerp, (float, float, float), float), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Mod(float, float)", asFUNCTION(fmodf), asCALL_CDECL);
     engine->RegisterGlobalFunction("float Mod(float, float)", asFUNCTION(fmodf), asCALL_CDECL);

+ 3 - 3
Source/Urho3D/Core/Profiler.cpp

@@ -106,7 +106,7 @@ void Profiler::PrintData(ProfilerBlock* block, String& output, unsigned depth, u
     char line[LINE_MAX_LENGTH];
     char line[LINE_MAX_LENGTH];
     char indentedName[LINE_MAX_LENGTH];
     char indentedName[LINE_MAX_LENGTH];
 
 
-    unsigned intervalFrames = (unsigned)Max(intervalFrames_, 1);
+    unsigned intervalFrames = Max(intervalFrames_, (unsigned)1);
 
 
     if (depth >= maxDepth)
     if (depth >= maxDepth)
         return;
         return;
@@ -129,7 +129,7 @@ void Profiler::PrintData(ProfilerBlock* block, String& output, unsigned depth, u
                 float frame = block->intervalTime_ / intervalFrames / 1000.0f;
                 float frame = block->intervalTime_ / intervalFrames / 1000.0f;
                 float all = block->intervalTime_ / 1000.0f;
                 float all = block->intervalTime_ / 1000.0f;
 
 
-                sprintf(line, "%s %5u %8.3f %8.3f %8.3f %9.3f\n", indentedName, Min(block->intervalCount_, 99999),
+                sprintf(line, "%s %5u %8.3f %8.3f %8.3f %9.3f\n", indentedName, Min(block->intervalCount_, (unsigned)99999),
                     avg, max, frame, all);
                     avg, max, frame, all);
             }
             }
             else
             else
@@ -142,7 +142,7 @@ void Profiler::PrintData(ProfilerBlock* block, String& output, unsigned depth, u
                 float totalMax = block->totalMaxTime_ / 1000.0f;
                 float totalMax = block->totalMaxTime_ / 1000.0f;
                 float totalAll = block->totalTime_ / 1000.0f;
                 float totalAll = block->totalTime_ / 1000.0f;
 
 
-                sprintf(line, "%s %5u %8.3f %8.3f %9.3f  %7u %9.3f %9.3f %11.3f\n", indentedName, Min(block->frameCount_, 99999),
+                sprintf(line, "%s %5u %8.3f %8.3f %9.3f  %7u %9.3f %9.3f %11.3f\n", indentedName, Min(block->frameCount_, (unsigned)99999),
                     avg, max, all, Min(block->totalCount_, 99999), totalAvg, totalMax, totalAll);
                     avg, max, all, Min(block->totalCount_, 99999), totalAvg, totalMax, totalAll);
             }
             }
 
 

+ 1 - 1
Source/Urho3D/Engine/Engine.cpp

@@ -672,7 +672,7 @@ void Engine::ApplyFrameLimit()
     if (!initialized_)
     if (!initialized_)
         return;
         return;
 
 
-    int maxFps = maxFps_;
+    unsigned maxFps = maxFps_;
     Input* input = GetSubsystem<Input>();
     Input* input = GetSubsystem<Input>();
     if (input && !input->HasFocus())
     if (input && !input->HasFocus())
         maxFps = Min(maxInactiveFps_, maxFps);
         maxFps = Min(maxInactiveFps_, maxFps);

+ 1 - 1
Source/Urho3D/Graphics/DecalSet.cpp

@@ -258,7 +258,7 @@ void DecalSet::SetMaterial(Material* material)
 void DecalSet::SetMaxVertices(unsigned num)
 void DecalSet::SetMaxVertices(unsigned num)
 {
 {
     // Never expand to 32 bit indices
     // Never expand to 32 bit indices
-    num = (unsigned)Clamp((int)num, MIN_VERTICES, MAX_VERTICES);
+    num = (unsigned)Clamp(num, MIN_VERTICES, MAX_VERTICES);
 
 
     if (num != maxVertices_)
     if (num != maxVertices_)
     {
     {

+ 1 - 1
Source/Urho3D/Graphics/ParticleEffect.cpp

@@ -435,7 +435,7 @@ void ParticleEffect::SetMaterial(Material* material)
 
 
 void ParticleEffect::SetNumParticles(unsigned num)
 void ParticleEffect::SetNumParticles(unsigned num)
 {
 {
-    numParticles_ = (unsigned)Max(0, num);
+    numParticles_ = Max(0, num);
 }
 }
 
 
 void ParticleEffect::SetUpdateInvisible(bool enable)
 void ParticleEffect::SetUpdateInvisible(bool enable)

+ 3 - 2
Source/Urho3D/Graphics/Terrain.cpp

@@ -47,6 +47,7 @@ namespace Urho3D
 extern const char* GEOMETRY_CATEGORY;
 extern const char* GEOMETRY_CATEGORY;
 
 
 static const Vector3 DEFAULT_SPACING(1.0f, 0.25f, 1.0f);
 static const Vector3 DEFAULT_SPACING(1.0f, 0.25f, 1.0f);
+static const unsigned MIN_LOD_LEVELS = 1;
 static const unsigned MAX_LOD_LEVELS = 4;
 static const unsigned MAX_LOD_LEVELS = 4;
 static const int DEFAULT_PATCH_SIZE = 32;
 static const int DEFAULT_PATCH_SIZE = 32;
 static const int MIN_PATCH_SIZE = 4;
 static const int MIN_PATCH_SIZE = 4;
@@ -193,7 +194,7 @@ void Terrain::SetSpacing(const Vector3& spacing)
 
 
 void Terrain::SetMaxLodLevels(unsigned levels)
 void Terrain::SetMaxLodLevels(unsigned levels)
 {
 {
-    levels = Clamp((int)levels, 1, MAX_LOD_LEVELS);
+    levels = Clamp(levels, MIN_LOD_LEVELS, MAX_LOD_LEVELS);
     if (levels != maxLodLevels_)
     if (levels != maxLodLevels_)
     {
     {
         maxLodLevels_ = levels;
         maxLodLevels_ = levels;
@@ -656,7 +657,7 @@ void Terrain::SetPatchSizeAttr(int value)
 
 
 void Terrain::SetMaxLodLevelsAttr(unsigned value)
 void Terrain::SetMaxLodLevelsAttr(unsigned value)
 {
 {
-    value = Clamp((int)value, 1, MAX_LOD_LEVELS);
+    value = Clamp(value, MIN_LOD_LEVELS, MAX_LOD_LEVELS);
     
     
     if (value != maxLodLevels_)
     if (value != maxLodLevels_)
     {
     {

+ 4 - 4
Source/Urho3D/Graphics/TerrainPatch.cpp

@@ -280,13 +280,13 @@ void TerrainPatch::OnWorldBoundingBoxUpdate()
 unsigned TerrainPatch::GetCorrectedLodLevel(unsigned lodLevel)
 unsigned TerrainPatch::GetCorrectedLodLevel(unsigned lodLevel)
 {
 {
     if (north_)
     if (north_)
-        lodLevel = (unsigned)Min((int)lodLevel, north_->GetLodLevel() + 1);
+        lodLevel = Min(lodLevel, north_->GetLodLevel() + 1);
     if (south_)
     if (south_)
-        lodLevel = (unsigned)Min((int)lodLevel, south_->GetLodLevel() + 1);
+        lodLevel = Min(lodLevel, south_->GetLodLevel() + 1);
     if (west_)
     if (west_)
-        lodLevel = (unsigned)Min((int)lodLevel, west_->GetLodLevel() + 1);
+        lodLevel = Min(lodLevel, west_->GetLodLevel() + 1);
     if (east_)
     if (east_)
-        lodLevel = (unsigned)Min((int)lodLevel, east_->GetLodLevel() + 1);
+        lodLevel = Min(lodLevel, east_->GetLodLevel() + 1);
 
 
     return lodLevel;
     return lodLevel;
 }
 }

+ 1 - 1
Source/Urho3D/IO/File.cpp

@@ -400,7 +400,7 @@ unsigned File::Seek(unsigned position)
         {
         {
             unsigned char skipBuffer[SKIP_BUFFER_SIZE];
             unsigned char skipBuffer[SKIP_BUFFER_SIZE];
             while (position > position_)
             while (position > position_)
-                Read(skipBuffer, (unsigned)Min((int)position - position_, (int)SKIP_BUFFER_SIZE));
+                Read(skipBuffer, Min(position - position_, SKIP_BUFFER_SIZE));
         }
         }
         else
         else
             URHO3D_LOGERROR("Seeking backward in a compressed file is not supported");
             URHO3D_LOGERROR("Seeking backward in a compressed file is not supported");

+ 18 - 7
Source/Urho3D/Math/MathDefs.h

@@ -22,6 +22,13 @@
 
 
 #pragma once
 #pragma once
 
 
+#ifdef _MSC_VER
+#pragma warning(push)
+#pragma warning(disable:4018) // Signed/unsigned mismatch
+#pragma warning(disable:4244) // Conversion from 'double' to 'float'
+#pragma warning(disable:4702) // unreachable code
+#endif
+
 #include "../Math/Random.h"
 #include "../Math/Random.h"
 
 
 #include <cstdlib>
 #include <cstdlib>
@@ -62,16 +69,16 @@ template <class T>
 inline bool Equals(T lhs, T rhs) { return lhs + std::numeric_limits<T>::epsilon() >= rhs && lhs - std::numeric_limits<T>::epsilon() <= rhs; }
 inline bool Equals(T lhs, T rhs) { return lhs + std::numeric_limits<T>::epsilon() >= rhs && lhs - std::numeric_limits<T>::epsilon() <= rhs; }
 
 
 /// Linear interpolation between two values.
 /// Linear interpolation between two values.
-template <class T>
-inline T Lerp(T lhs, T rhs, T t) { return lhs * (1.0 - t) + rhs * t; }
+template <class T, class U>
+inline T Lerp(T lhs, T rhs, U t) { return lhs * (1.0 - t) + rhs * t; }
 
 
 /// Return the smaller of two values.
 /// Return the smaller of two values.
-template <class T>
-inline T Min(T lhs, T rhs) { return lhs < rhs ? lhs : rhs; }
+template <class T, class U>
+inline T Min(T lhs, U rhs) { return lhs < rhs ? lhs : rhs; }
 
 
 /// Return the larger of two values.
 /// Return the larger of two values.
-template <class T>
-inline T Max(T lhs, T rhs) { return lhs > rhs ? lhs : rhs; }
+template <class T, class U>
+inline T Max(T lhs, U rhs) { return lhs > rhs ? lhs : rhs; }
 
 
 /// Return absolute value of a value
 /// Return absolute value of a value
 template <class T>
 template <class T>
@@ -95,7 +102,7 @@ inline bool IsNaN(float value)
 
 
 #endif
 #endif
 
 
-/// Clamp a float to a range.
+/// Clamp a number to a range.
 template <class T>
 template <class T>
 inline T Clamp(T value, T min, T max)
 inline T Clamp(T value, T min, T max)
 {
 {
@@ -251,3 +258,7 @@ inline float HalfToFloat(unsigned short value)
 }
 }
 
 
 }
 }
+
+#ifdef _MSC_VER
+#pragma warning(pop)
+#endif

+ 1 - 1
Source/Urho3D/Navigation/CrowdManager.cpp

@@ -89,7 +89,7 @@ void CrowdManager::RegisterObject(Context* context)
 void CrowdManager::ApplyAttributes()
 void CrowdManager::ApplyAttributes()
 {
 {
     // Values from Editor, saved-file, or network must be checked before applying
     // Values from Editor, saved-file, or network must be checked before applying
-    maxAgents_ = (unsigned)Max(1, maxAgents_);
+    maxAgents_ = Max(1, maxAgents_);
     maxAgentRadius_ = Max(0.f, maxAgentRadius_);
     maxAgentRadius_ = Max(0.f, maxAgentRadius_);
 
 
     bool navMeshChange = false;
     bool navMeshChange = false;

+ 1 - 1
Source/Urho3D/Navigation/NavigationMesh.cpp

@@ -529,7 +529,7 @@ void NavigationMesh::FindPath(PODVector<Vector3>& dest, const Vector3& start, co
     FindPath(navPathPoints, start, end, extents, filter);
     FindPath(navPathPoints, start, end, extents, filter);
 
 
     dest.Clear();
     dest.Clear();
-    for (int i = 0; i < navPathPoints.Size(); ++i)
+    for (unsigned i = 0; i < navPathPoints.Size(); ++i)
         dest.Push(navPathPoints[i].position_);
         dest.Push(navPathPoints[i].position_);
 }
 }
 
 

+ 1 - 1
Source/Urho3D/UI/Slider.cpp

@@ -271,7 +271,7 @@ void Slider::Page(const IntVector2& position, bool pressed)
 
 
     // Start transmitting repeated pages after the initial press
     // Start transmitting repeated pages after the initial press
     if (selected_ && pressed && repeatRate_ > 0.0f &&
     if (selected_ && pressed && repeatRate_ > 0.0f &&
-        repeatTimer_.GetMSec(false) >= Lerp(1000.0f / repeatRate_, 0, Abs(offset) / length))
+        repeatTimer_.GetMSec(false) >= Lerp(1000.0f / repeatRate_, 0.0f, Abs(offset) / length))
         repeatTimer_.Reset();
         repeatTimer_.Reset();
     else
     else
         pressed = false;
         pressed = false;

+ 1 - 1
Source/Urho3D/Urho2D/ParticleEmitter2D.cpp

@@ -128,7 +128,7 @@ void ParticleEmitter2D::SetBlendMode(BlendMode blendMode)
 
 
 void ParticleEmitter2D::SetMaxParticles(unsigned maxParticles)
 void ParticleEmitter2D::SetMaxParticles(unsigned maxParticles)
 {
 {
-    maxParticles = (unsigned)Max(maxParticles, 1);
+    maxParticles = Max(maxParticles, 1);
 
 
     particles_.Resize(maxParticles);
     particles_.Resize(maxParticles);
     sourceBatches_[0].vertices_.Reserve(maxParticles * 4);
     sourceBatches_[0].vertices_.Reserve(maxParticles * 4);