From 354418188ea55a2ff83526192cc471dcbd905662 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Wed, 4 Sep 2024 01:30:08 -0400 Subject: [PATCH] Add rect::shifted_by Companion of rect::shift for const objects --- src/sdl/rect.cpp | 13 +++++++++++++ src/sdl/rect.hpp | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/sdl/rect.cpp b/src/sdl/rect.cpp index 4417740460e..f74eaaebced 100644 --- a/src/sdl/rect.cpp +++ b/src/sdl/rect.cpp @@ -107,6 +107,19 @@ void rect::shift(const point& other) this->y += other.y; } +rect rect::shifted_by(int x, int y) const +{ + rect res = *this; + res.x += x; + res.y += y; + return res; +} + +rect rect::shifted_by(const point& other) const +{ + return shifted_by(other.x, other.y); +} + std::ostream& operator<<(std::ostream& s, const rect& r) { s << '[' << r.x << ',' << r.y << '|' << r.w << ',' << r.h << ']'; diff --git a/src/sdl/rect.hpp b/src/sdl/rect.hpp index 6f29a393fe2..27b2603638c 100644 --- a/src/sdl/rect.hpp +++ b/src/sdl/rect.hpp @@ -147,6 +147,10 @@ public: * The point's X and Y coordinates will be added to the rectangle's. */ void shift(const point& p); + + /** Returns a new rectangle shifted by the given relative position. */ + rect shifted_by(int x, int y) const; + rect shifted_by(const point& p) const; }; std::ostream& operator<<(std::ostream&, const rect&);