Skip to content

Instantly share code, notes, and snippets.

@kennyrkun
Created April 14, 2017 04:38
Show Gist options
  • Save kennyrkun/dba284c89a43f12ec96d5fd736b2abc0 to your computer and use it in GitHub Desktop.
Save kennyrkun/dba284c89a43f12ec96d5fd736b2abc0 to your computer and use it in GitHub Desktop.

Revisions

  1. kennyrkun created this gist Apr 14, 2017.
    82 changes: 82 additions & 0 deletions window.hpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    //- Window.hpp --------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #ifndef WINDOW_HPP
    #define WINDOW_HPP

    #include <SFML\Graphics.hpp>

    struct WindowStruct
    {
    sf::RectangleShape window;
    sf::RectangleShape windowtop;
    sf::Text windowtitle;
    sf::RectangleShape closebutton;

    void setPosition(sf::Vector2f &pos)
    {
    window.setPosition(pos);
    windowtop.setPosition(window.getPosition());
    windowtitle.setPosition(windowtop.getPosition());
    closebutton.setPosition(wingroup.windowtop.getPosition().x / 2 - wingroup.closebutton.getLocalBounds().width, wingroup.windowtop.getPosition().y);
    }
    };

    Struct WindowContent;

    class Window
    {
    public:
    void create(sf::Vector2f &size, const std::string title);
    void create(sf::Vector2f &size, const std::string title);

    void resize(sf::Vector2f &newsize);
    inline void setPosition(sf::Vector2f &newpos)
    {
    wingroup.setPosition(newpos);
    }

    private:
    WindowStruct wingroup;
    };

    #endif

    //- Window.cpp --------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    #define HEADER_HEIGHT

    void Window::create(sf::Vector2f &size, const sf::String title)
    {
    wingroup.window.setSize(size);
    wingroup.window.setOrigin(wingroup.window.getSize().x / 2, wingroup.window.getSize().y / 2);
    wingroup.window.setPosition(sf::Vector2f(0, 0);

    wingroup.windowtop.setSize(wingroup.window.getSize().x, HEADER_HEIGHT);
    wingroup.windowtop.setOrigin(wingroup.windowtop.getLocalBounds().width / 2, wingroup.windowtop.getLocalBounds.height / 2);
    wingroup.windowtop.setPosition(wingroup.window.getPosition().x, (wingroup.window.getPosition().y * 2) + (wingroup.windowtop.getLocalBounds.height / 2));

    wingroup.windowtitle.setString(title);
    wingroup.windowtitle.setCharacterSize(wingroup.windowtop.getLocalBounds().y - 4);
    wingroup.windowtitle.setOrigin(wingroup.windowtitle.getLocalBounds().width / 2, wingroup.windowtitle.getLocalBounds().height / 2);
    wingroup.windowtitle.setPosition(wingroup.windowtop.getPosition());

    wingroup.closebutton.setSize(HEADER_HEIGHT, wingroup.windowtop.getLocalBounds().height);
    wingroup.closebutton.setOrigin(wingroup.closebutton.getLocalBounds().width, wingroup.closebutton.getLocalBounds().height);
    wingroup.closebutton.setPosition(wingroup.windowtop.getPosition().x / 2 - wingroup.closebutton.getLocalBounds().width, wingroup.windowtop.getPosition().y);
    wingroup.closebutton.setFillColor(sf::Color::Red);
    }

    void Window::resize(sf::Vector2f &newsize)
    {
    wingroup.window.setSize(newsize);

    wingroup.windowtop.setSize(newsize.x, HEADER_HEIGHT);
    wingroup.windowtop.setOrigin(wingroup.windowtop.getLocalBounds().width / 2, wingroup.windowtop.getLocalBOunds().height / 2);
    wingroup.windowtop.setPosition(wingroup.window.getPosition().x, (wingroup.window.getPosition().y * 2) + (wingroup.windowtop.getLocalBounds().getLocalBounds().height / 2);

    wingroup.windowtitle.setPosition(windowtop.getPosition());

    wingroup.closebutton.setPosition(wingroup.windowtop.getPosition().x * 2 - wingroup.closebutton.getLocalBounds().width / 2, wingroup.windowtop.getPosition().y);
    }