* Vector2 now uses the new operator overloading
* sync: Rect changes to width/height style git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1515 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
parent
eada18b7e5
commit
b4ff671e6e
@ -51,17 +51,17 @@ else
|
||||
}
|
||||
|
||||
/**
|
||||
* Rect is an utility class for manipulating rectangles.
|
||||
* Template parameter defines the type of coordinates (integer float, ...)
|
||||
*/
|
||||
* Rect is an utility class for manipulating rectangles.
|
||||
* Template parameter defines the type of coordinates (integer float, ...)
|
||||
*/
|
||||
struct Rect(T)
|
||||
{
|
||||
|
||||
private:
|
||||
T left; // Left coordinate of the rectangle
|
||||
T top; // Top coordinate of the rectangle
|
||||
T right; // Right coordinate of the rectangle
|
||||
T bottom; // Bottom coordinate of the rectangle
|
||||
T width; // width
|
||||
T height; // height
|
||||
|
||||
public:
|
||||
static if (!isIntegerType!(T) && !isRealType!(T))
|
||||
@ -79,87 +79,47 @@ public:
|
||||
return i > j ? i : j;
|
||||
}
|
||||
|
||||
/+
|
||||
/**
|
||||
* Construct the rectangle from its coordinates
|
||||
*
|
||||
* Params:
|
||||
* leftCoord = Left coordinate of the rectangle
|
||||
* topCoord = Top coordinate of the rectangle
|
||||
* rightCoord = Right coordinate of the rectangle
|
||||
* bottomCoord = Bottom coordinate of the rectangle
|
||||
*/
|
||||
this(T leftCoord, T topCoord, T rightCoord, T bottomCoord)
|
||||
* Get the right coordinate of the rectangle
|
||||
*/
|
||||
T right()
|
||||
{
|
||||
left = leftCoord;
|
||||
top = topCoord;
|
||||
right = rightCoord;
|
||||
bottom = bottomCoord;
|
||||
}
|
||||
+/
|
||||
|
||||
/**
|
||||
* Get the width of the rectangle
|
||||
*
|
||||
* Returns:
|
||||
* Width of rectangle
|
||||
*/
|
||||
T getWidth()
|
||||
{
|
||||
return right - left;
|
||||
return left + width;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the height of the rectangle
|
||||
*
|
||||
* Returns:
|
||||
* Height of rectangle
|
||||
*/
|
||||
T getHeight()
|
||||
* Get the bottom coordinate of the rectangle
|
||||
*/
|
||||
T bottom()
|
||||
{
|
||||
return bottom - top;
|
||||
return top + height;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move the whole rectangle by the given offset
|
||||
*
|
||||
* Params:
|
||||
* offsetX = Horizontal offset
|
||||
* offsetY = Vertical offset
|
||||
*/
|
||||
void offset(T offsetX, T offsetY)
|
||||
{
|
||||
left += offsetX;
|
||||
right += offsetX;
|
||||
top += offsetY;
|
||||
bottom += offsetY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a point is inside the rectangle's area
|
||||
*
|
||||
* Params:
|
||||
* x = X coordinate of the point to test
|
||||
* y = Y coordinate of the point to test
|
||||
*
|
||||
* Returns:
|
||||
* True if the point is inside
|
||||
*/
|
||||
* Check if a point is inside the rectangle's area
|
||||
*
|
||||
* Params:
|
||||
* x = X coordinate of the point to test
|
||||
* y = Y coordinate of the point to test
|
||||
*
|
||||
* Returns:
|
||||
* True if the point is inside
|
||||
*/
|
||||
bool contains(T x, T y)
|
||||
{
|
||||
return (x >= left) && (x <= right) && (y >= top) && (y <= bottom);
|
||||
return (x >= left) && (x < right) && (y >= top) && (y < bottom);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check intersection between two rectangles
|
||||
*
|
||||
* Params:
|
||||
* rectangle = Rectangle to test
|
||||
* overlappingRect = Rectangle to be filled with overlapping rect (NULL by default)
|
||||
*
|
||||
* Returns:
|
||||
* True if rectangles overlap
|
||||
*/
|
||||
* Check intersection between two rectangles
|
||||
*
|
||||
* Params:
|
||||
* rectangle = Rectangle to test
|
||||
* overlappingRect = Rectangle to be filled with overlapping rect (NULL by default)
|
||||
*
|
||||
* Returns:
|
||||
* True if rectangles overlap
|
||||
*/
|
||||
bool intersects(Rect!(T) rectangle, out Rect!(T) overlappingRect = Rect!(T)())
|
||||
{
|
||||
// Compute overlapping rect
|
||||
|
@ -245,10 +245,8 @@ public:
|
||||
* Create a shape made of a single rectangle
|
||||
*
|
||||
* Params:
|
||||
* p1X = X position of the first point
|
||||
* p1Y = Y position of the first point
|
||||
* p2X = X position second point
|
||||
* p2Y = Y position second point
|
||||
* left, top = Top-left corner of the rectangle
|
||||
* width, height = Size of the rectangle
|
||||
* col = Color used to fill the rectangle
|
||||
* outline = Outline width (0 by default)
|
||||
* outlineCol = Color used to draw the outline (black by default)
|
||||
@ -256,9 +254,9 @@ public:
|
||||
* Returns:
|
||||
* new rectangle shape
|
||||
*/
|
||||
static Shape rectangle(float p1X, float p1Y, float p2X, float p2Y, Color col, float outline = 0.f, Color outlineCol = Color.BLACK)
|
||||
static Shape rectangle(float left, float top, float width, float height, Color col, float outline = 0.f, Color outlineCol = Color.BLACK)
|
||||
{
|
||||
return new Shape(sfShape_CreateRectangle(p1X, p1Y, p2X, p2Y, col, outline, outlineCol));
|
||||
return new Shape(sfShape_CreateRectangle(left, top, width, height, col, outline, outlineCol));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,7 +62,7 @@ public:
|
||||
*/
|
||||
this(Vector2f center, Vector2f size)
|
||||
{
|
||||
super(sfView_CreateFromRect(FloatRect(center.x - size.x / 2, center.y - size.y / 2, center.x + size.x / 2, center.y + size.y / 2) ));
|
||||
super(sfView_CreateFromRect(FloatRect(center.x - size.x / 2, center.y - size.y / 2, size.x, size.y) ));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -27,74 +27,56 @@
|
||||
module dsfml.system.vector2;
|
||||
|
||||
/**
|
||||
* Vector2 is an utility class for manipulating 2 dimensional
|
||||
* vectors. Template parameter defines the type of coordinates
|
||||
* (integer, float, ...)
|
||||
*/
|
||||
* Vector2 is an utility class for manipulating 2 dimensional
|
||||
* vectors. Template parameter defines the type of coordinates
|
||||
* (integer, float, ...)
|
||||
*/
|
||||
struct Vector2(T)
|
||||
{
|
||||
T x;
|
||||
T y;
|
||||
|
||||
/// unary (-) overload
|
||||
Vector2 opNeg()
|
||||
/// negate the vector
|
||||
Vector2 opUnary(string op : "-")()
|
||||
{
|
||||
return Vector2!(T)(-x, -y);
|
||||
}
|
||||
|
||||
/// (+=) overload
|
||||
Vector2 opAddAssign(Vector2 other)
|
||||
/// dot product
|
||||
T opBinary(string op : "*", U:Vector2)(U v)
|
||||
{
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
return x*v.x + y*v.y;
|
||||
}
|
||||
|
||||
/// element-wise operations, +, -,
|
||||
Vector2 opBinary(string op, U:Vector2)(U v)
|
||||
if (op != "*")
|
||||
{
|
||||
// pragma(msg, "opBinary!"~op);
|
||||
mixin("return Vector2!(T)( cast(T)(x " ~ op ~ " v.x), cast(T)(y " ~ op ~ " v.y) );");
|
||||
}
|
||||
|
||||
/// operations with a scalar
|
||||
Vector2 opBinary(string op)(int i)
|
||||
{
|
||||
mixin("return Vector2!(T) ( cast(T)(x " ~ op ~ " i), cast(T)(y " ~ op ~ " i) );");
|
||||
}
|
||||
|
||||
/// element-wise assign operations, +=, -=, ...
|
||||
Vector2 opOpAssign(string op, U:Vector2)(U v)
|
||||
{
|
||||
mixin("x " ~ op ~ " v.x;");
|
||||
mixin("y " ~ op ~ " v.y;");
|
||||
return this;
|
||||
}
|
||||
|
||||
/// (-=) overload
|
||||
Vector2 opSubAssign(Vector2 other)
|
||||
{
|
||||
x -= other.x;
|
||||
y -= other.y;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// (+) overload
|
||||
Vector2 opAdd(Vector2 other)
|
||||
{
|
||||
return Vector2!(T)( cast(T)(x + other.x), cast(T)(y + other.y) );
|
||||
}
|
||||
|
||||
/// (-) overload
|
||||
Vector2 opSub(Vector2 other)
|
||||
{
|
||||
return Vector2!(T) ( cast(T)(x - other.x), cast(T)(y - other.y) );
|
||||
}
|
||||
|
||||
/// (*) overload
|
||||
Vector2 opMul(int i)
|
||||
{
|
||||
return Vector2!(T) ( cast(T)(x * i), cast(T)(y * i) );
|
||||
}
|
||||
|
||||
/// (*=) overload
|
||||
Vector2 opMulAssign(int i)
|
||||
Vector2 opOpAssign(string op)(int i)
|
||||
{
|
||||
x *= i;
|
||||
y *= i;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// (/) overload
|
||||
Vector2 opDiv(int i)
|
||||
{
|
||||
return Vector2!(T) ( cast(T)(x / i), cast(T)(y / i));
|
||||
}
|
||||
|
||||
/// (/=) overload
|
||||
Vector2 opDivAssign(int i)
|
||||
{
|
||||
x /= i;
|
||||
y /= i;
|
||||
mixin("x "~op~" i;");
|
||||
mixin("y "~op~" i;");
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -110,44 +92,41 @@ struct Vector2(T)
|
||||
}
|
||||
}
|
||||
|
||||
version (UnitTest)
|
||||
unittest
|
||||
{
|
||||
unittest
|
||||
{
|
||||
Vector2f main = Vector2f(10f, 10f);
|
||||
Vector2f other = Vector2f(10f, 10f);
|
||||
Vector2f result;
|
||||
Vector2f main = Vector2f(10f, 10f);
|
||||
Vector2f other = Vector2f(10f, 10f);
|
||||
Vector2f result;
|
||||
|
||||
result = -main;
|
||||
assert (result == Vector2f(-10.f, -10.f) );
|
||||
result = -main;
|
||||
assert (result == Vector2f(-10.f, -10.f) );
|
||||
|
||||
result = main;
|
||||
result += other;
|
||||
assert (result == Vector2f(20.f, 20.f));
|
||||
result = main;
|
||||
result += other;
|
||||
assert (result == Vector2f(20.f, 20.f));
|
||||
|
||||
result = main;
|
||||
result -= other;
|
||||
assert (result == Vector2f(0.f, 0.f));
|
||||
result = main;
|
||||
result -= other;
|
||||
assert (result == Vector2f(0.f, 0.f));
|
||||
|
||||
result = main + other;
|
||||
assert (result == Vector2f(20.f, 20.f));
|
||||
result = main + other;
|
||||
assert (result == Vector2f(20.f, 20.f));
|
||||
|
||||
result = main - other;
|
||||
assert (result == Vector2f(0.f, 0.f));
|
||||
result = main - other;
|
||||
assert (result == Vector2f(0.f, 0.f));
|
||||
|
||||
result = main * 10;
|
||||
assert (result == Vector2f(100.f, 100.f));
|
||||
result = main * 10;
|
||||
assert (result == Vector2f(100.f, 100.f));
|
||||
|
||||
result *= 2;
|
||||
assert (result == Vector2f(200.f, 200.f));
|
||||
result *= 2;
|
||||
assert (result == Vector2f(200.f, 200.f));
|
||||
|
||||
result = main / 2;
|
||||
assert (result == Vector2f(5.f, 5.f));
|
||||
result = main / 2;
|
||||
assert (result == Vector2f(5.f, 5.f));
|
||||
|
||||
result = main;
|
||||
result /= 2;
|
||||
assert (result == Vector2f(5.f, 5.f));
|
||||
}
|
||||
result = main;
|
||||
result /= 2;
|
||||
assert (result == Vector2f(5.f, 5.f));
|
||||
}
|
||||
|
||||
/// Aliases
|
||||
|
Loading…
Reference in New Issue
Block a user