OOP and TGUI

  • 1 Replies
  • 65 Views
OOP and TGUI
« on: 17 September 2019, 11:58:45 »
I noticed that all examples use only "all in main" style. Is there any reason why it don't write in OOP-style?
Minimal example(it took for me a while before I realize how create Gui inside class):

Code: [Select]
#include <TGUI/TGUI.hpp>

class MyFrame
{
public:
    MyFrame(int argc, char* argv[]);
    void main();

protected:
    tgui::Button::Ptr m_Button;

private:
    sf::RenderWindow window;
    tgui::Gui gui;
};

MyFrame::MyFrame(int argc, char* argv[])
{
    window.create(sf::VideoMode(800, 600), "TGUI window");
    gui.setTarget(window);
    auto panel = tgui::ScrollablePanel::create();
    m_Button = tgui::Button::create("Press");
    panel->add(m_Button);
    gui.add(panel);
}

void MyFrame::main()
{
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            else if (event.type == sf::Event::Resized)
            {
                window.setView(sf::View(sf::FloatRect(0.f, 0.f, static_cast<float>(event.size.width), static_cast<float>(event.size.height))));
                gui.setView(window.getView());
            }
            gui.handleEvent(event);
        }

        window.clear();
        gui.draw();
        window.display();
    }
}

int main(int argc, char* argv[])
{
    MyFrame f(argc, argv);
    f.main();
}

*

texus

  • *****
  • 1475
    • View Profile
    • Texus's Blog
Re: OOP and TGUI
« Reply #1 on: 17 September 2019, 22:35:28 »
I don't do it because i don't want to complicate the code. It doesn't add much complexity, but I feel like the minimal examples should be as straightforward as possible: just a main function with the minimum required code in them. By adding classes you are adding stuff to the code that is irrelevant to the example.

I'm not against OOP for larger examples, but for the really simple examples I don't really feel that it is needed.

It might be a good idea to have at least one example with a class though, it could be useful to demonstrate how to initialize the gui in a class and how to connect member functions. These are cases that people likely face but are currently not covered in the examples.

Calling setTarget is one way to do it, but even in a class you can still use the constructor by using the initializer list:
MyFrame::MyFrame(int argc, char* argv[]) :
    window(sf::VideoMode(800, 600), "TGUI window"),
    gui(window)
{