From 6d0213810b84bcde1efab514369de5e905973d92 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 25 Dec 2014 18:30:52 +0300 Subject: [PATCH] Switch to Optional Remove "valid" flag from Rect and Point, derive them directly from SDL_Rect and SDL_Point structures, simplify logic. Now x/y/w/h member variables are directly accessible and Rect/Point may be passed as SDL_Rect/SDL_Point via pointer or reference. Change all cases where Null Rects and Points were used to Optional. invalid state related functions like Null(), IsNull() and Get() are now deprecated but are not removed yet for compatibility sake. --- SDL2pp/Point.cc | 67 ++++++++++---------------- SDL2pp/Point.hh | 21 ++++---- SDL2pp/Rect.cc | 103 ++++++++++++++++------------------------ SDL2pp/Rect.hh | 16 +++---- SDL2pp/Renderer.cc | 10 ++-- SDL2pp/Renderer.hh | 5 +- SDL2pp/Texture.cc | 6 +-- SDL2pp/Texture.hh | 7 +-- SDL2pp/TextureLock.cc | 4 +- tests/test_pointrect.cc | 42 ++++------------ 10 files changed, 103 insertions(+), 178 deletions(-) diff --git a/SDL2pp/Point.cc b/SDL2pp/Point.cc index 6185f5c..07311db 100644 --- a/SDL2pp/Point.cc +++ b/SDL2pp/Point.cc @@ -25,25 +25,22 @@ namespace SDL2pp { -Point::Point() : valid_(false) { +Point::Point() { + x = 0; + y = 0; } -Point::~Point() { +Point::Point(int nx, int ny) { + x = nx; + y = ny; } -Point::Point(int x, int y) : valid_(true) { - point_.x = x; - point_.y = y; -} - -Point Point::Null() { - return Point(); +Optional Point::Null() { + return NullOpt; } bool Point::operator==(const Point& other) const { - if (!valid_ || !other.valid_) - return valid_ == other.valid_; // true only if both null - return point_.x == other.point_.x && point_.y == other.point_.y; + return x == other.x && y == other.y; } bool Point::operator!=(const Point& other) const { @@ -51,67 +48,51 @@ bool Point::operator!=(const Point& other) const { } SDL_Point* Point::Get() { - return valid_ ? &point_ : nullptr; + return this; } const SDL_Point* Point::Get() const { - return valid_ ? &point_ : nullptr; + return this; } bool Point::IsNull() const { - return !valid_; + return false; } int Point::GetX() const { - assert(!IsNull()); - return point_.x; + return x; } -void Point::SetX(int x) { - assert(!IsNull()); - point_.x = x; +void Point::SetX(int nx) { + x = nx; } int Point::GetY() const { - assert(!IsNull()); - return point_.y; + return y; } -void Point::SetY(int y) { - assert(!IsNull()); - point_.y = y; +void Point::SetY(int ny) { + y = ny; } Point Point::operator+(const Point& other) const { - if (!valid_ || !other.valid_) - return Point(); - return Point(point_.x + other.point_.x, point_.y + other.point_.y); + return Point(x + other.x, y + other.y); } Point Point::operator-(const Point& other) const { - if (!valid_ || !other.valid_) - return Point(); - return Point(point_.x - other.point_.x, point_.y - other.point_.y); + return Point(x - other.x, y - other.y); } Point& Point::operator+=(const Point& other) { - if (!valid_ || !other.valid_) { - valid_ = false; - } else { - point_.x += other.point_.x; - point_.y += other.point_.y; - } + x += other.x; + y += other.y; return *this; } Point& Point::operator-=(const Point& other) { - if (!valid_ || !other.valid_) { - valid_ = false; - } else { - point_.x -= other.point_.x; - point_.y -= other.point_.y; - } + x -= other.x; + y -= other.y; return *this; } diff --git a/SDL2pp/Point.hh b/SDL2pp/Point.hh index 46cee26..a622ccb 100644 --- a/SDL2pp/Point.hh +++ b/SDL2pp/Point.hh @@ -24,21 +24,16 @@ #include +#include // for deprecated functionality + namespace SDL2pp { -class Point { -private: - SDL_Point point_; - bool valid_; - -private: - Point(); - +class Point : public SDL_Point{ public: - Point(int x, int y); - virtual ~Point(); + Point(); + Point(int nx, int ny); - static Point Null(); + static Optional Null(); Point(const Point&) noexcept = default; Point(Point&&) noexcept = default; @@ -54,10 +49,10 @@ public: bool IsNull() const; int GetX() const; - void SetX(int x); + void SetX(int nx); int GetY() const; - void SetY(int y); + void SetY(int ny); Point operator+(const Point& other) const; Point operator-(const Point& other) const; diff --git a/SDL2pp/Rect.cc b/SDL2pp/Rect.cc index 2f7a5ab..aef68d3 100644 --- a/SDL2pp/Rect.cc +++ b/SDL2pp/Rect.cc @@ -27,28 +27,27 @@ namespace SDL2pp { -Rect::Rect() : valid_(false) { +Rect::Rect() { + x = 0; + y = 0; + w = 0; + h = 0; } -Rect::~Rect() { +Rect::Rect(int nx, int ny, int nw, int nh) { + x = nx; + y = ny; + w = nw; + h = nh; } -Rect::Rect(int x, int y, int w, int h) : valid_(true) { - rect_.x = x; - rect_.y = y; - rect_.w = w; - rect_.h = h; -} - -Rect Rect::Null() { - return Rect(); +Optional Rect::Null() { + return NullOpt; } bool Rect::operator==(const Rect& other) const { - if (!valid_ || !other.valid_) - return valid_ == other.valid_; // true only if both null - return rect_.x == other.rect_.x && rect_.y == other.rect_.y && - rect_.w == other.rect_.w && rect_.h == other.rect_.h; + return x == other.x && y == other.y && + w == other.w && h == other.h; } bool Rect::operator!=(const Rect& other) const { @@ -56,11 +55,11 @@ bool Rect::operator!=(const Rect& other) const { } SDL_Rect* Rect::Get() { - return valid_ ? &rect_ : nullptr; + return this; } const SDL_Rect* Rect::Get() const { - return valid_ ? &rect_ : nullptr; + return this; } Rect Rect::FromCenter(int cx, int cy, int w, int h) { @@ -68,101 +67,79 @@ Rect Rect::FromCenter(int cx, int cy, int w, int h) { } bool Rect::IsNull() const { - return !valid_; + return false; } int Rect::GetX() const { - assert(!IsNull()); - return rect_.x; + return x; } -void Rect::SetX(int x) { - assert(!IsNull()); - rect_.x = x; +void Rect::SetX(int nx) { + x = nx; } int Rect::GetY() const { - assert(!IsNull()); - return rect_.y; + return y; } -void Rect::SetY(int y) { - assert(!IsNull()); - rect_.y = y; +void Rect::SetY(int ny) { + y = ny; } int Rect::GetW() const { - assert(!IsNull()); - return rect_.w; + return w; } -void Rect::SetW(int w) { - assert(!IsNull()); - rect_.w = w; +void Rect::SetW(int nw) { + w = nw; } int Rect::GetH() const { - assert(!IsNull()); - return rect_.h; + return h; } -void Rect::SetH(int h) { - assert(!IsNull()); - rect_.h = h; +void Rect::SetH(int nh) { + h = nh; } int Rect::GetX2() const { - assert(!IsNull()); - return rect_.x + rect_.w - 1; + return x + w - 1; } void Rect::SetX2(int x2) { - assert(!IsNull()); - rect_.w = x2 - rect_.x + 1; + w = x2 - x + 1; } int Rect::GetY2() const { - assert(!IsNull()); - return rect_.y + rect_.h - 1; + return y + h - 1; } void Rect::SetY2(int y2) { - assert(!IsNull()); - rect_.h = y2 - rect_.y + 1; + h = y2 - y + 1; } bool Rect::Contains(const Point& point) const { - if (IsNull() || point.IsNull()) - return false; - return !(point.GetX() < GetX() || point.GetY() < GetY() || point.GetX() > GetX2() || point.GetY() > GetY2()); + return !(point.x < x || point.y < y || point.x > GetX2() || point.y > GetY2()); } Rect Rect::operator+(const Point& offset) const { - assert(!IsNull() && !offset.IsNull()); - - return Rect(rect_.x + offset.GetX(), rect_.y + offset.GetY(), rect_.w, rect_.h); + return Rect(x + offset.x, y + offset.y, w, h); } Rect& Rect::operator+=(const Point& offset) { - assert(!IsNull() && !offset.IsNull()); - - rect_.x += offset.GetX(); - rect_.y += offset.GetY(); + x += offset.x; + y += offset.y; return *this; } Rect Rect::operator-(const Point& offset) const { - assert(!IsNull() && !offset.IsNull()); - - return Rect(rect_.x - offset.GetX(), rect_.y - offset.GetY(), rect_.w, rect_.h); + return Rect(x - offset.x, y - offset.y, w, h); } Rect& Rect::operator-=(const Point& offset) { - assert(!IsNull() && !offset.IsNull()); - - rect_.x -= offset.GetX(); - rect_.y -= offset.GetY(); + x -= offset.x; + y -= offset.y; return *this; } diff --git a/SDL2pp/Rect.hh b/SDL2pp/Rect.hh index a8305c8..36dfe32 100644 --- a/SDL2pp/Rect.hh +++ b/SDL2pp/Rect.hh @@ -24,25 +24,20 @@ #include +#include // for deprecated functionality + struct SDL_Rect; namespace SDL2pp { class Point; -class Rect { -private: - SDL_Rect rect_; - bool valid_; - -private: - Rect(); - +class Rect : public SDL_Rect { public: + Rect(); Rect(int x, int y, int w, int h); - virtual ~Rect(); - static Rect Null(); + static Optional Null(); static Rect FromCenter(int cx, int cy, int w, int h); @@ -54,6 +49,7 @@ public: bool operator==(const Rect& other) const; bool operator!=(const Rect& other) const; + // deprecated SDL_Rect* Get(); const SDL_Rect* Get() const; diff --git a/SDL2pp/Renderer.cc b/SDL2pp/Renderer.cc index 31b962c..079f9b8 100644 --- a/SDL2pp/Renderer.cc +++ b/SDL2pp/Renderer.cc @@ -27,8 +27,6 @@ #include #include #include -#include -#include namespace SDL2pp { @@ -74,13 +72,13 @@ void Renderer::GetInfo(SDL_RendererInfo* info) { throw Exception("SDL_GetRendererInfo failed"); } -void Renderer::Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect) { - if (SDL_RenderCopy(renderer_, texture.Get(), srcrect.Get(), dstrect.Get()) != 0) +void Renderer::Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect) { + if (SDL_RenderCopy(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr) != 0) throw Exception("SDL_RenderCopy failed"); } -void Renderer::Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect, double angle, const Point& center, int flip) { - if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect.Get(), dstrect.Get(), angle, center.Get(), static_cast(flip)) != 0) +void Renderer::Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect, double angle, const Optional& center, int flip) { + if (SDL_RenderCopyEx(renderer_, texture.Get(), srcrect ? &*srcrect : nullptr, dstrect ? &*dstrect : nullptr, angle, center ? &*center : nullptr, static_cast(flip)) != 0) throw Exception("SDL_RenderCopyEx failed"); } diff --git a/SDL2pp/Renderer.hh b/SDL2pp/Renderer.hh index e1ddbbc..8b27d73 100644 --- a/SDL2pp/Renderer.hh +++ b/SDL2pp/Renderer.hh @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -58,8 +59,8 @@ public: void GetInfo(SDL_RendererInfo* info); - void Copy(Texture& texture, const Rect& srcrect = Rect::Null(), const Rect& dstrect = Rect::Null()); - void Copy(Texture& texture, const Rect& srcrect, const Rect& dstrect, double angle, const Point& center = Point::Null(), int flip = 0); + void Copy(Texture& texture, const Optional& srcrect = NullOpt, const Optional& dstrect = NullOpt); + void Copy(Texture& texture, const Optional& srcrect, const Optional& dstrect, double angle, const Optional& center = NullOpt, int flip = 0); void SetDrawColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 255); void SetTarget(); diff --git a/SDL2pp/Texture.cc b/SDL2pp/Texture.cc index 7180444..7a57687 100644 --- a/SDL2pp/Texture.cc +++ b/SDL2pp/Texture.cc @@ -78,8 +78,8 @@ SDL_Texture* Texture::Get() const { return texture_; } -void Texture::Update(const Rect& rect, const void* pixels, int pitch) { - if (SDL_UpdateTexture(texture_, rect.Get(), pixels, pitch) != 0) +void Texture::Update(const Optional& rect, const void* pixels, int pitch) { + if (SDL_UpdateTexture(texture_, rect ? &*rect : nullptr, pixels, pitch) != 0) throw Exception("SDL_UpdateTexture failed"); } @@ -98,7 +98,7 @@ void Texture::SetColorMod(Uint8 r, Uint8 g, Uint8 b) { throw Exception("SDL_SetTextureColorMod failed"); } -Texture::LockHandle Texture::Lock(const Rect& rect = Rect::Null()) { +Texture::LockHandle Texture::Lock(const Optional& rect) { return LockHandle(this, rect); } diff --git a/SDL2pp/Texture.hh b/SDL2pp/Texture.hh index 18a5c16..7145061 100644 --- a/SDL2pp/Texture.hh +++ b/SDL2pp/Texture.hh @@ -27,6 +27,7 @@ #include #include +#include #include struct SDL_Texture; @@ -50,7 +51,7 @@ public: int pitch_; private: - LockHandle(Texture* texture, const Rect& rect); + LockHandle(Texture* texture, const Optional& rect); public: LockHandle(); @@ -81,13 +82,13 @@ public: SDL_Texture* Get() const; - void Update(const Rect& rect, const void* pixels, int pitch); + void Update(const Optional& rect, const void* pixels, int pitch); void SetBlendMode(SDL_BlendMode blendMode); void SetAlphaMod(Uint8 alpha = 255); void SetColorMod(Uint8 r = 255, Uint8 g = 255, Uint8 b = 255); - LockHandle Lock(const Rect& rect); + LockHandle Lock(const Optional& rect); Uint32 GetFormat() const; int GetAccess() const; diff --git a/SDL2pp/TextureLock.cc b/SDL2pp/TextureLock.cc index 8622b6e..048c68d 100644 --- a/SDL2pp/TextureLock.cc +++ b/SDL2pp/TextureLock.cc @@ -31,8 +31,8 @@ namespace SDL2pp { Texture::LockHandle::LockHandle() : texture_(nullptr) { } -Texture::LockHandle::LockHandle(Texture* texture, const Rect& rect) : texture_(texture) { - if (SDL_LockTexture(texture_->Get(), rect.Get(), &pixels_, &pitch_) != 0) +Texture::LockHandle::LockHandle(Texture* texture, const Optional& rect) : texture_(texture) { + if (SDL_LockTexture(texture_->Get(), rect ? &*rect : nullptr, &pixels_, &pitch_) != 0) throw Exception("SDL_LockTexture failed"); } diff --git a/tests/test_pointrect.cc b/tests/test_pointrect.cc index 3e3a3fb..d15f22a 100644 --- a/tests/test_pointrect.cc +++ b/tests/test_pointrect.cc @@ -10,32 +10,27 @@ BEGIN_TEST() // Point basic ops Point p(1,2); - EXPECT_TRUE(!p.IsNull()); EXPECT_TRUE(p.GetX() == 1 && p.GetY() == 2); EXPECT_TRUE(p == Point(1,2)); EXPECT_TRUE(p != Point(1,1)); EXPECT_TRUE(p != Point(2,2)); - EXPECT_TRUE(p != Point::Null()); - EXPECT_TRUE(p.Get() != nullptr); - EXPECT_TRUE(p.Get()->x == 1 && p.Get()->y == 2); + EXPECT_TRUE(p.x == 1); + EXPECT_TRUE(p.y == 2); p.SetX(4); p.SetY(5); EXPECT_TRUE(p.GetX() == 4 && p.GetY() == 5); EXPECT_TRUE(p == Point(4,5)); + EXPECT_TRUE(p.x == 4); + EXPECT_TRUE(p.y == 5); p = Point(6,7); EXPECT_TRUE(p.GetX() == 6 && p.GetY() == 7); EXPECT_TRUE(p == Point(6,7)); - - p = Point::Null(); - - EXPECT_TRUE(p != Point(1,2)); - EXPECT_TRUE(p == Point::Null()); - EXPECT_TRUE(p.IsNull()); - EXPECT_TRUE(p.Get() == nullptr); + EXPECT_TRUE(p.x == 6); + EXPECT_TRUE(p.y == 7); } { @@ -71,35 +66,21 @@ BEGIN_TEST() EXPECT_TRUE(sum.GetX() == 111 && sum.GetY() == 222); EXPECT_TRUE(diff.GetX() == -111 && diff.GetY() == -222); - - sum += Point::Null(); - diff -= Point::Null(); - - EXPECT_TRUE(sum.IsNull()); - EXPECT_TRUE(diff.IsNull()); - - EXPECT_TRUE((Point(1,1) + Point::Null()).IsNull()); - EXPECT_TRUE((Point(1,1) - Point::Null()).IsNull()); - EXPECT_TRUE((Point::Null() + Point(1,1)).IsNull()); - EXPECT_TRUE((Point::Null() - Point(1,1)).IsNull()); - EXPECT_TRUE((Point::Null() - Point::Null()).IsNull()); - EXPECT_TRUE((Point::Null() - Point::Null()).IsNull()); } { // Rect basic ops Rect r(1,2,3,4); - EXPECT_TRUE(!r.IsNull()); EXPECT_TRUE(r.GetX() == 1 && r.GetY() == 2 && r.GetW() == 3 && r.GetH() == 4); EXPECT_TRUE(r == Rect(1,2,3,4)); EXPECT_TRUE(r != Rect(2,2,3,4)); EXPECT_TRUE(r != Rect(1,3,3,4)); EXPECT_TRUE(r != Rect(1,2,4,4)); EXPECT_TRUE(r != Rect(1,2,3,5)); - EXPECT_TRUE(r != Rect::Null()); EXPECT_TRUE(r.Get() != nullptr); EXPECT_TRUE(r.Get()->x == 1 && r.Get()->y == 2 && r.Get()->w == 3 && r.Get()->h == 4); + EXPECT_TRUE(r.x == 1 && r.y == 2 && r.w == 3 && r.h == 4); r.SetX(5); r.SetY(6); @@ -108,18 +89,13 @@ BEGIN_TEST() EXPECT_TRUE(r.GetX() == 5 && r.GetY() == 6 && r.GetW() == 7 && r.GetH() == 8); EXPECT_TRUE(r == Rect(5,6,7,8)); + EXPECT_TRUE(r.x == 5 && r.y == 6 && r.w == 7 && r.h == 8); r = Rect(9,10,11,12); EXPECT_TRUE(r.GetX() == 9 && r.GetY() == 10 && r.GetW() == 11 && r.GetH() == 12); EXPECT_TRUE(r == Rect(9,10,11,12)); - - r = Rect::Null(); - - EXPECT_TRUE(r != Rect(1,2,3,4)); - EXPECT_TRUE(r == Rect::Null()); - EXPECT_TRUE(r.IsNull()); - EXPECT_TRUE(r.Get() == nullptr); + EXPECT_TRUE(r.x == 9 && r.y == 10 && r.w == 11 && r.h == 12); } {