pass by value vs by ref thing

This commit is contained in:
Andrea Blankenstijn 2021-08-14 16:37:02 +02:00
parent 478ed90d1f
commit 3458ca123b
19 changed files with 85 additions and 85 deletions

View File

@ -20,7 +20,7 @@ concept WidgetType = std::derived_from<T, bwidgets::Widget>;
template<WidgetType W>
void run_example(
const std::function<void(W*, const std::shared_ptr<bwidgets::Font>&, int, int)>& setup,
std::function<void(W*, const std::shared_ptr<bwidgets::Font>, int, int)> setup,
int w = 3, int h = 3)
{
std::atexit([]() {

View File

@ -14,12 +14,11 @@ namespace bwidgets
class Renderer;
class Texture;
[[nodiscard]] auto aa(const Color&, int, float) noexcept -> Color;
[[nodiscard]] auto filled_circle(const Color&, int resolution, Renderer*,
int aa_pixels = 3) -> std::shared_ptr<Texture>;
void set_pixels_color(
Texture*,
const std::function<uint32_t(const SDL_Point&, const SDL_PixelFormat*)>&);
[[nodiscard]] auto aa(Color, int, float) noexcept -> Color;
[[nodiscard]] auto filled_circle(Color, int resolution, Renderer*, int aa_pixels = 3)
-> std::shared_ptr<Texture>;
void set_pixels_color(Texture*,
std::function<uint32_t(SDL_Point, const SDL_PixelFormat*)>);
}
#endif

View File

@ -66,8 +66,8 @@ namespace bwidgets
auto kerning(bool) noexcept -> Font*;
[[nodiscard]] auto outline() const noexcept -> int;
auto outline(int) noexcept -> Font*;
auto render(RenderMode, const std::string&, const Color& fg = default_color_fg,
const Color& bg = default_color_bg) -> SDL_Surface*;
auto render(RenderMode, const std::string&, Color fg = default_color_fg,
Color bg = default_color_bg) -> SDL_Surface*;
[[nodiscard]] auto style() const noexcept -> uint8_t;
auto style(uint8_t) noexcept -> Font*;
[[nodiscard]] auto text_size(const std::string&) const noexcept -> Size;

View File

@ -17,7 +17,7 @@ namespace bwidgets
return (available_len - used_len) / 2;
}
[[nodiscard]] inline auto distance_sqrd(const SDL_Point& a, const SDL_Point& b) noexcept
[[nodiscard]] inline auto distance_sqrd(SDL_Point a, SDL_Point b) noexcept
-> float
{
// NOLINTNEXTLINE(bugprone-narrowing-conversions)
@ -25,13 +25,13 @@ namespace bwidgets
+ (a.y - b.y) * (a.y - b.y);
}
[[nodiscard]] inline auto distance(const SDL_Point& a, const SDL_Point& b) noexcept -> float
[[nodiscard]] inline auto distance(SDL_Point a, SDL_Point b) noexcept -> float
{
return std::sqrt(distance_sqrd(a, b));
}
template<FloatingPoint F>
[[nodiscard]] inline auto lerp(const Color& a, const Color& b, F x, bool op_alpha=false, bool op_color=true) noexcept -> Color
[[nodiscard]] inline auto lerp(Color a, Color b, F x, bool op_alpha=false, bool op_color=true) noexcept -> Color
{
return {{
op_color ? (uint8_t)std::lerp(a().r, b().r, x) : a().r,
@ -57,13 +57,13 @@ namespace bwidgets
&& (SDL_PointInRect(&bottom_right, &outer) == SDL_TRUE);
}
[[nodiscard]] inline auto rect_margin(const SDL_Rect& r, const Size& margin) noexcept
[[nodiscard]] inline auto rect_margin(const SDL_Rect& r, Size margin) noexcept
-> SDL_Rect
{
return {r.x + margin.w, r.y + margin.h, r.w - 2 * margin.w, r.h - 2 * margin.h};
}
[[nodiscard]] inline auto rect_offset(const SDL_Rect& r, const SDL_Point& offset) noexcept
[[nodiscard]] inline auto rect_offset(const SDL_Rect& r, SDL_Point offset) noexcept
-> SDL_Rect
{
return {r.x + offset.x, r.y + offset.y, r.w, r.h};

View File

@ -3,6 +3,7 @@
#include <cstdint>
#include <memory>
#include <span>
#include <vector>
#include <SDL2/SDL_render.h>
@ -46,15 +47,15 @@ namespace bwidgets
auto clear() -> Renderer*;
auto copy(const Texture*, const SDL_Rect*, const SDL_Rect*) -> Renderer*;
[[nodiscard]] auto draw_color() -> Color;
auto draw_color(const Color&) -> Renderer*;
auto draw_line(const SDL_Point&, const SDL_Point&) -> Renderer*;
auto draw_lines(const std::vector<SDL_Point>&) -> Renderer*;
auto draw_point(const SDL_Point&) -> Renderer*;
auto draw_points(const std::vector<SDL_Point>&) -> Renderer*;
auto draw_color(Color) -> Renderer*;
auto draw_line(SDL_Point, SDL_Point) -> Renderer*;
auto draw_lines(std::span<SDL_Point>) -> Renderer*;
auto draw_point(SDL_Point) -> Renderer*;
auto draw_points(std::span<SDL_Point>) -> Renderer*;
auto draw_rect(const SDL_Rect*) -> Renderer*;
auto draw_rects(const std::vector<SDL_Rect>&) -> Renderer*;
auto draw_rects(std::span<SDL_Rect>) -> Renderer*;
auto fill_rect(const SDL_Rect*) -> Renderer*;
auto fill_rects(const std::vector<SDL_Rect>&) -> Renderer*;
auto fill_rects(std::span<SDL_Rect>) -> Renderer*;
[[nodiscard]] auto output_size() -> Size;
void present() noexcept;
[[nodiscard]] auto viewport() noexcept -> SDL_Rect;

View File

@ -44,7 +44,7 @@ namespace bwidgets
[[nodiscard]] auto blend_mode() -> SDL_BlendMode;
auto blend_mode(SDL_BlendMode) -> Texture*;
[[nodiscard]] auto color_mode() -> Color;
auto color_mode(const Color&) -> Texture*;
auto color_mode(Color) -> Texture*;
[[nodiscard]] auto scale_mode() -> SDL_ScaleMode;
auto scale_mode(SDL_ScaleMode) -> Texture*;
auto update(SDL_Rect*, const void*, int) -> Texture*;

View File

@ -30,7 +30,7 @@ namespace bwidgets
bool _operate_on_colors;
template<Numeric N>
auto _operate(N operand, const std::function<N(N, N)>& operator_) const noexcept
auto _operate(N operand, std::function<N(N, N)> operator_) const noexcept
{
Color c(sdl_type, _operate_on_alpha, _operate_on_colors);
if (_operate_on_alpha) {
@ -61,7 +61,7 @@ namespace bwidgets
_operate_on_colors {op_on_colors},
sdl_type {r, g, b, a}
{}
inline Color(const SDL_Color& c = {}, bool op_on_alpha = false,
inline Color(SDL_Color c = {}, bool op_on_alpha = false,
bool op_on_colors = true) noexcept
: _operate_on_alpha(op_on_alpha), _operate_on_colors(op_on_colors), sdl_type(c)
{}
@ -80,47 +80,47 @@ namespace bwidgets
}
template<Numeric N>
[[nodiscard]] auto operator+(const N& operand) const noexcept
[[nodiscard]] auto operator+(N operand) const noexcept
{
return _operate(operand, [](const N& a, const N& b) { return a + b; });
return _operate(operand, [](N a, N b) { return a + b; });
}
template<Numeric N>
[[nodiscard]] auto operator-(const N& operand) const noexcept
[[nodiscard]] auto operator-(N operand) const noexcept
{
return _operate(operand, [](const N& a, const N& b) { return a - b; });
return _operate(operand, [](N a, N b) { return a - b; });
}
template<Numeric N>
[[nodiscard]] auto operator*(const N& operand) const noexcept
[[nodiscard]] auto operator*(N operand) const noexcept
{
return _operate(operand, [](const N& a, const N& b) { return a * b; });
return _operate(operand, [](N a, N b) { return a * b; });
}
template<Numeric N>
[[nodiscard]] auto operator/(const N& operand) const noexcept
[[nodiscard]] auto operator/(N operand) const noexcept
{
SDL_assert_release(operand != 0); // NOLINT
return _operate<N>(operand, [](const N& a, const N& b) { return a / b; });
return _operate<N>(operand, [](N a, N b) { return a / b; });
}
auto operator=(const Color& c) noexcept -> Color& = default;
auto operator=(Color&&) noexcept -> Color& = default;
auto& operator=(const SDL_Color& c) noexcept
auto& operator=(SDL_Color c) noexcept
{
sdl_type = c;
return *this;
}
[[nodiscard]] auto operator==(const Color& other) const noexcept
[[nodiscard]] auto operator==(Color other) const noexcept
{
return (_operate_on_colors && sdl_type.r == other().r
&& sdl_type.g == other().g && sdl_type.b == other().b)
|| (_operate_on_alpha && sdl_type.a == other().a);
}
[[nodiscard]] auto operator!=(const Color& other) const noexcept
[[nodiscard]] auto operator!=(Color other) const noexcept
{
return (sdl_type.r != other().r || sdl_type.g != other().g
|| sdl_type.b != other().b)

View File

@ -15,7 +15,7 @@ namespace bwidgets
T* _c_pod;
public:
OpaqueStruct(T* ptr, const Deleter& d) : _deleter {d}, _c_pod {ptr} {}
OpaqueStruct(T* ptr, Deleter d) : _deleter {std::move(d)}, _c_pod {ptr} {}
OpaqueStruct(const OpaqueStruct&) = delete;
OpaqueStruct(OpaqueStruct&&) = delete;
@ -36,11 +36,13 @@ namespace bwidgets
auto operator=(const OpaqueStruct&) = delete;
auto operator=(OpaqueStruct&&) = delete;
struct Wrapper
class Wrapper
{
protected:
OpaqueStruct _data;
Wrapper(T* ptr, const Deleter& d) : _data(ptr, d) {}
public:
Wrapper(T* ptr, Deleter d) : _data(ptr, std::move(d)) {}
};
};
}

View File

@ -53,7 +53,7 @@ namespace bwidgets
_handle_geometry_change(_viewport);
}
void _handle_font_color_change(const Color& fg, const Color& bg) override
void _handle_font_color_change(Color fg, Color bg) override
{
_input_caption.font_color_bg(bg)->font_color_fg(fg);
}
@ -140,7 +140,7 @@ namespace bwidgets
auto operator=(const Input&) = delete;
auto operator=(Input&&) = delete;
virtual auto color_fg(const Color& c) -> Input<T>*
virtual auto color_fg(Color c) -> Input<T>*
{
_input_caption.font_color_fg(c);
return this;
@ -194,7 +194,7 @@ namespace bwidgets
else
// NOLINTNEXTLINE(readability-simplify-boolean-expr)
static_assert((bool)sizeof(T) && false,
"string cannot be converted to v type T.");
"string cannot be converted to type T.");
return process_value(v);
}

View File

@ -21,7 +21,8 @@ namespace bwidgets
void _handle_focus_change(bool) override {}
void _handle_font_change(const std::shared_ptr<Font>&) override;
void _handle_font_color_change(const Color&, const Color&) override;
void _handle_font_color_change(Color, Color)
override;
void _handle_geometry_change(const SDL_Rect&) override;
void _handle_renderer_change(const std::shared_ptr<Renderer>&) override;
void _handle_rendering() override;
@ -33,9 +34,9 @@ namespace bwidgets
static const Color default_color_fg;
std::function<Color(int, int, float)> border_gradient;
Size border_size {3, 3};
Color color_bg = default_color_bg;
Color color_bg_hover = default_color_bg_hover;
Size border_size {3, 3};
Color color_bg = default_color_bg;
Color color_bg_hover = default_color_bg_hover;
Button(Widget* parent = nullptr) noexcept;
Button(const Button&) = delete;

View File

@ -21,7 +21,8 @@ namespace bwidgets
std::shared_ptr<Texture> _text_texture {nullptr};
void _handle_font_change(const std::shared_ptr<Font>&) override;
void _handle_font_color_change(const Color&, const Color&) override;
void _handle_font_color_change(Color, Color)
override;
void _handle_geometry_change(const SDL_Rect&) override;
void _handle_renderer_change(const std::shared_ptr<Renderer>&) override;
void _handle_rendering() override;

View File

@ -15,8 +15,8 @@ namespace bwidgets
Color _font_color_fg = default_font_color_fg;
Font::RenderMode _font_render_mode {Font::RenderMode::SHADED};
virtual void _handle_font_change(const std::shared_ptr<Font>&) = 0;
virtual void _handle_font_color_change(const Color&, const Color&) = 0;
virtual void _handle_font_change(const std::shared_ptr<Font>&) = 0;
virtual void _handle_font_color_change(Color, Color) = 0;
public:
inline static const Color default_font_color_bg {255, 255, 255,
@ -45,7 +45,7 @@ namespace bwidgets
return this;
}
virtual auto font_color_bg(const Color& c) -> FontHandler* final
virtual auto font_color_bg(Color c) -> FontHandler* final
{
if (c != _font_color_bg) {
_handle_font_color_change(_font_color_bg, c);
@ -54,7 +54,7 @@ namespace bwidgets
return this;
}
virtual auto font_color_fg(const Color& c) -> FontHandler* final
virtual auto font_color_fg(Color c) -> FontHandler* final
{
if (c != _font_color_fg) {
_handle_font_color_change(c, _font_color_fg);

View File

@ -30,7 +30,7 @@ namespace bwidgets
Input<T>::_handle_font_change(f);
}
void _handle_font_color_change(const Color& fg, const Color& bg) override
void _handle_font_color_change(Color fg, Color bg) override
{
Input<T>::_handle_font_color_change(fg, bg);
_decrement_button.font_color_bg(bg)->font_color_fg(fg);

View File

@ -10,8 +10,7 @@
namespace bwidgets
{
auto aa(const Color& base_color, const int aa_pixels, const float d) noexcept
-> Color
auto aa(Color base_color, const int aa_pixels, const float d) noexcept -> Color
{
Color c(base_color);
if (aa_pixels == 0) {
@ -25,8 +24,8 @@ namespace bwidgets
return c;
}
auto filled_circle(const Color& c, const int resolution, Renderer* r,
const int aa_pixels) -> std::shared_ptr<Texture>
auto filled_circle(Color c, const int resolution, Renderer* r, const int aa_pixels)
-> std::shared_ptr<Texture>
{
// clang-format off
auto texture {std::make_shared<Texture>(
@ -39,16 +38,15 @@ namespace bwidgets
const auto radius {resolution / 2};
const SDL_Point center {radius, radius};
set_pixels_color(
texture.get(),
[aa_pixels, c, center, radius](const SDL_Point& p,
const SDL_PixelFormat* format) -> uint32_t {
const auto d_delta = distance(center, p) - (float)radius;
const auto aa_color = aa(c, aa_pixels, d_delta);
set_pixels_color(texture.get(),
[aa_pixels, c, center, radius](
SDL_Point p, const SDL_PixelFormat* format) -> uint32_t {
const auto d_delta = distance(center, p) - (float)radius;
const auto aa_color = aa(c, aa_pixels, d_delta);
return SDL_MapRGBA(format, aa_color().r, aa_color().g, aa_color().b,
aa_color().a);
});
return SDL_MapRGBA(format, aa_color().r, aa_color().g,
aa_color().b, aa_color().a);
});
texture->blend_mode(SDL_BLENDMODE_BLEND);
texture->scale_mode(SDL_ScaleModeNearest);
@ -56,9 +54,7 @@ namespace bwidgets
}
void set_pixels_color(
Texture* t,
const std::function<uint32_t(const SDL_Point&, const SDL_PixelFormat*)>&
pixel_color)
Texture* t, std::function<uint32_t(SDL_Point, const SDL_PixelFormat*)> pixel_color)
{
auto attr = t->attributes();
auto pitch = attr.w * attr.format->BytesPerPixel;

View File

@ -61,8 +61,8 @@ auto Font::outline(const int size) noexcept -> Font*
return this;
}
auto Font::render(const RenderMode m, const std::string& str, const Color& fg,
const Color& bg) -> SDL_Surface*
auto Font::render(const RenderMode m, const std::string& str, Color fg, Color bg)
-> SDL_Surface*
{
std::function<SDL_Surface*()> renderer;
const char* c_str = str.empty() ? " " : str.c_str();

View File

@ -52,7 +52,7 @@ auto Renderer::draw_color() -> Color
return c;
}
auto Renderer::draw_color(const Color& c) -> Renderer*
auto Renderer::draw_color(Color c) -> Renderer*
{
success_or_throw<SDLError>(
SDL_SetRenderDrawColor(_data(), c().r, c().g, c().b, c().a));
@ -60,14 +60,14 @@ auto Renderer::draw_color(const Color& c) -> Renderer*
return this;
}
auto Renderer::draw_line(const SDL_Point& a, const SDL_Point& b) -> Renderer*
auto Renderer::draw_line(SDL_Point a, SDL_Point b) -> Renderer*
{
success_or_throw<SDLError>(SDL_RenderDrawLine(_data(), a.x, a.y, b.x, b.y));
return this;
}
auto Renderer::draw_lines(const std::vector<SDL_Point>& pts) -> Renderer*
auto Renderer::draw_lines(std::span<SDL_Point> pts) -> Renderer*
{
success_or_throw<SDLError>(
SDL_RenderDrawLines(_data(), pts.data(), (int)pts.size()));
@ -75,14 +75,14 @@ auto Renderer::draw_lines(const std::vector<SDL_Point>& pts) -> Renderer*
return this;
}
auto Renderer::draw_point(const SDL_Point& p) -> Renderer*
auto Renderer::draw_point(SDL_Point p) -> Renderer*
{
success_or_throw<SDLError>(SDL_RenderDrawPoint(_data(), p.x, p.y));
return this;
}
auto Renderer::draw_points(const std::vector<SDL_Point>& pts) -> Renderer*
auto Renderer::draw_points(std::span<SDL_Point> pts) -> Renderer*
{
success_or_throw<SDLError>(
SDL_RenderDrawPoints(_data(), pts.data(), (int)pts.size()));
@ -108,7 +108,7 @@ auto Renderer::draw_rect(const SDL_Rect* r) -> Renderer*
return this;
}
auto Renderer::draw_rects(const std::vector<SDL_Rect>& rs) -> Renderer*
auto Renderer::draw_rects(std::span<SDL_Rect> rs) -> Renderer*
{
success_or_throw<SDLError>(SDL_RenderDrawRects(_data(), rs.data(), (int)rs.size()));
@ -122,7 +122,7 @@ auto Renderer::fill_rect(const SDL_Rect* r) -> Renderer*
return this;
}
auto Renderer::fill_rects(const std::vector<SDL_Rect>& rs) -> Renderer*
auto Renderer::fill_rects(std::span<SDL_Rect> rs) -> Renderer*
{
success_or_throw<SDLError>(SDL_RenderFillRects(_data(), rs.data(), (int)rs.size()));

View File

@ -66,7 +66,7 @@ auto Texture::color_mode() -> Color
return mode;
}
auto Texture::color_mode(const Color& m) -> Texture*
auto Texture::color_mode(Color m) -> Texture*
{
success_or_throw<SDLError>(SDL_SetTextureColorMod(_data(), m().r, m().g, m().b));

View File

@ -11,7 +11,7 @@ const Color Button::default_color_bg {150, 150, 150, SDL_ALPHA_OPAQUE};
const Color Button::default_color_bg_hover {175, 175, 175, SDL_ALPHA_OPAQUE};
const Color Button::default_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE};
Button::Button(Widget* parent) noexcept : Widget{parent}, _caption{this}
Button::Button(Widget* parent) noexcept : Widget {parent}, _caption {this}
{
_focus_area = _click_area = &_widget_area;
_caption.alignment = Caption::Alignment::CENTER;
@ -49,7 +49,7 @@ void Button::_handle_font_change(const std::shared_ptr<Font>& f)
_handle_geometry_change(_viewport);
}
void Button::_handle_font_color_change(const Color& fg, const Color&)
void Button::_handle_font_color_change(Color fg, Color)
{
_caption.font_color_fg(fg);
}
@ -74,11 +74,11 @@ void Button::_handle_renderer_change(const std::shared_ptr<Renderer>& r)
void Button::_handle_rendering()
{
const Color& c = _is_hovered ? color_bg_hover : color_bg;
const auto divider = _is_pushed ? 1.5 : 2;
auto x = 0;
auto y = 0;
const auto biggest = border_size.w > border_size.h ? border_size.w : border_size.h;
Color c = _is_hovered ? color_bg_hover : color_bg;
const auto divider = _is_pushed ? 1.5 : 2;
auto x = 0;
auto y = 0;
const auto biggest = border_size.w > border_size.h ? border_size.w : border_size.h;
while (x < border_size.w || y < border_size.h) {
const auto max = x > y ? x : y;
const auto margin = Size({x, y});

View File

@ -50,7 +50,7 @@ void Caption::_handle_font_change(const std::shared_ptr<Font>&)
_text_texture.reset();
}
void Caption::_handle_font_color_change(const Color& fg, const Color& bg)
void Caption::_handle_font_color_change(Color fg, Color bg)
{
if (fg != _font_color_fg
|| (bg != _font_color_bg && _font_render_mode == Font::RenderMode::SHADED))