Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Application class (using sfml, sfgui and thor)  (Read 6986 times)

0 Members and 1 Guest are viewing this topic.

Kojay

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Application class (using sfml, sfgui and thor)
« on: January 02, 2014, 09:19:17 pm »
Hello,

in several projects now I have used these 3 libraries as a sort of lightweight gui framework, with the emphasis on graphical drawing on a canvas. The boilerplate code is contained within the following class:

#include <SFML/Window.hpp>
#include <Thor/Input/ActionMap.hpp>
#include <Thor/Input/EventSystem.hpp>
#include <SFGUI/SFGUI.hpp>

typedef thor::ActionMap<std::string> ActionMap;
typedef ActionMap::CallbackSystem CallbackSystem;

class Application
{
public:
    Application();
    void run();

private:
    sfg::SFGUI sfgui;
    sf::Window window;
    sfg::Desktop desktop;
    sf::Image icon;
    ActionMap actions;
    CallbackSystem system;
};
 
#include "Application.hpp"

Application::Application():
    window(sf::VideoMode(800, 600), "Application")
{
    if (icon.loadFromFile(""))
        window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());

    window.setFramerateLimit(60);

    actions["Close"] = thor::Action(sf::Event::Closed);
    system.connect("Close", std::bind(&sf::Window::close, &window));
}

void Application::run()
{
    sf::Clock clock;

    while (window.isOpen()){

        actions.clearEvents();

        sf::Event event;
        while (window.pollEvent(event)){
            actions.pushEvent(event);
            desktop.HandleEvent(event);
        }

        actions.invokeCallbacks(system, &window);
        desktop.Update(clock.restart().asSeconds());

        sfgui.Display(window);
        window.display();
    }
}
 

Note that sfml drawing can be done on a sfg::Canvas, therefore a sf::RenderWindow is not required.
Comments welcome.
« Last Edit: January 02, 2014, 09:24:11 pm by Kojay »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Application class (using sfml, sfgui and thor)
« Reply #1 on: January 03, 2014, 02:01:09 am »
Hmm interesting approach with the sfg::Canvas, not sure if I'd go through with it however. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/