Skip to content

Commit

Permalink
Added calculateFitTextureSize; refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriiKoniushenko committed Oct 8, 2023
1 parent 00ef6e3 commit f911b46
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Binary file added game/assets/textures/clock.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions game/source/VaKon2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "ShaderPack.h"
#include "TextBox.h"
#include "UtilsFunctions.h"
#include "Rect.h"

#include <iostream>

Expand All @@ -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});
Expand Down
5 changes: 5 additions & 0 deletions lib/core/shapes/include/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> &rect);

void calculateFitTextureSize();

private:
// clang-format off
inline static const std::vector<float> templateVertices_ = {
Expand All @@ -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_;
Expand Down
20 changes: 19 additions & 1 deletion lib/core/shapes/source/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -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_;
Expand All @@ -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_ = {};
Expand All @@ -254,3 +260,15 @@ Widget &Widget::operator=(Widget &&other) noexcept {

return *this;
}

void Widget::setTextureRect(const Utils::Rect<int> &rect) {
textureRect_ = rect;
}

void Widget::calculateFitTextureSize() {
if (texture_ && texture_->getImage()) {
auto size = texture_->getImage()->getSize();
size_.width = size.x;
size_.height = size.y;
}
}

0 comments on commit f911b46

Please sign in to comment.