diff --git a/examples/run.hpp b/examples/run.hpp index 0f95d48..23d0969 100644 --- a/examples/run.hpp +++ b/examples/run.hpp @@ -20,7 +20,7 @@ concept WidgetType = std::derived_from; template void run_example( - const std::function&, int, int)>& setup, + std::function, int, int)> setup, int w = 3, int h = 3) { std::atexit([]() { diff --git a/inc/basic_widgets/core/draw.hpp b/inc/basic_widgets/core/draw.hpp index 13f966a..8c42ee5 100644 --- a/inc/basic_widgets/core/draw.hpp +++ b/inc/basic_widgets/core/draw.hpp @@ -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; - void set_pixels_color( - Texture*, - const std::function&); + [[nodiscard]] auto aa(Color, int, float) noexcept -> Color; + [[nodiscard]] auto filled_circle(Color, int resolution, Renderer*, int aa_pixels = 3) + -> std::shared_ptr; + void set_pixels_color(Texture*, + std::function); } #endif diff --git a/inc/basic_widgets/core/font.hpp b/inc/basic_widgets/core/font.hpp index 99aed56..8b36799 100644 --- a/inc/basic_widgets/core/font.hpp +++ b/inc/basic_widgets/core/font.hpp @@ -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; diff --git a/inc/basic_widgets/core/math.hpp b/inc/basic_widgets/core/math.hpp index 36efe24..350e138 100644 --- a/inc/basic_widgets/core/math.hpp +++ b/inc/basic_widgets/core/math.hpp @@ -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 - [[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}; diff --git a/inc/basic_widgets/core/renderer.hpp b/inc/basic_widgets/core/renderer.hpp index bc376c8..14f4ea9 100644 --- a/inc/basic_widgets/core/renderer.hpp +++ b/inc/basic_widgets/core/renderer.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -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&) -> Renderer*; - auto draw_point(const SDL_Point&) -> Renderer*; - auto draw_points(const std::vector&) -> Renderer*; + auto draw_color(Color) -> Renderer*; + auto draw_line(SDL_Point, SDL_Point) -> Renderer*; + auto draw_lines(std::span) -> Renderer*; + auto draw_point(SDL_Point) -> Renderer*; + auto draw_points(std::span) -> Renderer*; auto draw_rect(const SDL_Rect*) -> Renderer*; - auto draw_rects(const std::vector&) -> Renderer*; + auto draw_rects(std::span) -> Renderer*; auto fill_rect(const SDL_Rect*) -> Renderer*; - auto fill_rects(const std::vector&) -> Renderer*; + auto fill_rects(std::span) -> Renderer*; [[nodiscard]] auto output_size() -> Size; void present() noexcept; [[nodiscard]] auto viewport() noexcept -> SDL_Rect; diff --git a/inc/basic_widgets/core/texture.hpp b/inc/basic_widgets/core/texture.hpp index 61f1a3f..a8799ba 100644 --- a/inc/basic_widgets/core/texture.hpp +++ b/inc/basic_widgets/core/texture.hpp @@ -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*; diff --git a/inc/basic_widgets/core/type/color.hpp b/inc/basic_widgets/core/type/color.hpp index 8c48cd1..80d95ac 100644 --- a/inc/basic_widgets/core/type/color.hpp +++ b/inc/basic_widgets/core/type/color.hpp @@ -30,7 +30,7 @@ namespace bwidgets bool _operate_on_colors; template - auto _operate(N operand, const std::function& operator_) const noexcept + auto _operate(N operand, std::function 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 - [[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 - [[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 - [[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 - [[nodiscard]] auto operator/(const N& operand) const noexcept + [[nodiscard]] auto operator/(N operand) const noexcept { SDL_assert_release(operand != 0); // NOLINT - return _operate(operand, [](const N& a, const N& b) { return a / b; }); + return _operate(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) diff --git a/inc/basic_widgets/core/type/opaque_struct.hpp b/inc/basic_widgets/core/type/opaque_struct.hpp index af5c3e5..67e077e 100644 --- a/inc/basic_widgets/core/type/opaque_struct.hpp +++ b/inc/basic_widgets/core/type/opaque_struct.hpp @@ -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)) {} }; }; } diff --git a/inc/basic_widgets/w/base/input.hpp b/inc/basic_widgets/w/base/input.hpp index 353578a..3d47015 100644 --- a/inc/basic_widgets/w/base/input.hpp +++ b/inc/basic_widgets/w/base/input.hpp @@ -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* + virtual auto color_fg(Color c) -> Input* { _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); } diff --git a/inc/basic_widgets/w/button.hpp b/inc/basic_widgets/w/button.hpp index 1f7296a..56bdd44 100644 --- a/inc/basic_widgets/w/button.hpp +++ b/inc/basic_widgets/w/button.hpp @@ -21,7 +21,8 @@ namespace bwidgets void _handle_focus_change(bool) override {} void _handle_font_change(const std::shared_ptr&) 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&) override; void _handle_rendering() override; @@ -33,9 +34,9 @@ namespace bwidgets static const Color default_color_fg; std::function 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; diff --git a/inc/basic_widgets/w/caption.hpp b/inc/basic_widgets/w/caption.hpp index 0ce59e6..13f6c0b 100644 --- a/inc/basic_widgets/w/caption.hpp +++ b/inc/basic_widgets/w/caption.hpp @@ -21,7 +21,8 @@ namespace bwidgets std::shared_ptr _text_texture {nullptr}; void _handle_font_change(const std::shared_ptr&) 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&) override; void _handle_rendering() override; diff --git a/inc/basic_widgets/w/feat/font_handler.hpp b/inc/basic_widgets/w/feat/font_handler.hpp index 6ab1a2a..6801988 100644 --- a/inc/basic_widgets/w/feat/font_handler.hpp +++ b/inc/basic_widgets/w/feat/font_handler.hpp @@ -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&) = 0; - virtual void _handle_font_color_change(const Color&, const Color&) = 0; + virtual void _handle_font_change(const std::shared_ptr&) = 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); diff --git a/inc/basic_widgets/w/numeric_input.hpp b/inc/basic_widgets/w/numeric_input.hpp index 0e6cd50..a5ec8c9 100644 --- a/inc/basic_widgets/w/numeric_input.hpp +++ b/inc/basic_widgets/w/numeric_input.hpp @@ -30,7 +30,7 @@ namespace bwidgets Input::_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::_handle_font_color_change(fg, bg); _decrement_button.font_color_bg(bg)->font_color_fg(fg); diff --git a/src/core/draw.cpp b/src/core/draw.cpp index 24b73f6..87f67ec 100644 --- a/src/core/draw.cpp +++ b/src/core/draw.cpp @@ -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 + auto filled_circle(Color c, const int resolution, Renderer* r, const int aa_pixels) + -> std::shared_ptr { // clang-format off auto texture {std::make_shared( @@ -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& - pixel_color) + Texture* t, std::function pixel_color) { auto attr = t->attributes(); auto pitch = attr.w * attr.format->BytesPerPixel; diff --git a/src/core/font.cpp b/src/core/font.cpp index 1cfcf83..c9c3ce6 100644 --- a/src/core/font.cpp +++ b/src/core/font.cpp @@ -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 renderer; const char* c_str = str.empty() ? " " : str.c_str(); diff --git a/src/core/renderer.cpp b/src/core/renderer.cpp index 0ddad64..7c76500 100644 --- a/src/core/renderer.cpp +++ b/src/core/renderer.cpp @@ -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( 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(SDL_RenderDrawLine(_data(), a.x, a.y, b.x, b.y)); return this; } -auto Renderer::draw_lines(const std::vector& pts) -> Renderer* +auto Renderer::draw_lines(std::span pts) -> Renderer* { success_or_throw( SDL_RenderDrawLines(_data(), pts.data(), (int)pts.size())); @@ -75,14 +75,14 @@ auto Renderer::draw_lines(const std::vector& pts) -> Renderer* return this; } -auto Renderer::draw_point(const SDL_Point& p) -> Renderer* +auto Renderer::draw_point(SDL_Point p) -> Renderer* { success_or_throw(SDL_RenderDrawPoint(_data(), p.x, p.y)); return this; } -auto Renderer::draw_points(const std::vector& pts) -> Renderer* +auto Renderer::draw_points(std::span pts) -> Renderer* { success_or_throw( 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& rs) -> Renderer* +auto Renderer::draw_rects(std::span rs) -> Renderer* { success_or_throw(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& rs) -> Renderer* +auto Renderer::fill_rects(std::span rs) -> Renderer* { success_or_throw(SDL_RenderFillRects(_data(), rs.data(), (int)rs.size())); diff --git a/src/core/texture.cpp b/src/core/texture.cpp index 6b326f4..723ad11 100644 --- a/src/core/texture.cpp +++ b/src/core/texture.cpp @@ -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(SDL_SetTextureColorMod(_data(), m().r, m().g, m().b)); diff --git a/src/w/button.cpp b/src/w/button.cpp index 24babe2..fbdb8c5 100644 --- a/src/w/button.cpp +++ b/src/w/button.cpp @@ -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& 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& 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}); diff --git a/src/w/caption.cpp b/src/w/caption.cpp index ba571e2..8019a8c 100644 --- a/src/w/caption.cpp +++ b/src/w/caption.cpp @@ -50,7 +50,7 @@ void Caption::_handle_font_change(const std::shared_ptr&) _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))