diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index 21e570a..f628247 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -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; diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index 074ac08..2f66395 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -25,13 +25,10 @@ #include #include - -struct SDL_Rect; +#include 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 diff --git a/tests/test_pointrect_constexpr.cc b/tests/test_pointrect_constexpr.cc index eabf98f..af08362 100644 --- a/tests/test_pointrect_constexpr.cc +++ b/tests/test_pointrect_constexpr.cc @@ -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()