From 486a8b24aa831655050861ecf154f24d2c0e782d Mon Sep 17 00:00:00 2001 From: lisyarus Date: Thu, 4 Mar 2021 10:02:46 +0300 Subject: [PATCH] UI painter: add draw_subimage & don't restrict to integer texcoords --- libs/ui/include/psemek/ui/painter.hpp | 1 + libs/ui/include/psemek/ui/painter_impl.hpp | 1 + libs/ui/source/painter_impl.cpp | 35 +++++++++++++++------- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/libs/ui/include/psemek/ui/painter.hpp b/libs/ui/include/psemek/ui/painter.hpp index 9cd04ddf..6a50cb3d 100644 --- a/libs/ui/include/psemek/ui/painter.hpp +++ b/libs/ui/include/psemek/ui/painter.hpp @@ -13,6 +13,7 @@ namespace psemek::ui virtual void draw_rect(geom::box const & rect, gfx::color_rgba const & color) = 0; virtual void draw_glyph(font const & f, char32_t c, geom::box const & rect, gfx::color_rgba const & color) = 0; virtual void draw_image(geom::box const & rect, gfx::texture_2d const & tex, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0; + virtual void draw_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color = {0, 0, 0, 0}) = 0; virtual ~painter() {} }; diff --git a/libs/ui/include/psemek/ui/painter_impl.hpp b/libs/ui/include/psemek/ui/painter_impl.hpp index 0293dff3..34c33b1b 100644 --- a/libs/ui/include/psemek/ui/painter_impl.hpp +++ b/libs/ui/include/psemek/ui/painter_impl.hpp @@ -18,6 +18,7 @@ namespace psemek::ui void draw_rect(geom::box const & rect, gfx::color_rgba const & color) override; void draw_glyph(font const & f, char32_t c, geom::box const & rect, gfx::color_rgba const & color) override; void draw_image(geom::box const & rect, gfx::texture_2d const & tex, gfx::color_rgba const & color) override; + void draw_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color = {0, 0, 0, 0}) override; void render(geom::matrix const & transform); diff --git a/libs/ui/source/painter_impl.cpp b/libs/ui/source/painter_impl.cpp index aecf0295..04aefeec 100644 --- a/libs/ui/source/painter_impl.cpp +++ b/libs/ui/source/painter_impl.cpp @@ -139,19 +139,19 @@ void main() return true; } - struct textured_vertex + struct bitmap_text_vertex { geom::point position; std::uint32_t depth; gfx::color_rgba color; geom::vector texcoords; }; - static_assert(sizeof(textured_vertex) == 20); + static_assert(sizeof(bitmap_text_vertex) == 20); struct bitmap_text_batch { gfx::texture_2d const * atlas = nullptr; - std::vector vertices{}; + std::vector vertices{}; std::vector indices{}; }; @@ -160,6 +160,15 @@ void main() return b1.atlas == b2.atlas; } + struct textured_vertex + { + geom::point position; + std::uint32_t depth; + gfx::color_rgba color; + geom::point texcoords; + }; + static_assert(sizeof(textured_vertex) == 24); + struct textured_batch { gfx::texture_2d const * texture = nullptr; @@ -216,7 +225,7 @@ void main() colored_mesh.setup, std::uint32_t, gfx::normalized>(); bitmap_text_mesh.setup, std::uint32_t, gfx::normalized, geom::vector>(); - textured_mesh.setup, std::uint32_t, gfx::normalized, geom::vector>(); + textured_mesh.setup, std::uint32_t, gfx::normalized, geom::vector>(); } painter_impl::painter_impl() @@ -270,18 +279,24 @@ void main() } void painter_impl::draw_image(geom::box const & rect, gfx::texture_2d const & tex, gfx::color_rgba const & color) + { + geom::box part; + part[0] = {0.f, tex.width()}; + part[1] = {0.f, tex.height()}; + draw_subimage(rect, tex, part, color); + } + + void painter_impl::draw_subimage(geom::box const & rect, gfx::texture_2d const & tex, geom::box const & part, gfx::color_rgba const & color) { auto & batch = impl().batch(textured_batch{&tex}); std::uint32_t const depth = impl().depth++; std::uint32_t const base = batch.vertices.size(); - auto const size = tex.size(); - - batch.vertices.push_back({rect.corner(0.f, 0.f), depth, color, {0, 0}}); - batch.vertices.push_back({rect.corner(1.f, 0.f), depth, color, {size[0], 0}}); - batch.vertices.push_back({rect.corner(0.f, 1.f), depth, color, {0, size[1]}}); - batch.vertices.push_back({rect.corner(1.f, 1.f), depth, color, {size[0], size[1]}}); + batch.vertices.push_back({rect.corner(0.f, 0.f), depth, color, part.corner(0.f, 0.f)}); + batch.vertices.push_back({rect.corner(1.f, 0.f), depth, color, part.corner(1.f, 0.f)}); + batch.vertices.push_back({rect.corner(0.f, 1.f), depth, color, part.corner(0.f, 1.f)}); + batch.vertices.push_back({rect.corner(1.f, 1.f), depth, color, part.corner(1.f, 1.f)}); batch.indices.insert(batch.indices.end(), {base + 0, base + 1, base + 2, base + 2, base + 1, base + 3}); }