sdl/rect: Add scalar multiplication and division operators

Mostly useful for simplifying pixel scale conversions.
This commit is contained in:
Tommy 2022-06-29 13:49:42 +12:00
parent 9181ae2cc4
commit f0e4d3673f
5 changed files with 14 additions and 13 deletions

View File

@ -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.

View File

@ -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;
};

View File

@ -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()

View File

@ -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 <SDL2/SDL_render.h>
@ -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.

View File

@ -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()) {