Selaa lähdekoodia

Add explicit Vector2 & Vector3 constructors taking IntVector2 & IntVector3. Add Graphics::GetSize(). Closes #1814.

Lasse Öörni 8 vuotta sitten
vanhempi
sitoutus
26574dcc27

+ 1 - 0
Source/Urho3D/AngelScript/GraphicsAPI.cpp

@@ -1888,6 +1888,7 @@ static void RegisterGraphics(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Graphics", "int get_width() const", asMETHOD(Graphics, GetWidth), asCALL_THISCALL);
     engine->RegisterObjectMethod("Graphics", "int get_height() const", asMETHOD(Graphics, GetHeight), asCALL_THISCALL);
     engine->RegisterObjectMethod("Graphics", "int get_multiSample() const", asMETHOD(Graphics, GetMultiSample), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Graphics", "IntVector2 get_size() const", asMETHOD(Graphics, GetSize), asCALL_THISCALL);
     engine->RegisterObjectMethod("Graphics", "bool get_fullscreen() const", asMETHOD(Graphics, GetFullscreen), asCALL_THISCALL);
     engine->RegisterObjectMethod("Graphics", "bool get_resizable() const", asMETHOD(Graphics, GetResizable), asCALL_THISCALL);
     engine->RegisterObjectMethod("Graphics", "bool get_borderless() const", asMETHOD(Graphics, GetBorderless), asCALL_THISCALL);

+ 12 - 0
Source/Urho3D/AngelScript/MathAPI.cpp

@@ -260,6 +260,11 @@ static void ConstructVector2Copy(const Vector2& vector, Vector2* ptr)
     new(ptr) Vector2(vector);
 }
 
+static void ConstructVector2IntVector2(const IntVector2& vector, Vector2* ptr)
+{
+    new(ptr)Vector2(vector);
+}
+
 static void ConstructVector2Init(float x, float y, Vector2* ptr)
 {
     new(ptr) Vector2(x, y);
@@ -280,6 +285,7 @@ static void RegisterVector2(asIScriptEngine* engine)
     engine->RegisterObjectType("Vector2", sizeof(Vector2), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK);
     engine->RegisterObjectBehaviour("Vector2", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructVector2), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Vector2", asBEHAVE_CONSTRUCT, "void f(const Vector2&in)", asFUNCTION(ConstructVector2Copy), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectBehaviour("Vector2", asBEHAVE_CONSTRUCT, "void f(const IntVector2&in)", asFUNCTION(ConstructVector2IntVector2), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Vector2", asBEHAVE_CONSTRUCT, "void f(float, float)", asFUNCTION(ConstructVector2Init), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Vector2", asBEHAVE_CONSTRUCT, "void f(float[]&)", asFUNCTION(ConstructVector2ArrayInit), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("Vector2", "float[]& get_data() const", asFUNCTION(Vector2Data), asCALL_CDECL_OBJLAST);
@@ -347,6 +353,11 @@ static void ConstructVector3Vector2(const Vector2& vector, Vector3* ptr)
     new(ptr) Vector3(vector);
 }
 
+static void ConstructVector3IntVector3(const IntVector3& vector, Vector3* ptr)
+{
+    new(ptr)Vector3(vector);
+}
+
 static void ConstructVector3XYZ(float x, float y, float z, Vector3* ptr)
 {
     new(ptr) Vector3(x, y, z);
@@ -374,6 +385,7 @@ static void RegisterVector3(asIScriptEngine* engine)
     engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT, "void f(const Vector3&in)", asFUNCTION(ConstructVector3Copy), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT, "void f(const Vector2&in, float)", asFUNCTION(ConstructVector3Vector2Z), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT, "void f(const Vector2&in)", asFUNCTION(ConstructVector3Vector2), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT, "void f(const IntVector3&in)", asFUNCTION(ConstructVector3IntVector3), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT, "void f(float, float, float)", asFUNCTION(ConstructVector3XYZ), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT, "void f(float, float)", asFUNCTION(ConstructVector3XY), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("Vector3", asBEHAVE_CONSTRUCT, "void f(float[]&)", asFUNCTION(ConstructVector3ArrayInit), asCALL_CDECL_OBJLAST);

+ 3 - 0
Source/Urho3D/Graphics/Graphics.h

@@ -282,6 +282,9 @@ public:
     /// Return multisample mode (1 = no multisampling.)
     int GetMultiSample() const { return multiSample_; }
 
+    /// Return window size in pixels.
+    IntVector2 GetSize() const { return IntVector2(width_, height_); }
+
     /// Return whether window is fullscreen.
     bool GetFullscreen() const { return fullscreen_; }
 

+ 2 - 0
Source/Urho3D/LuaScript/pkgs/Graphics/Graphics.pkg

@@ -34,6 +34,7 @@ class Graphics : public Object
     int GetWidth() const;
     int GetHeight() const;
     int GetMultiSample() const;
+    IntVector2 GetSize() const;
     bool GetFullscreen() const;
     bool GetResizable() const;
     bool GetBorderless() const;
@@ -85,6 +86,7 @@ class Graphics : public Object
     tolua_readonly tolua_property__get_set int width;
     tolua_readonly tolua_property__get_set int height;
     tolua_readonly tolua_property__get_set int multiSample;
+    tolua_readonly tolua_property__get_set IntVector2 size;
     tolua_readonly tolua_property__get_set bool fullscreen;
     tolua_readonly tolua_property__get_set bool resizable;
     tolua_readonly tolua_property__get_set bool borderless;

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Math/Vector2.pkg

@@ -4,6 +4,7 @@ class Vector2
 {
     Vector2();
     Vector2(const Vector2& vector);
+    Vector2(const IntVector2& vector);
     Vector2(float x, float y);
     ~Vector2();
 

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Math/Vector3.pkg

@@ -6,6 +6,7 @@ class Vector3
     Vector3(const Vector3& vector);
     Vector3(const Vector2& vector, float z);
     Vector3(const Vector2& vector);
+    Vector3(const IntVector3& vector);
     Vector3(float x, float y, float z);
     Vector3(float x, float y);
     ~Vector3();

+ 121 - 114
Source/Urho3D/Math/Vector2.h

@@ -28,6 +28,120 @@
 namespace Urho3D
 {
 
+/// Two-dimensional vector with integer values.
+class URHO3D_API IntVector2
+{
+public:
+    /// Construct a zero vector.
+    IntVector2() :
+        x_(0),
+        y_(0)
+    {
+    }
+
+    /// Construct from coordinates.
+    IntVector2(int x, int y) :
+        x_(x),
+        y_(y)
+    {
+    }
+
+    /// Construct from an int array.
+    IntVector2(const int* data) :
+        x_(data[0]),
+        y_(data[1])
+    {
+    }
+
+    /// Copy-construct from another vector.
+    IntVector2(const IntVector2& rhs) :
+        x_(rhs.x_),
+        y_(rhs.y_)
+    {
+    }
+
+    /// Assign from another vector.
+    IntVector2& operator =(const IntVector2& rhs)
+    {
+        x_ = rhs.x_;
+        y_ = rhs.y_;
+        return *this;
+    }
+
+    /// Test for equality with another vector.
+    bool operator ==(const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
+
+    /// Test for inequality with another vector.
+    bool operator !=(const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
+
+    /// Add a vector.
+    IntVector2 operator +(const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
+
+    /// Return negation.
+    IntVector2 operator -() const { return IntVector2(-x_, -y_); }
+
+    /// Subtract a vector.
+    IntVector2 operator -(const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
+
+    /// Multiply with a scalar.
+    IntVector2 operator *(int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
+
+    /// Divide by a scalar.
+    IntVector2 operator /(int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
+
+    /// Add-assign a vector.
+    IntVector2& operator +=(const IntVector2& rhs)
+    {
+        x_ += rhs.x_;
+        y_ += rhs.y_;
+        return *this;
+    }
+
+    /// Subtract-assign a vector.
+    IntVector2& operator -=(const IntVector2& rhs)
+    {
+        x_ -= rhs.x_;
+        y_ -= rhs.y_;
+        return *this;
+    }
+
+    /// Multiply-assign a scalar.
+    IntVector2& operator *=(int rhs)
+    {
+        x_ *= rhs;
+        y_ *= rhs;
+        return *this;
+    }
+
+    /// Divide-assign a scalar.
+    IntVector2& operator /=(int rhs)
+    {
+        x_ /= rhs;
+        y_ /= rhs;
+        return *this;
+    }
+
+    /// Return integer data.
+    const int* Data() const { return &x_; }
+
+    /// Return as string.
+    String ToString() const;
+
+    /// Return hash value for HashSet & HashMap.
+    unsigned ToHash() const { return (unsigned)x_ * 31 + (unsigned)y_; }
+
+    /// Return length.
+    float Length() const { return sqrtf((float)(x_ * x_ + y_ * y_)); }
+
+    /// X coordinate.
+    int x_;
+    /// Y coordinate.
+    int y_;
+
+    /// Zero vector.
+    static const IntVector2 ZERO;
+};
+
 /// Two-dimensional vector.
 class URHO3D_API Vector2
 {
@@ -46,6 +160,13 @@ public:
     {
     }
 
+    /// Construct from an IntVector2.
+    explicit Vector2(const IntVector2& vector) :
+        x_((float)vector.x_),
+        y_((float)vector.y_)
+    {
+    }
+
     /// Construct from coordinates.
     Vector2(float x, float y) :
         x_(x),
@@ -227,120 +348,6 @@ public:
 /// Multiply Vector2 with a scalar
 inline Vector2 operator *(float lhs, const Vector2& rhs) { return rhs * lhs; }
 
-/// Two-dimensional vector with integer values.
-class URHO3D_API IntVector2
-{
-public:
-    /// Construct a zero vector.
-    IntVector2() :
-        x_(0),
-        y_(0)
-    {
-    }
-
-    /// Construct from coordinates.
-    IntVector2(int x, int y) :
-        x_(x),
-        y_(y)
-    {
-    }
-
-    /// Construct from an int array.
-    IntVector2(const int* data) :
-        x_(data[0]),
-        y_(data[1])
-    {
-    }
-
-    /// Copy-construct from another vector.
-    IntVector2(const IntVector2& rhs) :
-        x_(rhs.x_),
-        y_(rhs.y_)
-    {
-    }
-
-    /// Assign from another vector.
-    IntVector2& operator =(const IntVector2& rhs)
-    {
-        x_ = rhs.x_;
-        y_ = rhs.y_;
-        return *this;
-    }
-
-    /// Test for equality with another vector.
-    bool operator ==(const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
-
-    /// Test for inequality with another vector.
-    bool operator !=(const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
-
-    /// Add a vector.
-    IntVector2 operator +(const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
-
-    /// Return negation.
-    IntVector2 operator -() const { return IntVector2(-x_, -y_); }
-
-    /// Subtract a vector.
-    IntVector2 operator -(const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
-
-    /// Multiply with a scalar.
-    IntVector2 operator *(int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
-
-    /// Divide by a scalar.
-    IntVector2 operator /(int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
-
-    /// Add-assign a vector.
-    IntVector2& operator +=(const IntVector2& rhs)
-    {
-        x_ += rhs.x_;
-        y_ += rhs.y_;
-        return *this;
-    }
-
-    /// Subtract-assign a vector.
-    IntVector2& operator -=(const IntVector2& rhs)
-    {
-        x_ -= rhs.x_;
-        y_ -= rhs.y_;
-        return *this;
-    }
-
-    /// Multiply-assign a scalar.
-    IntVector2& operator *=(int rhs)
-    {
-        x_ *= rhs;
-        y_ *= rhs;
-        return *this;
-    }
-
-    /// Divide-assign a scalar.
-    IntVector2& operator /=(int rhs)
-    {
-        x_ /= rhs;
-        y_ /= rhs;
-        return *this;
-    }
-
-    /// Return integer data.
-    const int* Data() const { return &x_; }
-
-    /// Return as string.
-    String ToString() const;
-
-    /// Return hash value for HashSet & HashMap.
-    unsigned ToHash() const { return (unsigned)x_ * 31 + (unsigned)y_; }
-
-    /// Return length.
-    float Length() const { return sqrtf((float)(x_ * x_ + y_ * y_)); }
-
-    /// X coordinate.
-    int x_;
-    /// Y coordinate.
-    int y_;
-
-    /// Zero vector.
-    static const IntVector2 ZERO;
-};
-
 /// Multiply IntVector2 with a scalar.
 inline IntVector2 operator *(int lhs, const IntVector2& rhs) { return rhs * lhs; }
 

+ 147 - 139
Source/Urho3D/Math/Vector3.h

@@ -27,6 +27,145 @@
 namespace Urho3D
 {
 
+/// Three-dimensional vector with integer values.
+class URHO3D_API IntVector3
+{
+public:
+    /// Construct a zero vector.
+    IntVector3() :
+        x_(0),
+        y_(0),
+        z_(0)
+    {
+    }
+
+    /// Construct from coordinates.
+    IntVector3(int x, int y, int z) :
+        x_(x),
+        y_(y),
+        z_(z)
+    {
+    }
+
+    /// Construct from an int array.
+    IntVector3(const int* data) :
+        x_(data[0]),
+        y_(data[1]),
+        z_(data[2])
+    {
+    }
+
+    /// Copy-construct from another vector.
+    IntVector3(const IntVector3& rhs) :
+        x_(rhs.x_),
+        y_(rhs.y_),
+        z_(rhs.z_)
+    {
+    }
+
+    /// Assign from another vector.
+    IntVector3& operator =(const IntVector3& rhs)
+    {
+        x_ = rhs.x_;
+        y_ = rhs.y_;
+        z_ = rhs.z_;
+        return *this;
+    }
+
+    /// Test for equality with another vector.
+    bool operator ==(const IntVector3& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
+
+    /// Test for inequality with another vector.
+    bool operator !=(const IntVector3& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
+
+    /// Add a vector.
+    IntVector3 operator +(const IntVector3& rhs) const { return IntVector3(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
+
+    /// Return negation.
+    IntVector3 operator -() const { return IntVector3(-x_, -y_, -z_); }
+
+    /// Subtract a vector.
+    IntVector3 operator -(const IntVector3& rhs) const { return IntVector3(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
+
+    /// Multiply with a scalar.
+    IntVector3 operator *(int rhs) const { return IntVector3(x_ * rhs, y_ * rhs, z_ * rhs); }
+
+    /// Divide by a scalar.
+    IntVector3 operator /(int rhs) const { return IntVector3(x_ / rhs, y_ / rhs, z_ / rhs); }
+
+    /// Add-assign a vector.
+    IntVector3& operator +=(const IntVector3& rhs)
+    {
+        x_ += rhs.x_;
+        y_ += rhs.y_;
+        z_ += rhs.z_;
+        return *this;
+    }
+
+    /// Subtract-assign a vector.
+    IntVector3& operator -=(const IntVector3& rhs)
+    {
+        x_ -= rhs.x_;
+        y_ -= rhs.y_;
+        z_ -= rhs.z_;
+        return *this;
+    }
+
+    /// Multiply-assign a scalar.
+    IntVector3& operator *=(int rhs)
+    {
+        x_ *= rhs;
+        y_ *= rhs;
+        z_ *= rhs;
+        return *this;
+    }
+
+    /// Divide-assign a scalar.
+    IntVector3& operator /=(int rhs)
+    {
+        x_ /= rhs;
+        y_ /= rhs;
+        z_ /= rhs;
+        return *this;
+    }
+
+    /// Return integer data.
+    const int* Data() const { return &x_; }
+
+    /// Return as string.
+    String ToString() const;
+
+    /// Return hash value for HashSet & HashMap.
+    unsigned ToHash() const { return (unsigned)x_ * 31 * 31 + (unsigned)y_ * 31 + (unsigned)z_; }
+
+    /// Return length.
+    float Length() const { return sqrtf((float)(x_ * x_ + y_ * y_ + z_ * z_)); }
+
+    /// X coordinate.
+    int x_;
+    /// Y coordinate.
+    int y_;
+    /// Z coordinate.
+    int z_;
+
+    /// Zero vector.
+    static const IntVector3 ZERO;
+    /// (-1,0,0) vector.
+    static const IntVector3 LEFT;
+    /// (1,0,0) vector.
+    static const IntVector3 RIGHT;
+    /// (0,1,0) vector.
+    static const IntVector3 UP;
+    /// (0,-1,0) vector.
+    static const IntVector3 DOWN;
+    /// (0,0,1) vector.
+    static const IntVector3 FORWARD;
+    /// (0,0,-1) vector.
+    static const IntVector3 BACK;
+    /// (1,1,1) vector.
+    static const IntVector3 ONE;
+};
+
 /// Three-dimensional vector.
 class URHO3D_API Vector3
 {
@@ -63,6 +202,14 @@ public:
     {
     }
 
+    /// Construct from an IntVector3.
+    explicit Vector3(const IntVector3& vector) :
+        x_((float)vector.x_),
+        y_((float)vector.y_),
+        z_((float)vector.z_)
+    {
+    }
+
     /// Construct from coordinates.
     Vector3(float x, float y, float z) :
         x_(x),
@@ -284,145 +431,6 @@ public:
 /// Multiply Vector3 with a scalar.
 inline Vector3 operator *(float lhs, const Vector3& rhs) { return rhs * lhs; }
 
-/// Three-dimensional vector with integer values.
-class URHO3D_API IntVector3
-{
-public:
-    /// Construct a zero vector.
-    IntVector3() :
-        x_(0),
-        y_(0),
-        z_(0)
-    {
-    }
-
-    /// Construct from coordinates.
-    IntVector3(int x, int y, int z) :
-        x_(x),
-        y_(y),
-        z_(z)
-    {
-    }
-
-    /// Construct from an int array.
-    IntVector3(const int* data) :
-        x_(data[0]),
-        y_(data[1]),
-        z_(data[2])
-    {
-    }
-
-    /// Copy-construct from another vector.
-    IntVector3(const IntVector3& rhs) :
-        x_(rhs.x_),
-        y_(rhs.y_),
-        z_(rhs.z_)
-    {
-    }
-
-    /// Assign from another vector.
-    IntVector3& operator =(const IntVector3& rhs)
-    {
-        x_ = rhs.x_;
-        y_ = rhs.y_;
-        z_ = rhs.z_;
-        return *this;
-    }
-
-    /// Test for equality with another vector.
-    bool operator ==(const IntVector3& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_ && z_ == rhs.z_; }
-
-    /// Test for inequality with another vector.
-    bool operator !=(const IntVector3& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_ || z_ != rhs.z_; }
-
-    /// Add a vector.
-    IntVector3 operator +(const IntVector3& rhs) const { return IntVector3(x_ + rhs.x_, y_ + rhs.y_, z_ + rhs.z_); }
-
-    /// Return negation.
-    IntVector3 operator -() const { return IntVector3(-x_, -y_, -z_); }
-
-    /// Subtract a vector.
-    IntVector3 operator -(const IntVector3& rhs) const { return IntVector3(x_ - rhs.x_, y_ - rhs.y_, z_ - rhs.z_); }
-
-    /// Multiply with a scalar.
-    IntVector3 operator *(int rhs) const { return IntVector3(x_ * rhs, y_ * rhs, z_ * rhs); }
-
-    /// Divide by a scalar.
-    IntVector3 operator /(int rhs) const { return IntVector3(x_ / rhs, y_ / rhs, z_ / rhs); }
-
-    /// Add-assign a vector.
-    IntVector3& operator +=(const IntVector3& rhs)
-    {
-        x_ += rhs.x_;
-        y_ += rhs.y_;
-        z_ += rhs.z_;
-        return *this;
-    }
-
-    /// Subtract-assign a vector.
-    IntVector3& operator -=(const IntVector3& rhs)
-    {
-        x_ -= rhs.x_;
-        y_ -= rhs.y_;
-        z_ -= rhs.z_;
-        return *this;
-    }
-
-    /// Multiply-assign a scalar.
-    IntVector3& operator *=(int rhs)
-    {
-        x_ *= rhs;
-        y_ *= rhs;
-        z_ *= rhs;
-        return *this;
-    }
-
-    /// Divide-assign a scalar.
-    IntVector3& operator /=(int rhs)
-    {
-        x_ /= rhs;
-        y_ /= rhs;
-        z_ /= rhs;
-        return *this;
-    }
-
-    /// Return integer data.
-    const int* Data() const { return &x_; }
-
-    /// Return as string.
-    String ToString() const;
-
-    /// Return hash value for HashSet & HashMap.
-    unsigned ToHash() const { return (unsigned)x_ * 31 * 31 + (unsigned)y_ * 31 + (unsigned)z_; }
-
-    /// Return length.
-    float Length() const { return sqrtf((float)(x_ * x_ + y_ * y_ + z_ * z_)); }
-
-    /// X coordinate.
-    int x_;
-    /// Y coordinate.
-    int y_;
-    /// Z coordinate.
-    int z_;
-
-    /// Zero vector.
-    static const IntVector3 ZERO;
-    /// (-1,0,0) vector.
-    static const IntVector3 LEFT;
-    /// (1,0,0) vector.
-    static const IntVector3 RIGHT;
-    /// (0,1,0) vector.
-    static const IntVector3 UP;
-    /// (0,-1,0) vector.
-    static const IntVector3 DOWN;
-    /// (0,0,1) vector.
-    static const IntVector3 FORWARD;
-    /// (0,0,-1) vector.
-    static const IntVector3 BACK;
-    /// (1,1,1) vector.
-    static const IntVector3 ONE;
-};
-
 /// Multiply IntVector3 with a scalar.
 inline IntVector3 operator *(int lhs, const IntVector3& rhs) { return rhs * lhs; }