Constexprify Rect constructors

This commit is contained in:
Dmitry Marakasov 2015-07-07 15:41:28 +03:00
parent 63cd8feebc
commit 4f3256fda5
3 changed files with 21 additions and 36 deletions

View File

@ -27,34 +27,6 @@
namespace SDL2pp {
Rect::Rect() {
x = 0;
y = 0;
w = 0;
h = 0;
}
Rect::Rect(const SDL_Rect& rect) {
x = rect.x;
y = rect.y;
w = rect.w;
h = rect.h;
}
Rect::Rect(const Point& corner, const Point& size) {
x = corner.x;
y = corner.y;
w = size.x;
h = size.y;
}
Rect::Rect(int nx, int ny, int nw, int nh) {
x = nx;
y = ny;
w = nw;
h = nh;
}
bool Rect::operator==(const Rect& other) const {
return x == other.x && y == other.y &&
w == other.w && h == other.h;

View File

@ -25,13 +25,10 @@
#include <SDL2/SDL_rect.h>
#include <SDL2pp/Optional.hh>
struct SDL_Rect;
#include <SDL2pp/Point.hh>
namespace SDL2pp {
class Point;
////////////////////////////////////////////////////////////
/// \brief 2D rectangle
///
@ -55,7 +52,8 @@ public:
/// Creates a Rect(0, 0, 0, 0)
///
////////////////////////////////////////////////////////////
Rect();
constexpr Rect() : SDL_Rect{0, 0, 0, 0} {
}
////////////////////////////////////////////////////////////
/// \brief Construct a rect from existing SDL_Rect
@ -63,7 +61,8 @@ public:
/// \param[in] rect Existing SDL_Rect
///
////////////////////////////////////////////////////////////
Rect(const SDL_Rect& rect);
constexpr Rect(const SDL_Rect& rect) : SDL_Rect{rect.x, rect.y, rect.w, rect.h} {
}
////////////////////////////////////////////////////////////
/// \brief Construct the rect from given corner coordinates, and size
@ -72,7 +71,8 @@ public:
/// \param[in] size Dimensions of the rectangle
///
////////////////////////////////////////////////////////////
Rect(const Point& corner, const Point& size);
constexpr Rect(const Point& corner, const Point& size) : SDL_Rect{corner.x, corner.y, size.x, size.y} {
}
////////////////////////////////////////////////////////////
/// \brief Construct the rect from given corner coordinates, width and height
@ -83,7 +83,8 @@ public:
/// \param[in] h Height of the rectangle
///
////////////////////////////////////////////////////////////
Rect(int x, int y, int w, int h);
constexpr Rect(int x, int y, int w, int h) : SDL_Rect{x, y, w, h} {
}
////////////////////////////////////////////////////////////
/// \brief Construct the rect from given center coordinates, width and height

View File

@ -38,4 +38,16 @@ BEGIN_TEST()
EXPECT_EQUAL(neg + sum + diff + mul1 + mul2 + div1 + div2 + rem1 + rem2, Point(0, 0));
}
{
constexpr SDL_Rect sr{0, 0, 1, 1};
constexpr Rect r1;
constexpr Rect r2(sr);
constexpr Rect r3(Point(0, 0), Point(1, 1));
constexpr Rect r4(0, 0, 1, 1);
constexpr Rect r5(r4);
EXPECT_EQUAL(r4, r2);
EXPECT_EQUAL(r5, r3);
}
END_TEST()