Advertisement
Guest User

sfml_mwlauncher

a guest
Jul 10th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 31.49 KB | None | 0 0
  1. #include <SFGUI/SFGUI.hpp>
  2. #include <SFGUI/Widgets.hpp>
  3. #include <SFGUI/Renderers.hpp>
  4.  
  5. #include <SFML/Graphics.hpp>
  6. #include <SFML/System/Clock.hpp>
  7. #include <SFML/OpenGL.hpp>
  8. #include <sstream>
  9. #include <cmath>
  10. #include <memory>
  11. #include <future>
  12.  
  13. std::mutex m;
  14.  
  15. class SampleApp {
  16. public:
  17.     SampleApp();
  18.     ~SampleApp();
  19.     void Run();
  20.  
  21. private:
  22.     void OnAddButtonHClick();
  23.     void OnAddButtonVClick();
  24.     void OnToggleTitlebarClick();
  25.     void OnHideWindowClicked();
  26.     void OnToggleOrientationClick();
  27.     void OnToggleSpaceClick();
  28.     void OnLimitCharsToggle();
  29.     void OnLoadThemeClick();
  30.     void OnAdjustmentChange();
  31.     void OnToggleSpinner();
  32.     void OnMirrorImageClick();
  33.     void OnSwitchRendererClick();
  34.     void RenderCustomGL();
  35.     void RenderCustomSFML();
  36.  
  37.     sf::RenderWindow m_window;
  38.  
  39.     // Create an SFGUI. This is required before doing anything with SFGUI.
  40.     sfg::SFGUI m_sfgui;
  41.  
  42.     sfg::Window::Ptr m_wndmain;
  43.     sfg::Box::Ptr m_boxbuttonsh;
  44.     sfg::Box::Ptr m_boxbuttonsv;
  45.     sfg::Box::Ptr m_boxorientation;
  46.     sfg::Entry::Ptr m_entry;
  47.     sfg::Table::Ptr m_table;
  48.     sfg::ScrolledWindow::Ptr m_scrolled_window;
  49.     sfg::Box::Ptr m_scrolled_window_box;
  50.     sfg::ToggleButton::Ptr m_titlebar_toggle;
  51.     sfg::CheckButton::Ptr m_limit_check;
  52.     sfg::Scale::Ptr m_scale;
  53.     sfg::ComboBox::Ptr m_combo_box;
  54.     sfg::ProgressBar::Ptr m_progress;
  55.     sfg::ProgressBar::Ptr m_progress_vert;
  56.     sfg::Spinner::Ptr m_spinner;
  57.     sfg::Image::Ptr m_image;
  58.     sfg::Canvas::Ptr m_gl_canvas;
  59.     sfg::Canvas::Ptr m_sfml_canvas;
  60.     sfg::Button::Ptr m_switch_renderer;
  61.  
  62.     sfg::Desktop m_desktop;
  63.  
  64.     unsigned int m_fps_counter;
  65.     sf::Clock m_fps_clock;
  66.  
  67.     sf::Texture m_background_texture;
  68.     sf::Sprite m_background_sprite;
  69.     sf::Sprite m_canvas_sprite;
  70.  
  71.     GLuint m_custom_draw_display_list;
  72. };
  73.  
  74. class Ouchy : public std::enable_shared_from_this<Ouchy> {
  75. public:
  76.     typedef std::shared_ptr<Ouchy> Ptr; //!< Shared pointer.
  77.     Ouchy( sfg::Button::Ptr button );
  78.  
  79.     void DoOuch();
  80.  
  81.     static std::vector<Ptr> m_ouchies;
  82. private:
  83.     sfg::Button::Ptr m_button;
  84.     bool m_state;
  85. };
  86.  
  87. std::vector<Ouchy::Ptr> Ouchy::m_ouchies;
  88.  
  89. Ouchy::Ouchy( sfg::Button::Ptr button ) :
  90.         m_button( button ),
  91.         m_state( false )
  92. {
  93. }
  94.  
  95. void Ouchy::DoOuch() {
  96.     if( !m_state ) {
  97.         m_button->SetLabel( "Ouch" );
  98.     }
  99.     else {
  100.         m_button->SetLabel( "Boom" );
  101.     }
  102.  
  103.     m_state = !m_state;
  104. }
  105.  
  106. SampleApp::SampleApp() :
  107.         m_window( sf::VideoMode( 1024, 768, 32 ), "SFGUI test", sf::Style::Default, sf::ContextSettings( 16, 0, 0, 2, 1 ) ),
  108.         m_desktop(),
  109.         m_custom_draw_display_list( 0 )
  110. {
  111.     m_background_texture.create( 1024, 768 );
  112.  
  113.     std::vector<sf::Uint8> pixels( 1024 * 768 * 4 );
  114.  
  115.     sf::Uint8 pixel_value = 139;
  116.  
  117.     for( std::size_t index = 0; index < 1024 * 768; ++index ) {
  118.         pixel_value = static_cast<sf::Uint8>( pixel_value ^ ( index + 809 ) );
  119.         pixel_value = static_cast<sf::Uint8>( pixel_value << ( index % 11 ) );
  120.         pixel_value = static_cast<sf::Uint8>( pixel_value * 233 );
  121.  
  122.         pixels[ index * 4 + 0 ] = static_cast<sf::Uint8>( pixel_value % 16 + 72 ); // R
  123.  
  124.         pixel_value ^= static_cast<sf::Uint8>( index );
  125.         pixel_value = static_cast<sf::Uint8>( pixel_value * 23 );
  126.  
  127.         pixels[ index * 4 + 1 ] = static_cast<sf::Uint8>( pixel_value % 16 + 72 ); // G
  128.  
  129.         pixel_value ^= static_cast<sf::Uint8>( index );
  130.         pixel_value = static_cast<sf::Uint8>( pixel_value * 193 );
  131.  
  132.         pixels[ index * 4 + 2 ] = static_cast<sf::Uint8>( pixel_value % 16 + 72 ); // B
  133.  
  134.         pixels[ index * 4 + 3 ] = 255; // A
  135.     }
  136.  
  137.     m_background_texture.update( pixels.data() );
  138.  
  139.     m_background_sprite.setTexture( m_background_texture );
  140. }
  141.  
  142. SampleApp::~SampleApp() {
  143.     if( m_custom_draw_display_list ) {
  144.         glDeleteLists( m_custom_draw_display_list, 1 );
  145.     }
  146.  
  147.     Ouchy::m_ouchies.clear();
  148. }
  149.  
  150. void SampleApp::Run() {
  151.  
  152.     std::unique_lock<std::mutex> lock(m, std::defer_lock);
  153.     lock.lock();
  154.  
  155.     sf::Event event;
  156.  
  157.     //m_window.SetFramerateLimit( 60 );
  158.     //m_window.EnableVerticalSync( true );
  159.     m_window.setActive( true );
  160.  
  161.     std::string renderer_string;
  162.  
  163.     // Tune Renderer
  164.     if( m_sfgui.GetRenderer().GetName() == "Non-Legacy Renderer" ) {
  165.         static_cast<sfg::NonLegacyRenderer*>( &m_sfgui.GetRenderer() )->TuneUseFBO( true );
  166.         static_cast<sfg::NonLegacyRenderer*>( &m_sfgui.GetRenderer() )->TuneCull( true );
  167.  
  168.         renderer_string = "NLR";
  169.     }
  170.     if( m_sfgui.GetRenderer().GetName() == "Vertex Buffer Renderer" ) {
  171.         static_cast<sfg::VertexBufferRenderer*>( &m_sfgui.GetRenderer() )->TuneUseFBO( true );
  172.         static_cast<sfg::VertexBufferRenderer*>( &m_sfgui.GetRenderer() )->TuneAlphaThreshold( .2f );
  173.         static_cast<sfg::VertexBufferRenderer*>( &m_sfgui.GetRenderer() )->TuneCull( true );
  174.  
  175.         renderer_string = "VBR";
  176.     }
  177.     else if( m_sfgui.GetRenderer().GetName() == "Vertex Array Renderer" ) {
  178.         static_cast<sfg::VertexArrayRenderer*>( &m_sfgui.GetRenderer() )->TuneAlphaThreshold( .2f );
  179.         static_cast<sfg::VertexArrayRenderer*>( &m_sfgui.GetRenderer() )->TuneCull( true );
  180.  
  181.         renderer_string = "VAR";
  182.     }
  183.  
  184.     // Play around with resource manager.
  185.     std::shared_ptr<sf::Font> my_font = std::make_shared<sf::Font>();
  186.     my_font->loadFromFile( "data/linden_hill.otf" );
  187.     m_desktop.GetEngine().GetResourceManager().AddFont( "custom_font", my_font );
  188.  
  189.     // Set properties.
  190.     // Multiple properties can be set at once to save calls.
  191.  
  192.     // m_desktop.SetProperty( "Button#close:Normal", "Color", sf::Color::Yellow );
  193.     // #close is sufficient since there is only 1 widget with this id
  194.     // m_desktop.SetProperty( "#close", "FontName", "data/linden_hill.otf" );
  195.     // m_desktop.SetProperty( "#close", "FontSize", 15.f );
  196.  
  197.     // We will batch the above properties into this call.
  198.     m_desktop.SetProperties(
  199.             "Window#second_window > Box > Label {"
  200.                     "   FontName: custom_font;"
  201.                     "   FontSize: 18;"
  202.                     "}"
  203.                     "Button#close:Normal {"
  204.                     "   Color: #FFFF00FF;"
  205.                     "}"
  206.                     "#close {"
  207.                     "   FontName: data/linden_hill.otf;"
  208.                     "   FontSize: 15;"
  209.                     "}"
  210.     );
  211.  
  212.     // Create widgets.
  213.     m_wndmain = sfg::Window::Create( sfg::Window::Style::TITLEBAR | sfg::Window::Style::BACKGROUND | sfg::Window::Style::RESIZE | sfg::Window::Style::CLOSE );
  214.     m_wndmain->SetTitle( L"Example application" );
  215.  
  216.     auto btnaddbuttonh = sfg::Button::Create( L"Add button horizontally" );
  217.     auto btnaddbuttonv = sfg::Button::Create( L"Add button vertically" );
  218.     m_titlebar_toggle = sfg::ToggleButton::Create( "Toggle titlebar" );
  219.     m_titlebar_toggle->SetActive( true );
  220.  
  221.     {
  222.         sf::Image add_image;
  223.         if( add_image.loadFromFile( "data/add.png" ) ) {
  224.             auto image = sfg::Image::Create( add_image );
  225.             btnaddbuttonh->SetImage( image );
  226.  
  227.             image = sfg::Image::Create( add_image );
  228.             btnaddbuttonv->SetImage( image );
  229.         }
  230.     }
  231.  
  232.     auto btnhidewindow = sfg::Button::Create( L"Close window" );
  233.     btnhidewindow->SetId( "close" );
  234.  
  235.     {
  236.         sf::Image close_image;
  237.         if( close_image.loadFromFile( "data/delete.png" ) ) {
  238.             auto image = sfg::Image::Create( close_image );
  239.             btnhidewindow->SetImage( image );
  240.         }
  241.     }
  242.  
  243.     auto btntoggleori = sfg::Button::Create( L"Box Orientation" );
  244.     auto btntogglespace = sfg::Button::Create( L"Box Spacing" );
  245.     auto btnloadstyle = sfg::Button::Create( L"Load theme" );
  246.  
  247.     m_entry = sfg::Entry::Create( L"Type" );
  248.     m_entry->SetRequisition( sf::Vector2f( 100.f, .0f ) );
  249.     m_entry->AppendText( L" something!" );
  250.  
  251.     m_limit_check = sfg::CheckButton::Create( L"Limit to 4 chars" );
  252.     m_limit_check->SetId( "limit_check" );
  253.  
  254.     auto password = sfg::Entry::Create();
  255.     password->HideText( '*' );
  256.  
  257.     // Layout.
  258.     auto boxtoolbar = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL );
  259.     boxtoolbar->SetSpacing( 5.f );
  260.     boxtoolbar->Pack( btnaddbuttonh, false );
  261.     boxtoolbar->Pack( btnaddbuttonv, false );
  262.     boxtoolbar->Pack( m_titlebar_toggle, false );
  263.     boxtoolbar->Pack( btnhidewindow, false );
  264.     boxtoolbar->Pack( m_entry, true );
  265.     boxtoolbar->Pack( m_limit_check, false );
  266.  
  267.     auto frame1 = sfg::Frame::Create( L"Toolbar 1" );
  268.     frame1->Add( boxtoolbar );
  269.  
  270.     auto boxtoolbar2 = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL );
  271.     boxtoolbar2->SetSpacing( 5.f );
  272.     boxtoolbar2->Pack( btntoggleori, false );
  273.     boxtoolbar2->Pack( btntogglespace, false );
  274.     boxtoolbar2->Pack( btnloadstyle, false );
  275.  
  276.     m_boxbuttonsh = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL );
  277.     m_boxbuttonsh->SetSpacing( 5.f );
  278.  
  279.     m_boxbuttonsv = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
  280.     m_boxbuttonsv->SetSpacing( 5.f );
  281.  
  282.     m_boxorientation = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL );
  283.     m_boxorientation->SetSpacing( 5.f );
  284.     m_boxorientation->Pack( sfg::Button::Create( L"Hello" ) );
  285.     m_boxorientation->Pack( sfg::Button::Create( L"World" ) );
  286.  
  287.     auto username_entry = sfg::Entry::Create();
  288.     username_entry->SetMaximumLength( 8 );
  289.  
  290.     m_progress = sfg::ProgressBar::Create( sfg::ProgressBar::Orientation::HORIZONTAL );
  291.     m_progress->SetRequisition( sf::Vector2f( 0.f, 20.f ) );
  292.  
  293.     m_progress_vert = sfg::ProgressBar::Create( sfg::ProgressBar::Orientation::VERTICAL );
  294.     m_progress_vert->SetRequisition( sf::Vector2f( 20.f, 0.f ) );
  295.  
  296.     auto separatorv = sfg::Separator::Create( sfg::Separator::Orientation::VERTICAL );
  297.  
  298.     m_table = sfg::Table::Create();
  299.     m_table->Attach( sfg::Label::Create( L"Please login using your username and password (span example)." ), sf::Rect<sf::Uint32>( 0, 0, 2, 1 ), sfg::Table::FILL, sfg::Table::FILL | sfg::Table::EXPAND );
  300.     m_table->Attach( sfg::Label::Create( L"Username:" ), sf::Rect<sf::Uint32>( 0, 1, 1, 1 ), sfg::Table::FILL, sfg::Table::FILL );
  301.     m_table->Attach( username_entry, sf::Rect<sf::Uint32>( 1, 1, 1, 1 ), sfg::Table::EXPAND | sfg::Table::FILL, sfg::Table::FILL );
  302.     m_table->Attach( sfg::Label::Create( L"Password:" ), sf::Rect<sf::Uint32>( 0, 2, 1, 1 ), sfg::Table::FILL, sfg::Table::FILL );
  303.     m_table->Attach( password, sf::Rect<sf::Uint32>( 1, 2, 1, 1 ), sfg::Table::FILL, sfg::Table::FILL );
  304.     m_table->Attach( sfg::Button::Create( L"Login" ), sf::Rect<sf::Uint32>( 2, 1, 1, 2 ), sfg::Table::FILL, sfg::Table::FILL );
  305.     m_table->Attach( separatorv, sf::Rect<sf::Uint32>( 3, 0, 1, 3 ), sfg::Table::FILL, sfg::Table::FILL );
  306.     m_table->Attach( m_progress_vert, sf::Rect<sf::Uint32>( 4, 0, 1, 3 ), sfg::Table::FILL, sfg::Table::FILL );
  307.     m_table->SetRowSpacings( 5.f );
  308.     m_table->SetColumnSpacings( 5.f );
  309.  
  310.     m_scrolled_window_box = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
  311.  
  312.     for( int i = 0; i < 5; i++ ) {
  313.         auto box = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL );
  314.  
  315.         for( int j = 0; j < 20; j++ ) {
  316.             box->Pack( sfg::Button::Create( L"One button among many" ), true );
  317.         }
  318.  
  319.         m_scrolled_window_box->Pack( box, false );
  320.     }
  321.  
  322.     m_scrolled_window = sfg::ScrolledWindow::Create();
  323.     m_scrolled_window->SetRequisition( sf::Vector2f( .0f, 160.f ) );
  324.     m_scrolled_window->SetScrollbarPolicy( sfg::ScrolledWindow::HORIZONTAL_AUTOMATIC | sfg::ScrolledWindow::VERTICAL_AUTOMATIC );
  325.     m_scrolled_window->SetPlacement( sfg::ScrolledWindow::Placement::TOP_LEFT );
  326.     m_scrolled_window->AddWithViewport( m_scrolled_window_box );
  327.  
  328.     auto scrollbar = sfg::Scrollbar::Create();
  329.     scrollbar->SetRange( .0f, 100.f );
  330.  
  331.     m_scale = sfg::Scale::Create();
  332.     m_scale->SetAdjustment( scrollbar->GetAdjustment() );
  333.     m_scale->SetRequisition( sf::Vector2f( 100.f, .0f ) );
  334.     boxtoolbar2->Pack( m_scale, false );
  335.  
  336.     m_combo_box = sfg::ComboBox::Create();
  337.  
  338.     for( int index = 0; index < 30; ++index ) {
  339.         std::stringstream sstr;
  340.  
  341.         sstr << "Item " << index;
  342.  
  343.         m_combo_box->AppendItem( sstr.str() );
  344.     }
  345.  
  346.     boxtoolbar2->Pack( m_combo_box, true );
  347.  
  348.     m_switch_renderer = sfg::Button::Create( "Renderer: " + renderer_string );
  349.  
  350.     boxtoolbar2->Pack( m_switch_renderer, false );
  351.  
  352.     auto frame2 = sfg::Frame::Create( L"Toolbar 2" );
  353.     frame2->Add( boxtoolbar2 );
  354.     frame2->SetAlignment( sf::Vector2f( .8f, .0f ) );
  355.  
  356.     auto separatorh = sfg::Separator::Create( sfg::Separator::Orientation::HORIZONTAL );
  357.  
  358.     auto box_image = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL );
  359.     box_image->SetSpacing( 15.f );
  360.  
  361.     auto fixed_container = sfg::Fixed::Create();
  362.     auto fixed_button = sfg::Button::Create( L"I'm at (34,61)" );
  363.     fixed_container->Put( fixed_button, sf::Vector2f( 34.f, 61.f ) );
  364.     box_image->Pack( fixed_container, false );
  365.  
  366.     sf::Image sfgui_logo;
  367.     m_image = sfg::Image::Create();
  368.  
  369.     if( sfgui_logo.loadFromFile( "data/sfgui.png" ) ) {
  370.         m_image->SetImage( sfgui_logo );
  371.         box_image->Pack( m_image, false );
  372.     }
  373.  
  374.     auto mirror_image = sfg::Button::Create( L"Mirror Image" );
  375.  
  376.     box_image->Pack( mirror_image, false );
  377.  
  378.     auto spinner_box = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
  379.  
  380.     m_spinner = sfg::Spinner::Create();
  381.     m_spinner->SetRequisition( sf::Vector2f( 40.f, 40.f ) );
  382.     m_spinner->Start();
  383.     auto spinner_toggle = sfg::ToggleButton::Create( L"Spin" );
  384.     spinner_toggle->SetActive( true );
  385.     spinner_box->SetSpacing( 5.f );
  386.     spinner_box->Pack( m_spinner, false );
  387.     spinner_box->Pack( spinner_toggle, false );
  388.  
  389.     box_image->Pack( spinner_box, false );
  390.  
  391.     auto radio_box = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
  392.  
  393.     auto radio1 = sfg::RadioButton::Create( "Radio 1" );
  394.     auto radio2 = sfg::RadioButton::Create( "Radio 2", radio1->GetGroup() );
  395.     auto radio3 = sfg::RadioButton::Create( "Radio 3", radio2->GetGroup() );
  396.  
  397.     radio_box->Pack( radio1 );
  398.     radio_box->Pack( radio2 );
  399.     radio_box->Pack( radio3 );
  400.  
  401.     box_image->Pack( radio_box, false );
  402.  
  403.     auto spinbutton = sfg::SpinButton::Create( scrollbar->GetAdjustment() );
  404.     spinbutton->SetRequisition( sf::Vector2f( 80.f, 0.f ) );
  405.     spinbutton->SetDigits( 3 );
  406.  
  407.     auto spinbutton_box = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
  408.     spinbutton_box->Pack( spinbutton, false, false );
  409.  
  410.     box_image->Pack( spinbutton_box, false, false );
  411.  
  412.     auto aligned_combo_box = sfg::ComboBox::Create();
  413.     aligned_combo_box->AppendItem( L"I'm way over here" );
  414.     aligned_combo_box->AppendItem( L"Me too" );
  415.     aligned_combo_box->AppendItem( L"Me three" );
  416.     aligned_combo_box->SelectItem( 0 );
  417.  
  418.     auto alignment = sfg::Alignment::Create();
  419.     alignment->Add( aligned_combo_box );
  420.     box_image->Pack( alignment, true );
  421.     alignment->SetAlignment( sf::Vector2f( 1.f, .5f ) );
  422.     alignment->SetScale( sf::Vector2f( 0.f, .01f ) );
  423.  
  424.     auto boxmain = sfg::Box::Create( sfg::Box::Orientation::VERTICAL );
  425.     boxmain->SetSpacing( 5.f );
  426.     boxmain->Pack( scrollbar, false );
  427.     boxmain->Pack( m_progress, false );
  428.     boxmain->Pack( frame1, false );
  429.     boxmain->Pack( frame2, false );
  430.     boxmain->Pack( m_boxbuttonsh, false );
  431.     boxmain->Pack( m_boxbuttonsv, false );
  432.     boxmain->Pack( box_image, true );
  433.     boxmain->Pack( separatorh, false );
  434.     boxmain->Pack( m_table, true );
  435.     boxmain->Pack( m_boxorientation, true );
  436.     boxmain->Pack( m_scrolled_window );
  437.  
  438.     auto notebook1 = sfg::Notebook::Create();
  439.     auto notebook2 = sfg::Notebook::Create();
  440.     auto notebook3 = sfg::Notebook::Create();
  441.     auto notebook4 = sfg::Notebook::Create();
  442.  
  443.     notebook1->SetTabPosition( sfg::Notebook::TabPosition::TOP );
  444.     notebook2->SetTabPosition( sfg::Notebook::TabPosition::RIGHT );
  445.     notebook3->SetTabPosition( sfg::Notebook::TabPosition::BOTTOM );
  446.     notebook4->SetTabPosition( sfg::Notebook::TabPosition::LEFT );
  447.  
  448.     auto vertigo_box = sfg::Box::Create( sfg::Box::Orientation::HORIZONTAL );
  449.     auto vertigo_button = sfg::Button::Create( L"Vertigo" );
  450.     vertigo_box->Pack( vertigo_button, true, true );
  451.  
  452.     notebook1->AppendPage( boxmain, sfg::Label::Create( "Page Name Here" ) );
  453.     notebook1->AppendPage( notebook2, sfg::Label::Create( "Another Page" ) );
  454.     notebook2->AppendPage( notebook3, sfg::Label::Create( "Yet Another Page" ) );
  455.     notebook2->AppendPage( sfg::Label::Create( L"" ), sfg::Label::Create( "Dummy Page" ) );
  456.     notebook3->AppendPage( notebook4, sfg::Label::Create( "And Another Page" ) );
  457.     notebook3->AppendPage( sfg::Label::Create( L"" ), sfg::Label::Create( "Dummy Page" ) );
  458.     notebook4->AppendPage( vertigo_box, sfg::Label::Create( "And The Last Page" ) );
  459.     notebook4->AppendPage( sfg::Label::Create( L"" ), sfg::Label::Create( "Dummy Page" ) );
  460.  
  461.     m_wndmain->Add( notebook1 );
  462.  
  463.     // Signals.
  464.     m_wndmain->GetSignal( sfg::Window::OnCloseButton ).Connect( [this] { OnHideWindowClicked(); } );
  465.     btnaddbuttonh->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnAddButtonHClick(); } );
  466.     btnaddbuttonv->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnAddButtonVClick(); } );
  467.     m_titlebar_toggle->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnToggleTitlebarClick(); } );
  468.     btnhidewindow->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnHideWindowClicked(); } );
  469.     btntoggleori->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnToggleOrientationClick(); } );
  470.     btntogglespace->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnToggleSpaceClick(); } );
  471.     m_limit_check->GetSignal( sfg::ToggleButton::OnToggle ).Connect( [this] { OnLimitCharsToggle(); } );
  472.     btnloadstyle->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnLoadThemeClick(); } );
  473.     m_scale->GetAdjustment()->GetSignal( sfg::Adjustment::OnChange ).Connect( [this] { OnAdjustmentChange(); } );
  474.     spinner_toggle->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnToggleSpinner(); } );
  475.     mirror_image->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnMirrorImageClick(); } );
  476.     m_switch_renderer->GetSignal( sfg::Widget::OnLeftClick ).Connect( [this] { OnSwitchRendererClick(); } );
  477.  
  478.     spinbutton->SetValue( 20.f );
  479.     spinbutton->GetAdjustment()->SetMinorStep( .8f );
  480.  
  481.     m_wndmain->SetPosition( sf::Vector2f( 100.f, 100.f ) );
  482.  
  483.     // Another window
  484.     auto second_window = sfg::Window::Create( sfg::Window::TITLEBAR | sfg::Window::BACKGROUND | sfg::Window::RESIZE );
  485.     second_window->SetId( "second_window" );
  486.     second_window->SetTitle( "Resize this window to see ad-hoc wrapping." );
  487.     auto box = sfg::Box::Create( sfg::Box::Orientation::VERTICAL, 5.f );
  488.  
  489.     auto lipsum = sfg::Label::Create(
  490.             "Nullam ut ante leo. Quisque consequat condimentum pulvinar. "
  491.                     "Duis a enim sapien, ut vestibulum est. Vestibulum commodo, orci non gravida. "
  492.                     "Aliquam sed pretium lacus. "
  493.                     "Nullam placerat mauris vel nulla sagittis pellentesque. "
  494.                     "Suspendisse in justo dui.\n"
  495.                     "Ut dolor massa, gravida eu facilisis convallis, convallis sed odio.\n"
  496.                     "Nunc placerat consequat vehicula."
  497.     );
  498.  
  499.     lipsum->SetRequisition( sf::Vector2f( 400.f, 0.f ) );
  500.     lipsum->SetLineWrap( true );
  501.  
  502.     box->Pack( lipsum );
  503.     second_window->Add( box );
  504.     second_window->SetPosition( sf::Vector2f( 10.f, 10.f ) );
  505.     second_window->SetId( "second_window" );
  506.     m_desktop.Add( second_window );
  507.  
  508.     auto third_window = sfg::Window::Create( sfg::Window::TITLEBAR | sfg::Window::BACKGROUND | sfg::Window::RESIZE );
  509.  
  510.     m_gl_canvas = sfg::Canvas::Create( true );
  511.     m_gl_canvas->SetRequisition( sf::Vector2f( 200.f, 150.f ) );
  512.  
  513.     third_window->Add( m_gl_canvas );
  514.  
  515.     third_window->SetId( "third_window" );
  516.     third_window->SetTitle( "Embedded OpenGL drawing" );
  517.     third_window->SetPosition( sf::Vector2f( 480.f, 20.f ) );
  518.     m_desktop.Add( third_window );
  519.  
  520.     sf::Texture texture;
  521.     texture.loadFromImage( sfgui_logo );
  522.     m_canvas_sprite.setTexture( texture );
  523.  
  524.     auto fourth_window = sfg::Window::Create( sfg::Window::TITLEBAR | sfg::Window::BACKGROUND | sfg::Window::RESIZE );
  525.  
  526.     m_sfml_canvas = sfg::Canvas::Create();
  527.     m_sfml_canvas->SetRequisition( sf::Vector2f( static_cast<float>( texture.getSize().x ), static_cast<float>( texture.getSize().y ) ) );
  528.  
  529.     fourth_window->Add( m_sfml_canvas );
  530.  
  531.     fourth_window->SetId( "fourth_window" );
  532.     fourth_window->SetTitle( "Embedded SFML drawing" );
  533.     fourth_window->SetPosition( sf::Vector2f( 760.f, 20.f ) );
  534.     m_desktop.Add( fourth_window );
  535.  
  536.     // Add window to desktop
  537.     m_desktop.Add( m_wndmain );
  538.  
  539.     m_fps_counter = 0;
  540.     m_fps_clock.restart();
  541.  
  542.     sf::Clock clock;
  543.     sf::Clock frame_time_clock;
  544.  
  545.     sf::Int64 frame_times[5000];
  546.     std::size_t frame_times_index = 0;
  547.  
  548.     std::fill( std::begin( frame_times ), std::end( frame_times ), 0 );
  549.  
  550.     m_desktop.Update( 0.f );
  551.  
  552.     while( m_window.isOpen() ) {
  553.         if (!m_window.hasFocus() && lock.owns_lock())
  554.         {
  555.             lock.release();
  556.             break;
  557.         }
  558.         if (m_window.hasFocus() && !lock.owns_lock())
  559.         {
  560.             lock.try_lock();
  561.             break;
  562.         }
  563.         while( m_window.pollEvent( event ) ) {
  564.             if( event.type == sf::Event::Closed ) {
  565.                 return;
  566.             }
  567.  
  568.             m_desktop.HandleEvent( event );
  569.         }
  570.  
  571.         m_window.draw( m_background_sprite );
  572.  
  573.         auto microseconds = clock.getElapsedTime().asMicroseconds();
  574.  
  575.         // Only update every 5ms
  576.         if( microseconds > 5000 ) {
  577.             m_desktop.Update( static_cast<float>( microseconds ) / 1000000.f );
  578.             clock.restart();
  579.  
  580.             // Only refresh canvas contents every 5ms too
  581.             m_gl_canvas->Bind();
  582.             m_gl_canvas->Clear( sf::Color( 0, 0, 0, 0 ), true );
  583.             RenderCustomGL();
  584.             m_gl_canvas->Display();
  585.             m_gl_canvas->Unbind();
  586.  
  587.             m_sfml_canvas->Bind();
  588.             m_sfml_canvas->Clear( sf::Color( 0, 0, 0, 0 ) );
  589.             RenderCustomSFML();
  590.             m_sfml_canvas->Display();
  591.             m_sfml_canvas->Unbind();
  592.  
  593.             m_window.setActive( true );
  594.         }
  595.  
  596.         m_sfgui.Display( m_window );
  597.  
  598.         m_window.display();
  599.  
  600.         auto frame_time = frame_time_clock.getElapsedTime().asMicroseconds();
  601.         frame_time_clock.restart();
  602.  
  603.         frame_times[ frame_times_index ] = frame_time;
  604.         frame_times_index = ( frame_times_index + 1 ) % 5000;
  605.  
  606.         if( m_fps_clock.getElapsedTime().asMicroseconds() >= 1000000 ) {
  607.             m_fps_clock.restart();
  608.  
  609.             sf::Int64 total_time = 0;
  610.  
  611.             for( std::size_t index = 0; index < 5000; ++index ) {
  612.                 total_time += frame_times[index];
  613.             }
  614.  
  615.             std::stringstream sstr;
  616.             sstr << "SFGUI test -- FPS: " << m_fps_counter << " -- Frame Time (microsecs): min: "
  617.                  << *std::min_element( frame_times, frame_times + 5000 ) << " max: "
  618.                  << *std::max_element( frame_times, frame_times + 5000 ) << " avg: "
  619.                  << static_cast<float>( total_time ) / 5000.f;
  620.  
  621.             m_window.setTitle( sstr.str() );
  622.  
  623.             m_fps_counter = 0;
  624.         }
  625.  
  626.         ++m_fps_counter;
  627.     }
  628. }
  629.  
  630. void SampleApp::OnAddButtonHClick() {
  631.     auto button = sfg::Button::Create( L"New ->" );
  632.  
  633.     auto ouchy = std::make_shared<Ouchy>( button );
  634.     Ouchy::m_ouchies.push_back( ouchy );
  635.  
  636.     std::weak_ptr<Ouchy> weak_ouchy = ouchy;
  637.  
  638.     button->GetSignal( sfg::Widget::OnLeftClick ).Connect( [weak_ouchy] {
  639.         auto strong_ouchy = weak_ouchy.lock();
  640.  
  641.         if( strong_ouchy ) {
  642.             strong_ouchy->DoOuch();
  643.         }
  644.     } );
  645.  
  646.     m_boxbuttonsh->Pack( button, true );
  647. }
  648.  
  649. void SampleApp::OnAddButtonVClick() {
  650.     auto button = sfg::Button::Create( L"<- New" );
  651.  
  652.     auto ouchy = std::make_shared<Ouchy>( button );
  653.     Ouchy::m_ouchies.push_back( ouchy );
  654.  
  655.     std::weak_ptr<Ouchy> weak_ouchy = ouchy;
  656.  
  657.     button->GetSignal( sfg::Widget::OnLeftClick ).Connect( [weak_ouchy] {
  658.         auto strong_ouchy = weak_ouchy.lock();
  659.  
  660.         if( strong_ouchy ) {
  661.             strong_ouchy->DoOuch();
  662.         }
  663.     } );
  664.  
  665.     m_boxbuttonsv->Pack( button, false );
  666. }
  667.  
  668. void SampleApp::OnToggleTitlebarClick() {
  669.     m_wndmain->SetStyle( m_wndmain->GetStyle() ^ sfg::Window::TITLEBAR );
  670. }
  671.  
  672. void SampleApp::OnHideWindowClicked() {
  673.     m_wndmain->Show( !m_wndmain->IsLocallyVisible() );
  674. }
  675.  
  676. void SampleApp::OnToggleOrientationClick() {
  677.     if( m_boxorientation->GetOrientation() == sfg::Box::Orientation::HORIZONTAL ) {
  678.         m_boxorientation->SetOrientation( sfg::Box::Orientation::VERTICAL );
  679.     }
  680.     else {
  681.         m_boxorientation->SetOrientation( sfg::Box::Orientation::HORIZONTAL );
  682.     }
  683. }
  684.  
  685. void SampleApp::OnToggleSpaceClick() {
  686.     if( m_scrolled_window_box->GetSpacing() > .0f ) {
  687.         m_scrolled_window_box->SetSpacing( .0f );
  688.     }
  689.     else {
  690.         m_scrolled_window_box->SetSpacing( 40.f );
  691.     }
  692. }
  693.  
  694.  
  695. void SampleApp::OnLimitCharsToggle() {
  696.     if( m_limit_check->IsActive() ) {
  697.         m_entry->SetMaximumLength( 4 );
  698.     }
  699.     else {
  700.         m_entry->SetMaximumLength( 0 );
  701.     }
  702. }
  703.  
  704. void SampleApp::OnLoadThemeClick() {
  705.     m_desktop.LoadThemeFromFile( "data/example.theme" );
  706. }
  707.  
  708. void SampleApp::OnAdjustmentChange() {
  709.     m_progress->SetFraction( m_scale->GetValue() / 100.f );
  710.     m_progress_vert->SetFraction( m_scale->GetValue() / 100.f );
  711. }
  712.  
  713. void SampleApp::OnToggleSpinner() {
  714.     if( !m_spinner->Started() ) {
  715.         m_spinner->Start();
  716.     }
  717.     else {
  718.         m_spinner->Stop();
  719.     }
  720. }
  721.  
  722. void SampleApp::OnMirrorImageClick() {
  723.     auto image = m_image->GetImage();
  724.  
  725.     for( unsigned int height_index = 0; height_index < image.getSize().y; ++height_index ) {
  726.         for( unsigned int width_index = 0; width_index < image.getSize().x / 2; ++width_index ) {
  727.             auto color0 = image.getPixel( width_index, height_index );
  728.             auto color1 = image.getPixel( image.getSize().x - width_index - 1, height_index );
  729.             image.setPixel( width_index, height_index, color1 );
  730.             image.setPixel( image.getSize().x - width_index - 1, height_index, color0 );
  731.         }
  732.     }
  733.  
  734.     m_image->SetImage( image );
  735. }
  736.  
  737. void SampleApp::OnSwitchRendererClick() {
  738.     if( sfg::Renderer::Get().GetName() == "Non-Legacy Renderer" ) {
  739.         auto renderer = sfg::VertexBufferRenderer::Create();
  740.  
  741.         sfg::Renderer::Set( renderer );
  742.  
  743.         renderer->TuneUseFBO( true );
  744.         renderer->TuneAlphaThreshold( .2f );
  745.         renderer->TuneCull( true );
  746.  
  747.         m_switch_renderer->SetLabel( "Renderer: VBR" );
  748.  
  749.         m_desktop.Refresh();
  750.         return;
  751.     }
  752.  
  753.     if( sfg::Renderer::Get().GetName() == "Vertex Buffer Renderer" ) {
  754.         auto renderer = sfg::VertexArrayRenderer::Create();
  755.  
  756.         sfg::Renderer::Set( renderer );
  757.  
  758.         renderer->TuneAlphaThreshold( .2f );
  759.         renderer->TuneCull( true );
  760.  
  761.         m_switch_renderer->SetLabel( "Renderer: VAR" );
  762.  
  763.         m_desktop.Refresh();
  764.         return;
  765.     }
  766.  
  767.     if( sfg::NonLegacyRenderer::IsAvailable() ) {
  768.         auto renderer = sfg::NonLegacyRenderer::Create();
  769.  
  770.         sfg::Renderer::Set( renderer );
  771.  
  772.         renderer->TuneUseFBO( true );
  773.         renderer->TuneCull( true );
  774.  
  775.         m_switch_renderer->SetLabel( "Renderer: NLR" );
  776.  
  777.         m_desktop.Refresh();
  778.     }
  779.     else if( sfg::VertexBufferRenderer::IsAvailable() ) {
  780.         auto renderer = sfg::VertexBufferRenderer::Create();
  781.  
  782.         sfg::Renderer::Set( renderer );
  783.  
  784.         renderer->TuneUseFBO( true );
  785.         renderer->TuneAlphaThreshold( .2f );
  786.         renderer->TuneCull( true );
  787.  
  788.         m_switch_renderer->SetLabel( "Renderer: VBR" );
  789.  
  790.         m_desktop.Refresh();
  791.     }
  792. }
  793.  
  794. void SampleApp::RenderCustomGL() {
  795.     static sf::Clock clock;
  796.  
  797.     glMatrixMode( GL_MODELVIEW );
  798.     glPushMatrix();
  799.     glLoadIdentity();
  800.  
  801.     glTranslatef( 0.f, 0.f, -3.f );
  802.  
  803.     glRotatef( clock.getElapsedTime().asSeconds() * 50.f, 1.f, 0.f, 0.f );
  804.     glRotatef( clock.getElapsedTime().asSeconds() * 30.f, 0.f, 1.f, 0.f );
  805.     glRotatef( clock.getElapsedTime().asSeconds() * 90.f, 0.f, 0.f, 1.f );
  806.  
  807.     glViewport( 0, 0, static_cast<int>( std::floor( m_gl_canvas->GetAllocation().width + .5f ) ), static_cast<int>( std::floor( m_gl_canvas->GetAllocation().height + .5f ) ) );
  808.  
  809.     if( !m_custom_draw_display_list ) {
  810.         m_custom_draw_display_list = glGenLists( 1 );
  811.  
  812.         glNewList( m_custom_draw_display_list, GL_COMPILE );
  813.  
  814.         glEnable( GL_DEPTH_TEST );
  815.         glDepthMask( GL_TRUE );
  816.  
  817.         glDisable( GL_TEXTURE_2D );
  818.  
  819.         glMatrixMode( GL_PROJECTION );
  820.         glPushMatrix();
  821.         glLoadIdentity();
  822.  
  823.         static const auto pi = 3.1415926535897932384626433832795f;
  824.         static const auto fov = 90.f;
  825.         static const auto near_distance = 1.f;
  826.         static const auto far_distance = 20.f;
  827.  
  828.         // We set the proper aspect ratio using the dimensions of our Canvas.
  829.         auto aspect = m_gl_canvas->GetAllocation().width / m_gl_canvas->GetAllocation().height;
  830.         auto frustum_height = std::tan( fov / 360 * pi ) * near_distance;
  831.         auto frustum_width = frustum_height * aspect;
  832.  
  833.         glFrustum( -frustum_width, frustum_width, -frustum_height, frustum_height, near_distance, far_distance );
  834.  
  835.         glBegin( GL_QUADS );
  836.  
  837.         glColor3f( 0.f, 0.f, 1.f );
  838.         glVertex3f( -1.f, -1.f, -1.f );
  839.         glVertex3f( -1.f, 1.f, -1.f );
  840.         glVertex3f( 1.f, 1.f, -1.f );
  841.         glVertex3f( 1.f, -1.f, -1.f );
  842.  
  843.         glColor3f( 0.f, 1.f, 0.f );
  844.         glVertex3f( -1.f, -1.f, 1.f );
  845.         glVertex3f( -1.f, 1.f, 1.f );
  846.         glVertex3f( 1.f, 1.f, 1.f );
  847.         glVertex3f( 1.f, -1.f, 1.f );
  848.  
  849.         glColor3f( 0.f, 1.f, 1.f );
  850.         glVertex3f( -1.f, -1.f, -1.f );
  851.         glVertex3f( -1.f, 1.f, -1.f );
  852.         glVertex3f( -1.f, 1.f, 1.f );
  853.         glVertex3f( -1.f, -1.f, 1.f );
  854.  
  855.         glColor3f( 1.f, 0.f, 0.f );
  856.         glVertex3f( 1.f, -1.f, -1.f );
  857.         glVertex3f( 1.f, 1.f, -1.f );
  858.         glVertex3f( 1.f, 1.f, 1.f );
  859.         glVertex3f( 1.f, -1.f, 1.f );
  860.  
  861.         glColor3f( 1.f, 0.f, 1.f );
  862.         glVertex3f( -1.f, -1.f, 1.f );
  863.         glVertex3f( -1.f, -1.f, -1.f );
  864.         glVertex3f( 1.f, -1.f, -1.f );
  865.         glVertex3f( 1.f, -1.f, 1.f );
  866.  
  867.         glColor3f( 1.f, 1.f, 0.f );
  868.         glVertex3f( -1.f, 1.f, 1.f );
  869.         glVertex3f( -1.f, 1.f, -1.f );
  870.         glVertex3f( 1.f, 1.f, -1.f );
  871.         glVertex3f( 1.f, 1.f, 1.f );
  872.  
  873.         glEnd();
  874.  
  875.         glPopMatrix();
  876.  
  877.         glEnable( GL_TEXTURE_2D );
  878.         glDisable( GL_DEPTH_TEST );
  879.  
  880.         glEndList();
  881.     }
  882.  
  883.     glCallList( m_custom_draw_display_list );
  884.  
  885.     glMatrixMode( GL_MODELVIEW );
  886.     glPopMatrix();
  887.  
  888.     glViewport( 0, 0, static_cast<int>( m_window.getSize().x ), static_cast<int>( m_window.getSize().y ) );
  889. }
  890.  
  891. void SampleApp::RenderCustomSFML() {
  892.     m_sfml_canvas->Draw( m_canvas_sprite );
  893. }
  894.  
  895. bool launcher (SampleApp & app)
  896. {
  897.     app.Run();
  898.     return true;
  899. }
  900. int main() {
  901.     SampleApp app1;
  902.     SampleApp app2;
  903.     auto o1 = std::async(std::launch::async, launcher, std::ref(app1));;
  904.     auto o2 = std::async(std::launch::async, launcher, std::ref(app2));;
  905.  
  906.     o1.wait();
  907.     o2.wait();
  908.     return 0;
  909. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement