From f911b469699205493aeb19936f76cb5aea5d73ee Mon Sep 17 00:00:00 2001 From: ValeriiKoniushenko Date: Sun, 8 Oct 2023 15:17:53 +0300 Subject: [PATCH] Added calculateFitTextureSize; refactoring --- game/assets/textures/clock.png | Bin 0 -> 4320 bytes game/source/VaKon2D.cpp | 8 ++++++-- lib/core/shapes/include/Widget.h | 5 +++++ lib/core/shapes/source/Widget.cpp | 20 +++++++++++++++++++- 4 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 game/assets/textures/clock.png diff --git a/game/assets/textures/clock.png b/game/assets/textures/clock.png new file mode 100644 index 0000000000000000000000000000000000000000..0308086611c1de018c7d2e7ad35952e7418acc84 GIT binary patch literal 4320 zcmb_g2~<;88V)$1vbj}Js1Spq6tjnf1S651N`!(UNFjum7YJk{2@v+hUD+(Hh^V*~ zuvisF*<66yvZ)|w5d;K_0jwzC!oJK6B6X(iOi$0ebCTTm-gp21`@es==j5QvHhXOi zJq-i`q3!5kNJm?1GM-8X zHD?JAG$fnBgg$qf#kUGaIync|_bA!Sq0QIN74V8G!0W8NR=@s~W$?hgxqfzkg`CGXux zMO;}JTLN$K2ipD5D#ZRNd66&_mV88C%$BghEP^FcI*Kb136N~Dn9qaQ%2=_XLe6Z9 z&t(TG6Cr;qsz3E3WrzNEi69aVqEH|b8c&5_G|`V_hNf~LGDtUvOf|z(DG=W5gT%j+ zh=8Zz$TR}^LwEcgiDu=80@y--7))|3>hA~h*XsX0%ijm{AFclX3kItk;O{FL`=3QV z^X{Wz4+>`H5ja7V%ekWq+{_(*un_1Z0Y}@iE=&VBMSUG@wrrQ5czKe=_fo9dpYD? zcy*~!QrC-d=Q(_{?~~;$YOJaKLFe=1r7uowFlpbLVnNAY-Luv^?d`C` z(GyV}qo=y653yonpA`L4`;F;>HG39YeTn5m?m-(gd)1acsgGtXO2vf6=f6xSyZq(% z7E7%FBsVv=cx)4;#&zQY6aU1hF(}{0tA~SXRH&xvX|1>0f84GsS(@uGeO5<}p~QPE zDa3!nV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**U3)Q$5X1k-Gm0N=DPLVqH7rW4e#5d3#GP2V zI`G#~dG3Xx1BP+F?|PJGtA)FcD$SN10zKZep%AaVZb}75d-(MQ&H6nmBr{vx;Rno+Q z=^a@tmHV_GnRZ5;l;QTRYfP=DOwE-owC#tmk?Ho+Ee!z?;_tOX_3;vJwquN@IyHi%Uo}DH&4w~txwxi z8n)kv2ac8V1!J$>Pap2W6AU0vIaF{2REb!ey98o!qIk{ K%_S?}gnt1>%`z_l literal 0 HcmV?d00001 diff --git a/game/source/VaKon2D.cpp b/game/source/VaKon2D.cpp index 022ad66d9..dd20a65b7 100644 --- a/game/source/VaKon2D.cpp +++ b/game/source/VaKon2D.cpp @@ -40,6 +40,7 @@ #include "ShaderPack.h" #include "TextBox.h" #include "UtilsFunctions.h" +#include "Rect.h" #include @@ -53,13 +54,16 @@ void VaKon2D::start() { shaderPack.loadShaders("widget", "assets/shaders/widget.vert", "assets/shaders/widget.frag"); Texture texture(Gl::Texture::Target::Texture2D, true, true); - Image image("assets/textures/apple.png"); + Image image("assets/textures/clock.png"); image.setInternalChannel(Gl::Texture::Channel::SRGBA); texture.setImage(image); texture.setMagAndMinFilter(Gl::Texture::MagFilter::Linear, Gl::Texture::MinFilter::LinearMipmapLinear); + Widget widget; widget.setTexture(texture); - widget.prepare(shaderPack); + widget.calculateFitTextureSize(); + widget.setTextureRect(Utils::IRect{{320, 0}, + {32, 32}}); while (!GetWindow().shouldClose()) { GetWindow().clearColor({0.2f, 0.3f, 0.3f}); diff --git a/lib/core/shapes/include/Widget.h b/lib/core/shapes/include/Widget.h index 284a69d4d..34be77b61 100644 --- a/lib/core/shapes/include/Widget.h +++ b/lib/core/shapes/include/Widget.h @@ -89,6 +89,10 @@ class Widget : public DrawAble, public JsonPrintable, public Updateable, public _NODISCARD boost::property_tree::ptree toJson() const override; + void setTextureRect(const Utils::Rect &rect); + + void calculateFitTextureSize(); + private: // clang-format off inline static const std::vector templateVertices_ = { @@ -98,6 +102,7 @@ class Widget : public DrawAble, public JsonPrintable, public Updateable, public 1.f, -1.f, 1.f, 0.f, }; // clang-format on + Utils::IRect textureRect_; Texture *texture_{}; Vbo vbo_; Vao vao_; diff --git a/lib/core/shapes/source/Widget.cpp b/lib/core/shapes/source/Widget.cpp index bcd47e822..5db299994 100644 --- a/lib/core/shapes/source/Widget.cpp +++ b/lib/core/shapes/source/Widget.cpp @@ -43,7 +43,11 @@ Widget::~Widget() { void Widget::draw(ShaderPack &shaderPack) { if (!isPrepared) { - throw std::runtime_error("The widget is not prepared. Use Widget::prepare before Widget::draw to resolve it."); + prepare(shaderPack); + if (!isPrepared) { + throw std::runtime_error( + "The widget is not prepared. Use Widget::prepare before Widget::draw to resolve it."); + } } auto &shaderProgram = shaderPack["widget"]; @@ -233,6 +237,7 @@ Widget::Widget(Widget &&other) noexcept { Widget &Widget::operator=(Widget &&other) noexcept { isPrepared = false; + textureRect_ = other.textureRect_; texture_ = other.texture_; vbo_ = other.vbo_; vao_ = other.vao_; @@ -242,6 +247,7 @@ Widget &Widget::operator=(Widget &&other) noexcept { wasHover_ = other.wasHover_; isPrepared = other.isPrepared; + other.textureRect_ = {}; other.isPrepared = false; other.texture_ = {}; other.vbo_ = {}; @@ -254,3 +260,15 @@ Widget &Widget::operator=(Widget &&other) noexcept { return *this; } + +void Widget::setTextureRect(const Utils::Rect &rect) { + textureRect_ = rect; +} + +void Widget::calculateFitTextureSize() { + if (texture_ && texture_->getImage()) { + auto size = texture_->getImage()->getSize(); + size_.width = size.x; + size_.height = size.y; + } +}