Velvet 0.1a
A beginner-friendly C++ GUI framework built on SFML.
Loading...
Searching...
No Matches
Window.hpp
Go to the documentation of this file.
1#pragma once
2#include <SFML/Graphics.hpp>
4#include <vector>
5
19class Window {
20 sf::RenderWindow window;
21 sf::Color backgroundColor;
22 std::vector<Widget*> widgets;
23
24 void add_widget(Widget* w) { add(w); }
25 void add_widget(Widget& w) { add(&w); }
26
27public:
31 Window(int width, int height, const std::string& title)
32 : window(sf::VideoMode(width, height), title),
33 backgroundColor(sf::Color(230, 230, 230)) {
34 window.setFramerateLimit(60);
35 }
36
42 void setBackgroundColor(unsigned int hexCode) { backgroundColor = sf::Color(hexCode); }
43
49 void add(Widget* widget) { widgets.push_back(widget); }
50
54 template<typename... Ts>
55 void add(Ts&&... widgets_list) {
56 (add_widget(std::forward<Ts>(widgets_list)), ...);
57 }
58
62 bool isOpen() { return window.isOpen(); }
63
70 void run() {
71 while (window.isOpen()) {
72 sf::Event event;
73 sf::Cursor cursor;
74
75 while (window.pollEvent(event)) {
76 if (cursor.loadFromSystem(sf::Cursor::Arrow)) window.setMouseCursor(cursor);
77
78 if (event.type == sf::Event::Closed)
79 window.close();
80
81 if (event.type == sf::Event::Resized) {
82 sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
83 window.setView(sf::View(visibleArea));
84 }
85
86 for (Widget* w : widgets)
87 w->handleEvent(event, window);
88 }
89 window.clear(backgroundColor);
90
91 for (Widget* w : widgets)
92 w->render(window);
93
94 window.display();
95 }
96 }
97
101 void close() {
102 window.close();
103 }
104};
Base class for all Velvet UI elements.
Definition Widget.hpp:16
virtual void render(sf::RenderWindow &window)
Render this widget.
virtual void handleEvent(const sf::Event &event, sf::RenderWindow &window)
Handle an SFML event.
Application window and main event/render loop.
Definition Window.hpp:19
bool isOpen()
Check if the native window is open.
Definition Window.hpp:62
void setBackgroundColor(unsigned int hexCode)
Set window background color from RGBA hex integer.
Definition Window.hpp:42
void add(Widget *widget)
Add a widget by pointer.
Definition Window.hpp:49
Window(int width, int height, const std::string &title)
Create a window.
Definition Window.hpp:31
void close()
Close the window.
Definition Window.hpp:101
void add(Ts &&... widgets_list)
Add one or more widgets (pointer or reference forms).
Definition Window.hpp:55
void run()
Start event + render loop.
Definition Window.hpp:70