From 56c6f3ad609cbb2e7fad2e0d303c9b9bb0c23577 Mon Sep 17 00:00:00 2001 From: ValeriiKoniushenko Date: Mon, 10 Jul 2023 20:43:04 +0300 Subject: [PATCH] Added an abilty to resize a rectangle\sprite and set scale for it --- lib/core/shapes/include/Rectangle.h | 20 +++++++++++++------ lib/core/shapes/source/Rectangle.cpp | 29 +++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/core/shapes/include/Rectangle.h b/lib/core/shapes/include/Rectangle.h index c8ce525c8..f99103e21 100644 --- a/lib/core/shapes/include/Rectangle.h +++ b/lib/core/shapes/include/Rectangle.h @@ -21,6 +21,7 @@ // SOFTWARE. #include "DrawAble.h" +#include "Size.h" #include "Vao.h" #include "Vbo.h" @@ -37,19 +38,26 @@ class Rectangle : public DrawAble void setTexture(Texture& texture); _NODISCARD Texture& getTexture(); + void setSize(Utils::FSize2D newSize); + _NODISCARD Utils::FSize2D getSize() const; + + void setScale(Utils::FSize2D newScale); + _NODISCARD Utils::FSize2D getScale() const; + void prepare(); private: // clang-format off - const std::vector vertices = { - 0.f, 0.f, 0.f, 0.f, - 500.f, 0.f, 1.f, 0.f, - 0.f, 500.f, 0.f, 1.f, - 500.f, 500.f, 1.f, 1.f, + inline static const std::vector templateVertices_ = { + 0.f, 0.f, 0.f, 1.f, + 0.f,-1.f, 0.f, 0.f, + 1.f, 0.f, 1.f, 1.f, + 1.f,-1.f, 1.f, 0.f, }; // clang-format on - Texture* texture_{}; Vbo vbo_; Vao vao_; + Utils::FSize2D size_ = {.width = 500.f, .height = 500.f}; + Utils::FSize2D scale_ = {.width = 1.f, .height = 1.f}; }; \ No newline at end of file diff --git a/lib/core/shapes/source/Rectangle.cpp b/lib/core/shapes/source/Rectangle.cpp index 053993b22..098f52bea 100644 --- a/lib/core/shapes/source/Rectangle.cpp +++ b/lib/core/shapes/source/Rectangle.cpp @@ -53,7 +53,7 @@ void Rectangle::draw(ShaderProgram& shaderProgram) std::size_t Rectangle::getVerticesCount() const { constexpr std::size_t countOfParts = 4; - return vertices.size() / countOfParts; + return templateVertices_.size() / countOfParts; } void Rectangle::setTexture(Texture& texture) @@ -72,6 +72,13 @@ void Rectangle::prepare() { vbo_.generate(); } + + std::vector vertices = templateVertices_; + vertices.at(5) *= size_.height * scale_.height; + vertices.at(8) *= size_.width * scale_.width; + vertices.at(12) *= size_.width * scale_.width; + vertices.at(13) *= size_.height * scale_.height; + vbo_.bind(); vbo_.data(vertices); @@ -91,3 +98,23 @@ void Rectangle::prepare() texture_->loadToGpu(); } } + +void Rectangle::setSize(Utils::FSize2D newSize) +{ + size_ = newSize; +} + +Utils::FSize2D Rectangle::getSize() const +{ + return size_; +} + +void Rectangle::setScale(Utils::FSize2D newScale) +{ + scale_ = newScale; +} + +Utils::FSize2D Rectangle::getScale() const +{ + return scale_; +}