Commit 77d97b18 authored by rogerborg's avatar rogerborg

Make position2d a typedef of vector2d. Very tentatively deprecate position2d....

Make position2d a typedef of vector2d.  Very tentatively deprecate position2d.  Add the position2d methods that operate on a dimension2d to vector2d.  Add vector2d constructor and equality methods to dimension2d.  Add a new test case that verifies that vector2d and position2d are interchangeable, and that vector2d and dimension2d can operate on and implicitly convert each other.  Remove the now duplicate position2d methods from CAttributeImpl.  Tested with unit/regression tests and building all the examples.  This should be a transparent change for users.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@2000 dfc29bdd-3216-0410-991c-e03cc46cb475
parent ea44396c
...@@ -12,6 +12,8 @@ namespace irr ...@@ -12,6 +12,8 @@ namespace irr
{ {
namespace core namespace core
{ {
template <class T>
class vector2d;
//! Specifies a 2 dimensional size. //! Specifies a 2 dimensional size.
template <class T> template <class T>
...@@ -24,6 +26,8 @@ namespace core ...@@ -24,6 +26,8 @@ namespace core
dimension2d(const T& width, const T& height) dimension2d(const T& width, const T& height)
: Width(width), Height(height) {} : Width(width), Height(height) {}
dimension2d(const vector2d<T>& other); // Defined in vector2d.h
//! Equality operator //! Equality operator
bool operator==(const dimension2d<T>& other) const bool operator==(const dimension2d<T>& other) const
{ {
...@@ -37,6 +41,12 @@ namespace core ...@@ -37,6 +41,12 @@ namespace core
return ! (*this == other); return ! (*this == other);
} }
bool operator==(const vector2d<T>& other) const; // Defined in vector2d.h
bool operator!=(const vector2d<T>& other) const
{
return !(*this == other);
}
//! Set to new values //! Set to new values
dimension2d<T>& set(const T& width, const T& height) dimension2d<T>& set(const T& width, const T& height)
......
...@@ -5,116 +5,22 @@ ...@@ -5,116 +5,22 @@
#ifndef __IRR_POSITION_H_INCLUDED__ #ifndef __IRR_POSITION_H_INCLUDED__
#define __IRR_POSITION_H_INCLUDED__ #define __IRR_POSITION_H_INCLUDED__
#include "irrTypes.h" // position2d is deprecated. It is currently defined as a vector2d, but
#include "dimension2d.h" // vector2d should be used directly.
#include "vector2d.h"
namespace irr namespace irr
{ {
namespace core namespace core
{ {
// Use explicit typedefs where possible
typedef vector2d<f32> position2df;
//! Simple class for holding 2d coordinates. typedef vector2d<s32> position2di;
/** Not supposed for doing geometric calculations. }; // namespace core
use vector2d instead for things like that. }; // namespace irr
*/
template <class T>
class position2d
{
public:
//! Default constructor for (0,0).
position2d() : X(0), Y(0) {}
position2d(T x, T y) : X(x), Y(y) {}
position2d(const position2d<T>& other)
: X(other.X), Y(other.Y) {}
bool operator == (const position2d<T>& other) const // And a #define to catch the rest
{ #define position2d vector2d
return X == other.X && Y == other.Y;
}
bool operator != (const position2d<T>& other) const
{
return X != other.X || Y != other.Y;
}
const position2d<T>& operator+=(const position2d<T>& other)
{
X += other.X;
Y += other.Y;
return *this;
}
const position2d<T>& operator-=(const position2d<T>& other)
{
X -= other.X;
Y -= other.Y;
return *this;
}
const position2d<T>& operator+=(const dimension2d<T>& other)
{
X += other.Width;
Y += other.Height;
return *this;
}
const position2d<T>& operator-=(const dimension2d<T>& other)
{
X -= other.Width;
Y -= other.Height;
return *this;
}
position2d<T> operator-(const position2d<T>& other) const
{
return position2d<T>(X-other.X, Y-other.Y);
}
position2d<T> operator+(const position2d<T>& other) const
{
return position2d<T>(X+other.X, Y+other.Y);
}
position2d<T> operator*(const position2d<T>& other) const
{
return position2d<T>(X*other.X, Y*other.Y);
}
position2d<T> operator*(const T& scalar) const
{
return position2d<T>(X*scalar, Y*scalar);
}
position2d<T> operator+(const dimension2d<T>& other) const
{
return position2d<T>(X+other.Width, Y+other.Height);
}
position2d<T> operator-(const dimension2d<T>& other) const
{
return position2d<T>(X-other.Width, Y-other.Height);
}
const position2d<T>& operator=(const position2d<T>& other)
{
X = other.X;
Y = other.Y;
return *this;
}
//! X coordinate of the position.
T X;
//! Y coordinate of the position.
T Y;
};
//! Typedef for an f32 position.
typedef position2d<f32> position2df;
//! Typedef for an integer position.
typedef position2d<s32> position2di;
} // end namespace core
} // end namespace irr
#endif
#endif // __IRR_POSITION_H_INCLUDED__
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define __IRR_POINT_2D_H_INCLUDED__ #define __IRR_POINT_2D_H_INCLUDED__
#include "irrMath.h" #include "irrMath.h"
#include "dimension2d.h"
namespace irr namespace irr
{ {
...@@ -14,6 +15,8 @@ namespace core ...@@ -14,6 +15,8 @@ namespace core
//! 2d vector template class with lots of operators and methods. //! 2d vector template class with lots of operators and methods.
/** As of Irrlicht 1.6, this class supercedes position2d, which should
be considered deprecated. */
template <class T> template <class T>
class vector2d class vector2d
{ {
...@@ -27,21 +30,28 @@ public: ...@@ -27,21 +30,28 @@ public:
//! Copy constructor //! Copy constructor
vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {} vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}
vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}
// operators // operators
vector2d<T> operator-() const { return vector2d<T>(-X, -Y); } vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }
vector2d<T>& operator=(const vector2d<T>& other) { X = other.X; Y = other.Y; return *this; } vector2d<T>& operator=(const vector2d<T>& other) { X = other.X; Y = other.Y; return *this; }
vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Width; return *this; }
vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); } vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }
vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }
vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; } vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }
vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); } vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }
vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; } vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }
vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; }
vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); } vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }
vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }
vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; } vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }
vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); } vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }
vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; } vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }
vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this; }
vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); } vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }
vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; } vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }
...@@ -289,6 +299,13 @@ public: ...@@ -289,6 +299,13 @@ public:
template<class S, class T> template<class S, class T>
vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; } vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
// These methods are declared in dimension2d, but need definitions of vector2d
template<class T>
dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
template<class T>
bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
} // end namespace core } // end namespace core
} // end namespace irr } // end namespace irr
......
...@@ -242,22 +242,6 @@ public: ...@@ -242,22 +242,6 @@ public:
ValueF.push_back(value.Z); ValueF.push_back(value.Z);
} }
CNumbersAttribute(const char* name, core::position2df value) :
ValueI(), ValueF(), Count(2), IsFloat(true)
{
Name = name;
ValueF.push_back(value.X);
ValueF.push_back(value.Y);
}
CNumbersAttribute(const char* name, core::position2di value) :
ValueI(), ValueF(), Count(2), IsFloat(false)
{
Name = name;
ValueI.push_back(value.X);
ValueI.push_back(value.Y);
}
CNumbersAttribute(const char* name, core::rect<s32> value) : CNumbersAttribute(const char* name, core::rect<s32> value) :
ValueI(), ValueF(), Count(4), IsFloat(false) ValueI(), ValueF(), Count(4), IsFloat(false)
{ {
......
...@@ -48,6 +48,7 @@ int main(int argumentCount, char * arguments[]) ...@@ -48,6 +48,7 @@ int main(int argumentCount, char * arguments[])
// Note that to interactively debug a test, you will generally want to move it // Note that to interactively debug a test, you will generally want to move it
// (temporarily) to the beginning of the list, since each test runs in its own // (temporarily) to the beginning of the list, since each test runs in its own
// process. // process.
TEST(vectorPositionDimension2d);
TEST(disambiguateTextures); // Normally you should run this first, since it validates the working directory. TEST(disambiguateTextures); // Normally you should run this first, since it validates the working directory.
TEST(irrCoreEquals); TEST(irrCoreEquals);
TEST(sceneNodeAnimator); TEST(sceneNodeAnimator);
......
Test suite pass at GMT Mon Dec 29 13:31:01 2008 Test suite pass at GMT Mon Dec 29 16:01:24 2008
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
<Unit filename="testUtils.cpp" /> <Unit filename="testUtils.cpp" />
<Unit filename="testVector2d.cpp" /> <Unit filename="testVector2d.cpp" />
<Unit filename="testVector3d.cpp" /> <Unit filename="testVector3d.cpp" />
<Unit filename="vectorPositionDimension2d.cpp" />
<Extensions> <Extensions>
<code_completion /> <code_completion />
<debugger /> <debugger />
......
...@@ -245,6 +245,10 @@ ...@@ -245,6 +245,10 @@
RelativePath=".\testVector3d.cpp" RelativePath=".\testVector3d.cpp"
> >
</File> </File>
<File
RelativePath=".\vectorPositionDimension2d.cpp"
>
</File>
</Filter> </Filter>
<Filter <Filter
Name="Header Files" Name="Header Files"
......
...@@ -242,6 +242,10 @@ ...@@ -242,6 +242,10 @@
RelativePath=".\testVector3d.cpp" RelativePath=".\testVector3d.cpp"
> >
</File> </File>
<File
RelativePath=".\vectorPositionDimension2d.cpp"
>
</File>
</Filter> </Filter>
<Filter <Filter
Name="Header Files" Name="Header Files"
......
// Copyright (C) 2008 Colin MacDonald
// No rights reserved: this software is in the public domain.
/** This test verifies that position2d and vector2d are interchangeable,
and that they can convert from dimension2d */
#include "testUtils.h"
#include "irrlicht.h"
#include <assert.h>
using namespace irr;
using namespace core;
template <class DIMENSION, class VECTOR, class POSITION, class T>
static bool doTest(void)
{
bool result = true;
DIMENSION dimension((T)99.9, (T)99.9);
VECTOR vector(dimension);
POSITION position(vector);
DIMENSION dimension2(vector);
result &= (vector == position);
result &= (vector == dimension); // The conversion should be explicit.
result &= (dimension2 == position);
result &= (position == POSITION((T)99.9, (T)99.9));
dimension = (T)2 * position;
result &= (dimension == VECTOR(2 * (T)99.9, 2 * (T)99.9));
dimension /= (T)2;
result &= (dimension == POSITION((T)99.9, (T)99.9));
dimension += vector;
result &= (dimension == VECTOR(2 * (T)99.9, 2 * (T)99.9));
dimension -= position;
result &= (dimension == POSITION((T)99.9, (T)99.9));
position = dimension;
result &= (position == VECTOR((T)99.9, (T)99.9));
vector += position;
result &= (vector == POSITION(2 * (T)99.9, 2 * (T)99.9));
vector -= position;
result &= (vector == dimension);
position *= (T)3.5;
result &= (position == VECTOR((T)3.5 * (T)99.9, (T)3.5 * (T)99.9));
vector += dimension;
result &= (vector == VECTOR(2 * (T)99.9, 2 * (T)99.9));
return result;
}
bool vectorPositionDimension2d(void)
{
bool result = true;
result &= doTest<dimension2di, vector2di, position2di, s32>();
result &= doTest<dimension2df, vector2df, position2df, f32>();
result &= doTest<dimension2d<f64>, vector2d<f64>, position2d<f64>, f64>();
if(!result)
assert(false);
return result;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment