mirror of
https://github.com/libSDL2pp/libSDL2pp.git
synced 2025-08-03 10:55:57 -04:00
Add Point and Rect less-than operator
This allows using them as map and set keys
This commit is contained in:
parent
f525231b94
commit
95142b065c
@ -69,3 +69,9 @@ std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point) {
|
||||
stream << "[x:" << point.x << ",y:" << point.y << "]";
|
||||
return stream;
|
||||
}
|
||||
|
||||
bool operator<(const SDL2pp::Point& a, const SDL2pp::Point& b) {
|
||||
if (a.x == b.x)
|
||||
return a.y < b.y;
|
||||
return a.x < b.x;
|
||||
}
|
||||
|
@ -458,4 +458,15 @@ public:
|
||||
////////////////////////////////////////////////////////////
|
||||
std::ostream& operator<<(std::ostream& stream, const SDL2pp::Point& point);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Less-than operator for SDL2pp::Point
|
||||
///
|
||||
/// \param[in] a First comparison argument
|
||||
/// \param[in] b Second comparison argument
|
||||
///
|
||||
/// \returns true if a < b
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool operator<(const SDL2pp::Point& a, const SDL2pp::Point& b);
|
||||
|
||||
#endif
|
||||
|
@ -88,3 +88,15 @@ std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect) {
|
||||
stream << "[x:" << rect.x << ",y:" << rect.y << ",w:" << rect.w << ",h:" << rect.h << "]";
|
||||
return stream;
|
||||
}
|
||||
|
||||
bool operator<(const SDL2pp::Rect& a, const SDL2pp::Rect& b) {
|
||||
if (a.x == b.x) {
|
||||
if (a.y == b.y) {
|
||||
if (a.w == b.w)
|
||||
return a.h < b.h;
|
||||
return a.w < b.w;
|
||||
}
|
||||
return a.y < b.y;
|
||||
}
|
||||
return a.x < b.x;
|
||||
}
|
||||
|
@ -534,4 +534,15 @@ public:
|
||||
////////////////////////////////////////////////////////////
|
||||
std::ostream& operator<<(std::ostream& stream, const SDL2pp::Rect& rect);
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// \brief Less-than operator for SDL2pp::Rect
|
||||
///
|
||||
/// \param[in] a First comparison argument
|
||||
/// \param[in] b Second comparison argument
|
||||
///
|
||||
/// \returns true if a < b
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
bool operator<(const SDL2pp::Rect& a, const SDL2pp::Rect& b);
|
||||
|
||||
#endif
|
||||
|
@ -79,6 +79,18 @@ BEGIN_TEST()
|
||||
EXPECT_EQUAL(p /= Point(5, 3), Point(3, 3));
|
||||
EXPECT_EQUAL(p *= Point(10, 20), Point(30, 60));
|
||||
EXPECT_EQUAL(p %= Point(7, 11), Point(2, 5));
|
||||
|
||||
// Less-than
|
||||
EXPECT_TRUE(Point(0, 0) < Point(1, 0));
|
||||
EXPECT_TRUE(Point(0, 1) < Point(1, 0));
|
||||
EXPECT_TRUE(Point(0, 1) < Point(1, 1));
|
||||
EXPECT_TRUE(Point(0, 0) < Point(0, 1));
|
||||
|
||||
EXPECT_TRUE(!(Point(1, 0) < Point(0, 0)));
|
||||
EXPECT_TRUE(!(Point(1, 0) < Point(0, 1)));
|
||||
EXPECT_TRUE(!(Point(1, 1) < Point(0, 1)));
|
||||
EXPECT_TRUE(!(Point(0, 1) < Point(0, 0)));
|
||||
EXPECT_TRUE(!(Point(1, 1) < Point(1, 1)));
|
||||
}
|
||||
|
||||
{
|
||||
@ -286,6 +298,39 @@ BEGIN_TEST()
|
||||
EXPECT_EQUAL(r, Rect(-9, -18, 3, 4));
|
||||
}
|
||||
|
||||
{
|
||||
// Less-than
|
||||
EXPECT_TRUE(!(Rect(0, 0, 0, 0) < Rect(0, 0, 0, 0)));
|
||||
EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 0, 0, 1));
|
||||
EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 0, 1, 0));
|
||||
EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(0, 1, 0, 0));
|
||||
EXPECT_TRUE(Rect(0, 0, 0, 0) < Rect(1, 0, 0, 0));
|
||||
|
||||
EXPECT_TRUE(!(Rect(0, 0, 0, 1) < Rect(0, 0, 0, 0)));
|
||||
EXPECT_TRUE(!(Rect(0, 0, 0, 1) < Rect(0, 0, 0, 1)));
|
||||
EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(0, 0, 1, 0));
|
||||
EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(0, 1, 0, 0));
|
||||
EXPECT_TRUE(Rect(0, 0, 0, 1) < Rect(1, 0, 0, 0));
|
||||
|
||||
EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 0, 0)));
|
||||
EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 0, 1)));
|
||||
EXPECT_TRUE(!(Rect(0, 0, 1, 0) < Rect(0, 0, 1, 0)));
|
||||
EXPECT_TRUE(Rect(0, 0, 1, 0) < Rect(0, 1, 0, 0));
|
||||
EXPECT_TRUE(Rect(0, 0, 1, 0) < Rect(1, 0, 0, 0));
|
||||
|
||||
EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 0, 0)));
|
||||
EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 0, 1)));
|
||||
EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 0, 1, 0)));
|
||||
EXPECT_TRUE(!(Rect(0, 1, 0, 0) < Rect(0, 1, 0, 0)));
|
||||
EXPECT_TRUE(Rect(0, 1, 0, 0) < Rect(1, 0, 0, 0));
|
||||
|
||||
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 0, 0)));
|
||||
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 0, 1)));
|
||||
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 0, 1, 0)));
|
||||
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(0, 1, 0, 0)));
|
||||
EXPECT_TRUE(!(Rect(1, 0, 0, 0) < Rect(1, 0, 0, 0)));
|
||||
}
|
||||
|
||||
{
|
||||
// Construction from and comparison with SDL objects
|
||||
SDL_Rect sdlrect = { 1, 2, 3, 4 };
|
||||
|
Loading…
x
Reference in New Issue
Block a user