From f0e4d3673f72f34eb1a92692025831e68edd9eca Mon Sep 17 00:00:00 2001 From: Tommy Date: Wed, 29 Jun 2022 13:49:42 +1200 Subject: [PATCH] sdl/rect: Add scalar multiplication and division operators Mostly useful for simplifying pixel scale conversions. --- src/gui/core/canvas.cpp | 8 ++------ src/sdl/rect.hpp | 6 ++++++ src/video.cpp | 5 ++--- src/video.hpp | 3 ++- src/widgets/textbox.cpp | 5 ++--- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/gui/core/canvas.cpp b/src/gui/core/canvas.cpp index a8a75921a97..a3794bf5788 100644 --- a/src/gui/core/canvas.cpp +++ b/src/gui/core/canvas.cpp @@ -461,16 +461,12 @@ void text_shape::draw(wfl::map_formula_callable& variables) SDL_Rect visible = sdl::intersect_rects(draw::get_clip(), dst_rect); // Get the source region of text for clipping. - SDL_Rect clip_in = visible; + rect clip_in = visible; clip_in.x -= x; clip_in.y -= y; // Source region for high-dpi text needs to have pixel scale applied. - const int pixel_scale = CVideo::get_singleton().get_pixel_scale(); - clip_in.x *= pixel_scale; - clip_in.y *= pixel_scale; - clip_in.w *= pixel_scale; - clip_in.h *= pixel_scale; + clip_in *= CVideo::get_singleton().get_pixel_scale(); // Render the currently visible portion of text // TODO: highdpi - it would be better to render this all, but some things currently have far too much text. Namely the credits screen. diff --git a/src/sdl/rect.hpp b/src/sdl/rect.hpp index c02528df247..15679a19b56 100644 --- a/src/sdl/rect.hpp +++ b/src/sdl/rect.hpp @@ -122,6 +122,12 @@ public: bool operator==(const rect& r) const; bool operator==(const SDL_Rect& r) const; + // Scalar multiplication and division + rect operator*(int s) const { return {x*s, y*s, w*s, h*s}; } + rect& operator*=(int s) { x*=s; y*=s; w*=s; h*=s; return *this; } + rect operator/(int s) const { return {x/s, y/s, w/s, h/s}; } + rect& operator/=(int s) { x/=s; y/=s; w/=s; h/=s; return *this; } + /** False if both w and h are > 0, true otherwise. */ bool empty() const; }; diff --git a/src/video.cpp b/src/video.cpp index c773ed9f95c..15bed129ae7 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -528,10 +528,9 @@ SDL_Rect CVideo::clip_to_draw_area(const SDL_Rect* r) const } } -SDL_Rect CVideo::to_output(const SDL_Rect& r) const +rect CVideo::to_output(const rect& r) const { - int s = get_pixel_scale(); - return {s * r.x, s * r.y, s * r.w, s * r.h}; + return r * get_pixel_scale(); } void CVideo::render_screen() diff --git a/src/video.hpp b/src/video.hpp index de75d68fb83..c3045419355 100644 --- a/src/video.hpp +++ b/src/video.hpp @@ -19,6 +19,7 @@ #include "exceptions.hpp" #include "lua_jailbreak_exception.hpp" #include "sdl/point.hpp" +#include "sdl/rect.hpp" #include "sdl/texture.hpp" #include @@ -198,7 +199,7 @@ public: /** * Convert coordinates in draw space to coordinates in render space. */ - SDL_Rect to_output(const SDL_Rect& draw_space_rect) const; + rect to_output(const rect& draw_space_rect) const; /** * Tests whether the given flags are currently set on the SDL window. diff --git a/src/widgets/textbox.cpp b/src/widgets/textbox.cpp index 78af5b75274..dbd8ea0c695 100644 --- a/src/widgets/textbox.cpp +++ b/src/widgets/textbox.cpp @@ -184,7 +184,7 @@ void textbox::draw_contents() draw::fill(loc, c); - SDL_Rect src; + rect src; if(text_image_ == nullptr) { update_text_cache(true); @@ -227,8 +227,7 @@ void textbox::draw_contents() } // text may be rendered at high dpi, scale source clip accordingly - const int ps = video().get_pixel_scale(); - src.x *= ps; src.y *= ps; src.w *= ps; src.h *= ps; + src *= video().get_pixel_scale(); // TODO: highdpi - consider sizing source clip in draw coordinates, now that textures have a draw-space w/h? That would work here. if(enabled()) {