Skip to content

Commit

Permalink
Added an abilty to resize a rectangle\sprite and set scale for it
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriiKoniushenko committed Jul 10, 2023
1 parent 5d26463 commit 56c6f3a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
20 changes: 14 additions & 6 deletions lib/core/shapes/include/Rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// SOFTWARE.

#include "DrawAble.h"
#include "Size.h"
#include "Vao.h"
#include "Vbo.h"

Expand All @@ -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<float> 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<float> 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};
};
29 changes: 28 additions & 1 deletion lib/core/shapes/source/Rectangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -72,6 +72,13 @@ void Rectangle::prepare()
{
vbo_.generate();
}

std::vector<float> 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);

Expand All @@ -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_;
}

0 comments on commit 56c6f3a

Please sign in to comment.