Skip to content

Commit

Permalink
Added prepare-defender; refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeriiKoniushenko committed Oct 8, 2023
1 parent 96cd9dd commit 00ef6e3
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 329 deletions.
65 changes: 25 additions & 40 deletions game/source/VaKon2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,52 +43,37 @@

#include <iostream>

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"});
}
100 changes: 54 additions & 46 deletions lib/core/shapes/include/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> onMouseHover;
LambdaMulticastDelegate<void()> onMouseUnHover;
LambdaMulticastDelegate<void()> onMouseLeftClick;
LambdaMulticastDelegate<void()> onMouseRightClick;
LambdaMulticastDelegate<void()> onMouseMiddleClick;
LambdaMulticastDelegate<void(double)> onMouseWheel;
LambdaMulticastDelegate<void(unsigned int)> 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<void()> onMouseHover;
LambdaMulticastDelegate<void()> onMouseUnHover;
LambdaMulticastDelegate<void()> onMouseLeftClick;
LambdaMulticastDelegate<void()> onMouseRightClick;
LambdaMulticastDelegate<void()> onMouseMiddleClick;
LambdaMulticastDelegate<void(double)> onMouseWheel;
LambdaMulticastDelegate<void(unsigned int)> onTextInput;

_NODISCARD boost::property_tree::ptree toJson() const override;

private:
// clang-format off
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 = 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<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 = 100.f, .height = 100.f};
Utils::FSize2D scale_ = {.width = 1.f, .height = 1.f};
bool isDrawBorder_ = false;
bool wasHover_ = false;
bool isPrepared = false;
};
97 changes: 42 additions & 55 deletions lib/core/shapes/source/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,74 +29,61 @@

#include <boost/algorithm/string.hpp>

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<std::string> strings;
boost::split(strings, text, boost::is_any_of("\n"));
std::vector<std::string> 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);
}
}
Loading

0 comments on commit 00ef6e3

Please sign in to comment.