| 7 | #include <SFML/Graphics.hpp> |
| 8 | |
| 9 | int main() { |
| 10 | // Create the main SFML window |
| 11 | sf::RenderWindow app_window( sf::VideoMode( { 800, 600 } ), "SFGUI Progress Bar Example", sf::Style::Titlebar | sf::Style::Close ); |
| 12 | |
| 13 | // We have to do this because we don't use SFML to draw. |
| 14 | app_window.resetGLStates(); |
| 15 | |
| 16 | // Create an SFGUI. This is required before doing anything with SFGUI. |
| 17 | sfg::SFGUI sfgui; |
| 18 | |
| 19 | // Create our main SFGUI window |
| 20 | auto window = sfg::Window::Create(); |
| 21 | window->SetTitle( "Title" ); |
| 22 | |
| 23 | // Create our progress bar |
| 24 | auto progressbar = sfg::ProgressBar::Create(); |
| 25 | |
| 26 | // Set how big the progress bar should be |
| 27 | progressbar->SetRequisition( sf::Vector2f( 200.f, 40.f ) ); |
| 28 | |
| 29 | // Create a button and connect the click signal. |
| 30 | auto button = sfg::Button::Create( "Set Random Value" ); |
| 31 | |
| 32 | button->GetSignal( sfg::Widget::OnLeftClick ).Connect( [&progressbar] { |
| 33 | // Generate random value for progress bar percent done |
| 34 | auto random_percentage = static_cast<float>( rand() ) / static_cast<float>( RAND_MAX ); |
| 35 | progressbar->SetFraction(random_percentage); |
| 36 | } ); |
| 37 | |
| 38 | // Create a horizontal box layouter and add widgets to it. |
| 39 | auto box = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL, 5.0f ); |
| 40 | box->Pack( progressbar ); |
| 41 | box->Pack( button, false ); |
| 42 | |
| 43 | // Add the box to the window. |
| 44 | window->Add( box ); |
| 45 | |
| 46 | // Our clock |
| 47 | sf::Clock clock; |
| 48 | |
| 49 | // Update an initial time to construct the GUI before drawing begins. |
| 50 | // This makes sure that there are no frames in which no GUI is visible. |
| 51 | window->Update( 0.f ); |
| 52 | |
| 53 | // Start the game loop |
| 54 | while ( app_window.isOpen() ) { |
| 55 | // Process events |
| 56 | while ( const std::optional event = app_window.pollEvent() ) { |
| 57 | // Handle events |
| 58 | window->HandleEvent( *event ); |
| 59 | |
| 60 | // Close window : exit |
| 61 | if ( event->is<sf::Event::Closed>() ) { |
| 62 | return 0; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Update the GUI every 5ms |
nothing calls this directly
no test coverage detected