From 00ef6e312fd2ca48b83783ffeb5537eb7edc0322 Mon Sep 17 00:00:00 2001 From: ValeriiKoniushenko Date: Sun, 8 Oct 2023 14:46:19 +0300 Subject: [PATCH] Added prepare-defender; refactoring --- game/source/VaKon2D.cpp | 65 ++--- lib/core/shapes/include/Widget.h | 100 ++++---- lib/core/shapes/source/TextBox.cpp | 97 ++++---- lib/core/shapes/source/Widget.cpp | 372 ++++++++++++++--------------- 4 files changed, 305 insertions(+), 329 deletions(-) diff --git a/game/source/VaKon2D.cpp b/game/source/VaKon2D.cpp index b903dcca7..022ad66d9 100644 --- a/game/source/VaKon2D.cpp +++ b/game/source/VaKon2D.cpp @@ -43,52 +43,37 @@ #include -void VaKon2D::start() -{ - initCore(); +void VaKon2D::start() { + initCore(); - GetWindow().viewport(0, 0, 800, 600); + GetWindow().viewport(0, 0, 800, 600); - ShaderPack shaderPack; - shaderPack.loadShaders("text", "assets/shaders/text.vert", "assets/shaders/text.frag"); - shaderPack.loadShaders("widget", "assets/shaders/widget.vert", "assets/shaders/widget.frag"); + ShaderPack shaderPack; + shaderPack.loadShaders("text", "assets/shaders/text.vert", "assets/shaders/text.frag"); + 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.setInternalChannel(Gl::Texture::Channel::SRGBA); - texture.setImage(image); - texture.setMagAndMinFilter(Gl::Texture::MagFilter::Linear, Gl::Texture::MinFilter::LinearMipmapLinear); + Texture texture(Gl::Texture::Target::Texture2D, true, true); + Image image("assets/textures/apple.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); - Font font("assets/fonts/Roboto-Medium.ttf"); - TextBox text; - text.setFont(font); - text.setText("Hello\nworld"); - text.setTexture(texture); - // text.setColor({ 255, 0, 0 }); - text.prepare(shaderPack); - // text.setPosition({100.f, 50.f}); - // text.prepare(shaderPack); + while (!GetWindow().shouldClose()) { + GetWindow().clearColor({0.2f, 0.3f, 0.3f}); + GetWindow().clear(GL_COLOR_BUFFER_BIT); - KeyboardInputAction iaWidgetReflector("WidgetReflector", Keyboard::Key::F1); - iaWidgetReflector.setFrequency(KeyboardInputAction::TimeT(100)); - iaWidgetReflector.setIsRepeatable(false); - iaWidgetReflector.onAction.subscribe([]() { getWidgetReflector().toggle(); }); + widget.draw(shaderPack); - while (!GetWindow().shouldClose()) - { - GetWindow().clearColor({0.2f, 0.3f, 0.3f}); - GetWindow().clear(GL_COLOR_BUFFER_BIT); - - text.draw(shaderPack); - - GetUpdateableCollector().updateAll(); - GetWorld().update(); - GetWindow().swapBuffers(); - GetWindow().pollEvent(); - } + GetUpdateableCollector().updateAll(); + GetWorld().update(); + GetWindow().swapBuffers(); + GetWindow().pollEvent(); + } } -void VaKon2D::initCore() -{ - Initer::init({.glfwVersion = {3, 3}, .windowSize = {800, 600}, .title = "My game"}); +void VaKon2D::initCore() { + Initer::init({.glfwVersion = {3, 3}, .windowSize = {800, 600}, .title = "My game"}); } \ No newline at end of file diff --git a/lib/core/shapes/include/Widget.h b/lib/core/shapes/include/Widget.h index e5c896f75..284a69d4d 100644 --- a/lib/core/shapes/include/Widget.h +++ b/lib/core/shapes/include/Widget.h @@ -37,65 +37,73 @@ class Texture; -class Widget : public DrawAble, public JsonPrintable, public Updateable, public Utils::NotCopyableButMovable -{ +class Widget : public DrawAble, public JsonPrintable, public Updateable, public Utils::NotCopyableButMovable { public: - inline static constexpr glm::vec4 borderColor = {1.f, 1.f, 0.f, 1.f}; - inline static constexpr float borderWidth = 0.05f; - inline static constexpr const char* componentName = "widget"; + inline static constexpr glm::vec4 borderColor = {1.f, 1.f, 0.f, 1.f}; + inline static constexpr float borderWidth = 0.05f; + inline static constexpr const char *componentName = "widget"; - Widget(); - Widget(Widget&& other) noexcept; - Widget& operator=(Widget&& other) noexcept; - ~Widget() override; + Widget(); - void draw(ShaderPack& shaderProgram) override; - _NODISCARD std::size_t getVerticesCount() const override; + Widget(Widget &&other) noexcept; - void setTexture(Texture& texture); - _NODISCARD Texture& getTexture(); + Widget &operator=(Widget &&other) noexcept; - void setSize(Utils::FSize2D newSize); - _NODISCARD Utils::FSize2D getSize() const; + ~Widget() override; - void setScale(Utils::FSize2D newScale); - _NODISCARD Utils::FSize2D getScale() const; + void draw(ShaderPack &shaderProgram) override; - _NODISCARD Utils::FRect getRect() const; + _NODISCARD std::size_t getVerticesCount() const override; - void setIsDrawBorder(bool isDraw); - _NODISCARD bool isDrawBorder() const; + void setTexture(Texture &texture); - virtual void prepare(ShaderPack& shader); + _NODISCARD Texture &getTexture(); - void update() override; + void setSize(Utils::FSize2D newSize); - _NODISCARD virtual std::string getComponentName() const; + _NODISCARD Utils::FSize2D getSize() const; - LambdaMulticastDelegate onMouseHover; - LambdaMulticastDelegate onMouseUnHover; - LambdaMulticastDelegate onMouseLeftClick; - LambdaMulticastDelegate onMouseRightClick; - LambdaMulticastDelegate onMouseMiddleClick; - LambdaMulticastDelegate onMouseWheel; - LambdaMulticastDelegate onTextInput; + void setScale(Utils::FSize2D newScale); - _NODISCARD boost::property_tree::ptree toJson() const override; + _NODISCARD Utils::FSize2D getScale() const; + + _NODISCARD Utils::FRect getRect() const; + + void setIsDrawBorder(bool isDraw); + + _NODISCARD bool isDrawBorder() const; + + virtual void prepare(ShaderPack &shader); + + void update() override; + + _NODISCARD virtual std::string getComponentName() const; + + LambdaMulticastDelegate onMouseHover; + LambdaMulticastDelegate onMouseUnHover; + LambdaMulticastDelegate onMouseLeftClick; + LambdaMulticastDelegate onMouseRightClick; + LambdaMulticastDelegate onMouseMiddleClick; + LambdaMulticastDelegate onMouseWheel; + LambdaMulticastDelegate onTextInput; + + _NODISCARD boost::property_tree::ptree toJson() const override; private: - // clang-format off - 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 = 100.f, .height = 100.f}; - Utils::FSize2D scale_ = {.width = 1.f, .height = 1.f}; - bool isDrawBorder_ = false; - bool wasHover_ = false; + // clang-format off + 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 = 100.f, .height = 100.f}; + Utils::FSize2D scale_ = {.width = 1.f, .height = 1.f}; + bool isDrawBorder_ = false; + bool wasHover_ = false; + bool isPrepared = false; }; \ No newline at end of file diff --git a/lib/core/shapes/source/TextBox.cpp b/lib/core/shapes/source/TextBox.cpp index 5a2fcdc4a..4292b49b5 100644 --- a/lib/core/shapes/source/TextBox.cpp +++ b/lib/core/shapes/source/TextBox.cpp @@ -29,74 +29,61 @@ #include -std::string TextBox::getComponentName() const -{ - return componentName; +std::string TextBox::getComponentName() const { + return componentName; } -void TextBox::setText(const std::string& text) -{ - if (!font_) - { - spdlog::get("core")->critical("Can't set text without a font"); - BOOST_ASSERT_MSG(font_, "Can't set text without a font"); - return; - } +void TextBox::setText(const std::string &text) { + if (!font_) { + spdlog::get("core")->critical("Can't set text without a font"); + BOOST_ASSERT_MSG(font_, "Can't set text without a font"); + return; + } - std::vector strings; - boost::split(strings, text, boost::is_any_of("\n")); + std::vector strings; + boost::split(strings, text, boost::is_any_of("\n")); - rows_.resize(strings.size()); + rows_.resize(strings.size()); - float maxWidth = 0; - std::size_t i = 0; - for (auto& lineText : rows_) - { - lineText.setText(strings[i]); - lineText.setFont(*font_); - if (lineText.getTextWidth() > maxWidth) - { - maxWidth = lineText.getTextWidth(); - } + float maxWidth = 0; + std::size_t i = 0; + for (auto &lineText: rows_) { + lineText.setText(strings[i]); + lineText.setFont(*font_); + if (lineText.getTextWidth() > maxWidth) { + maxWidth = lineText.getTextWidth(); + } - if (i != 0) - { - lineText.move({0.f, lineText.getTextHeight()}); - } - ++i; - } + if (i != 0) { + lineText.move({0.f, lineText.getTextHeight()}); + } + ++i; + } - if (!rows_.empty()) - { - 5 setSize({maxWidth, rows_.front().getTextHeight() * rows_.size()}); - } + if (!rows_.empty()) { + setSize({maxWidth, rows_.front().getTextHeight() * rows_.size()}); + } } -void TextBox::setFont(Font& font) -{ - font_ = &font; - for (auto& lineText : rows_) - { - lineText.setFont(font); - } +void TextBox::setFont(Font &font) { + font_ = &font; + for (auto &lineText: rows_) { + lineText.setFont(font); + } } -void TextBox::prepare(ShaderPack& shader) -{ - Widget::prepare(shader); +void TextBox::prepare(ShaderPack &shader) { + Widget::prepare(shader); - for (auto& lineText : rows_) - { - lineText.prepare(shader); - } + for (auto &lineText: rows_) { + lineText.prepare(shader); + } } -void TextBox::draw(ShaderPack& shaderProgram) -{ - Widget::draw(shaderProgram); +void TextBox::draw(ShaderPack &shaderProgram) { + Widget::draw(shaderProgram); - for (auto& lineText : rows_) - { - lineText.draw(shaderProgram); - } + for (auto &lineText: rows_) { + lineText.draw(shaderProgram); + } } diff --git a/lib/core/shapes/source/Widget.cpp b/lib/core/shapes/source/Widget.cpp index 4e54c7f3d..bcd47e822 100644 --- a/lib/core/shapes/source/Widget.cpp +++ b/lib/core/shapes/source/Widget.cpp @@ -33,228 +33,224 @@ #include #include -Widget::Widget() -{ - getWidgetCollector().add(this); +Widget::Widget() { + getWidgetCollector().add(this); } -Widget::~Widget() -{ - getWidgetCollector().remove(this); +Widget::~Widget() { + getWidgetCollector().remove(this); } -void Widget::draw(ShaderPack& shaderPack) -{ - auto& shaderProgram = shaderPack["widget"]; - vao_.bind(); - if (texture_) - { - texture_->bind(); - } - else - { - Gl::Texture::bind(Gl::Texture::Target::Texture2D, 0); - } - - glm::mat4 trans = glm::mat4(1.0f); - trans = glm::translate(trans, glm::vec3(position_ / glm::vec2(static_cast(GetWindow().getSize().width) / 2.f, - static_cast(GetWindow().getSize().height) / 2.f), - 0.f)); - trans = glm::rotate(trans, rotation_, glm::vec3(0.0f, 0.0f, 1.0f)); - trans[3][1] = -trans[3][1]; - - shaderProgram.use(); - shaderProgram.uniform("uHasTexture", static_cast(texture_)); - shaderProgram.uniform("uTransform", false, trans); - shaderProgram.uniform( - "uResolution", static_cast(GetWindow().getSize().width), static_cast(GetWindow().getSize().height)); - shaderProgram.uniform("uGamma", shaderProgram.lightning.gamma); - shaderProgram.uniform("uBrightness", shaderProgram.lightning.brightness); - shaderProgram.uniform("uContrast", shaderProgram.lightning.contrast); - shaderProgram.uniform("uSaturation", shaderProgram.lightning.saturation); - shaderProgram.uniform("uBorderColor", borderColor.r, borderColor.g, borderColor.b, borderColor.a); - shaderProgram.uniform("uBorderWidth", borderWidth); - shaderProgram.uniform("uIsDrawBorder", isDrawBorder_); - - DrawAble::draw(shaderPack); +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."); + } + + auto &shaderProgram = shaderPack["widget"]; + vao_.bind(); + if (texture_) { + texture_->bind(); + } else { + Gl::Texture::bind(Gl::Texture::Target::Texture2D, 0); + } + + glm::mat4 trans = glm::mat4(1.0f); + trans = glm::translate(trans, glm::vec3(position_ / glm::vec2(static_cast(GetWindow().getSize().width) / 2.f, + static_cast(GetWindow().getSize().height) / + 2.f), + 0.f)); + trans = glm::rotate(trans, rotation_, glm::vec3(0.0f, 0.0f, 1.0f)); + trans[3][1] = -trans[3][1]; + + shaderProgram.use(); + shaderProgram.uniform("uHasTexture", static_cast(texture_)); + shaderProgram.uniform("uTransform", false, trans); + shaderProgram.uniform( + "uResolution", static_cast(GetWindow().getSize().width), + static_cast(GetWindow().getSize().height)); + shaderProgram.uniform("uGamma", shaderProgram.lightning.gamma); + shaderProgram.uniform("uBrightness", shaderProgram.lightning.brightness); + shaderProgram.uniform("uContrast", shaderProgram.lightning.contrast); + shaderProgram.uniform("uSaturation", shaderProgram.lightning.saturation); + shaderProgram.uniform("uBorderColor", borderColor.r, borderColor.g, borderColor.b, borderColor.a); + shaderProgram.uniform("uBorderWidth", borderWidth); + shaderProgram.uniform("uIsDrawBorder", isDrawBorder_); + + DrawAble::draw(shaderPack); } -std::size_t Widget::getVerticesCount() const -{ - constexpr std::size_t countOfParts = 4; - return templateVertices_.size() / countOfParts; +std::size_t Widget::getVerticesCount() const { + constexpr std::size_t countOfParts = 4; + return templateVertices_.size() / countOfParts; } -void Widget::setTexture(Texture& texture) -{ - texture_ = &texture; +void Widget::setTexture(Texture &texture) { + isPrepared = false; + texture_ = &texture; } -Texture& Widget::getTexture() -{ - return *texture_; +Texture &Widget::getTexture() { + return *texture_; } -void Widget::prepare(ShaderPack& shader) -{ - shader["widget"].use(); - - if (!vbo_.isGenerated()) - { - vbo_.generate(); - } - - std::vector vertices = templateVertices_; - vertices.at(5) *= size_.height * 2.f * scale_.height; - vertices.at(8) *= size_.width * 2.f * scale_.width; - vertices.at(12) *= size_.width * 2.f * scale_.width; - vertices.at(13) *= size_.height * 2.f * scale_.height; - - vbo_.bind(); - vbo_.data(vertices); - - if (!vao_.isGenerated()) - { - vao_.generate(); - } - vao_.bind(); - Gl::Vao::vertexAttribPointer(0, 2, Gl::Type::Float, false, 4 * sizeof(float), nullptr); - Gl::Vao::enableVertexAttribArray(0); - - Gl::Vao::vertexAttribPointer(1, 2, Gl::Type::Float, false, 4 * sizeof(float), reinterpret_cast(2 * sizeof(float))); - Gl::Vao::enableVertexAttribArray(1); - - if (texture_) - { - texture_->bind(); - texture_->loadToGpu(); - } +void Widget::prepare(ShaderPack &shader) { + shader["widget"].use(); + + if (!vbo_.isGenerated()) { + vbo_.generate(); + } + + std::vector vertices = templateVertices_; + vertices.at(5) *= size_.height * 2.f * scale_.height; + vertices.at(8) *= size_.width * 2.f * scale_.width; + vertices.at(12) *= size_.width * 2.f * scale_.width; + vertices.at(13) *= size_.height * 2.f * scale_.height; + + vbo_.bind(); + vbo_.data(vertices); + + if (!vao_.isGenerated()) { + vao_.generate(); + } + vao_.bind(); + Gl::Vao::vertexAttribPointer(0, 2, Gl::Type::Float, false, 4 * sizeof(float), nullptr); + Gl::Vao::enableVertexAttribArray(0); + + Gl::Vao::vertexAttribPointer(1, 2, Gl::Type::Float, false, 4 * sizeof(float), + reinterpret_cast(2 * sizeof(float))); + Gl::Vao::enableVertexAttribArray(1); + + if (texture_) { + texture_->bind(); + texture_->loadToGpu(); + } + + isPrepared = true; } -void Widget::setSize(Utils::FSize2D newSize) -{ - size_ = newSize; +void Widget::setSize(Utils::FSize2D newSize) { + isPrepared = false; + size_ = newSize; } -Utils::FSize2D Widget::getSize() const -{ - return size_; +Utils::FSize2D Widget::getSize() const { + return size_; } -void Widget::setScale(Utils::FSize2D newScale) -{ - scale_ = newScale; +void Widget::setScale(Utils::FSize2D newScale) { + isPrepared = false; + scale_ = newScale; } -Utils::FSize2D Widget::getScale() const -{ - return scale_; +Utils::FSize2D Widget::getScale() const { + return scale_; } -void Widget::update() -{ - if (getRect().isCollision(Mouse::getPosition(GetWindow()))) - { - onMouseHover.trigger(); - - if (Mouse::isKeyPressed(Mouse::Key::Left)) - { - onMouseLeftClick.trigger(); - } - if (Mouse::isKeyPressed(Mouse::Key::Right)) - { - onMouseRightClick.trigger(); - } - if (Mouse::isKeyPressed(Mouse::Key::Middle)) - { - onMouseMiddleClick.trigger(); - } - try - { - if (get(GetWorldVariables()["mouse-wheel-y"]) != 0.) - { - onMouseWheel.trigger(get(GetWorldVariables()["mouse-wheel-y"])); - } - } - catch (...) - { - } - try - { - if (get(GetWorldVariables()["inputted-text"]) != 0.) - { - onTextInput.trigger(get(GetWorldVariables()["inputted-text"])); - } - } - catch (...) - { - } - - wasHover_ = true; - } - else if (wasHover_) - { - onMouseUnHover.trigger(); - wasHover_ = false; - } +void Widget::update() { + if (getRect().isCollision(Mouse::getPosition(GetWindow()))) { + onMouseHover.trigger(); + + if (Mouse::isKeyPressed(Mouse::Key::Left)) { + onMouseLeftClick.trigger(); + } + if (Mouse::isKeyPressed(Mouse::Key::Right)) { + onMouseRightClick.trigger(); + } + if (Mouse::isKeyPressed(Mouse::Key::Middle)) { + onMouseMiddleClick.trigger(); + } + try { + if (get(GetWorldVariables()["mouse-wheel-y"]) != 0.) { + onMouseWheel.trigger(get(GetWorldVariables()["mouse-wheel-y"])); + } + } + catch (...) { + } + try { + if (get(GetWorldVariables()["inputted-text"]) != 0.) { + onTextInput.trigger(get(GetWorldVariables()["inputted-text"])); + } + } + catch (...) { + } + + wasHover_ = true; + } else if (wasHover_) { + onMouseUnHover.trigger(); + wasHover_ = false; + } } -Utils::FRect Widget::getRect() const -{ - return {position_, size_}; +Utils::FRect Widget::getRect() const { + return {position_, size_}; } -void Widget::setIsDrawBorder(bool isDraw) -{ - isDrawBorder_ = isDraw; +void Widget::setIsDrawBorder(bool isDraw) { + isDrawBorder_ = isDraw; } -bool Widget::isDrawBorder() const -{ - return isDrawBorder_; +bool Widget::isDrawBorder() const { + return isDrawBorder_; } -boost::property_tree::ptree Widget::toJson() const -{ - boost::property_tree::ptree ptree; - ptree.put("component", getComponentName()); - ptree.put("verticies-count", templateVertices_.size()); - ptree.put("vbo", vbo_.getId()); - ptree.put("vao", vao_.getId()); - ptree.put("width", size_.width); - ptree.put("height", size_.height); - ptree.put("scale-width", scale_.width); - ptree.put("scale-height", scale_.height); - ptree.put("position-x", position_.x); - ptree.put("position-y", position_.y); - if (texture_) - { - boost::property_tree::ptree texture; - texture.put("name", texture_->getName()); - texture.put("mag-filter", Gl::Texture::magFilterToString(texture_->getMagFilter())); - texture.put("min-filter", Gl::Texture::minFilterToString(texture_->getMinFilter())); - texture.put("width", texture_->getImage()->getWidth()); - texture.put("height", texture_->getImage()->getHeight()); - texture.put("channel", Image::channelToString(texture_->getImage()->getChannel())); - texture.put("internal-channel", Gl::Texture::channelToString(texture_->getImage()->getInternalChannel())); - - ptree.add_child("texture", texture); - } - - return ptree; +boost::property_tree::ptree Widget::toJson() const { + boost::property_tree::ptree ptree; + ptree.put("component", getComponentName()); + ptree.put("verticies-count", templateVertices_.size()); + ptree.put("vbo", vbo_.getId()); + ptree.put("vao", vao_.getId()); + ptree.put("width", size_.width); + ptree.put("height", size_.height); + ptree.put("scale-width", scale_.width); + ptree.put("scale-height", scale_.height); + ptree.put("position-x", position_.x); + ptree.put("position-y", position_.y); + if (texture_) { + boost::property_tree::ptree texture; + texture.put("name", texture_->getName()); + texture.put("mag-filter", Gl::Texture::magFilterToString(texture_->getMagFilter())); + texture.put("min-filter", Gl::Texture::minFilterToString(texture_->getMinFilter())); + texture.put("width", texture_->getImage()->getWidth()); + texture.put("height", texture_->getImage()->getHeight()); + texture.put("channel", Image::channelToString(texture_->getImage()->getChannel())); + texture.put("internal-channel", Gl::Texture::channelToString(texture_->getImage()->getInternalChannel())); + + ptree.add_child("texture", texture); + } + + return ptree; } -std::string Widget::getComponentName() const -{ - return componentName; +std::string Widget::getComponentName() const { + return componentName; } -Widget::Widget(Widget&& other) noexcept -{ - *this = std::move(other); +Widget::Widget(Widget &&other) noexcept { + isPrepared = false; + *this = std::move(other); } -Widget& Widget::operator=(Widget&& other) noexcept -{ - return *this; +Widget &Widget::operator=(Widget &&other) noexcept { + isPrepared = false; + + texture_ = other.texture_; + vbo_ = other.vbo_; + vao_ = other.vao_; + size_ = other.size_; + scale_ = other.scale_; + isDrawBorder_ = other.isDrawBorder_; + wasHover_ = other.wasHover_; + isPrepared = other.isPrepared; + + other.isPrepared = false; + other.texture_ = {}; + other.vbo_ = {}; + other.vao_ = {}; + other.size_ = {}; + other.scale_ = {}; + other.isDrawBorder_ = {}; + other.wasHover_ = {}; + other.isPrepared = {}; + + return *this; }