Skip to content

Commit

Permalink
Classified most of the examples, made sfg::SFGUI have a purpose as a …
Browse files Browse the repository at this point in the history
…wrapper to Renderer to avoid those ugly sfg::Renderer::Get() everywhere, replaced std::endl by "\n".
  • Loading branch information
binary1248 committed Apr 14, 2012
1 parent 9f4eb7e commit c7beecf
Show file tree
Hide file tree
Showing 26 changed files with 290 additions and 210 deletions.
6 changes: 2 additions & 4 deletions examples/Box.cpp
@@ -1,4 +1,3 @@

#include <SFML/Graphics.hpp>

// Always include the necessary header files.
Expand All @@ -13,8 +12,7 @@ int main() {
// We have to do this because we don't use SFML to draw.
app_window.resetGLStates();

// Construct our SFML guard
// See http://sfgui.sfml-dev.de/forum/topic52-crash-on-close.html for more info.
// Create an SFGUI. This is required before doing anything with SFGUI.
sfg::SFGUI sfgui;

// Create our main SFGUI window
Expand Down Expand Up @@ -83,7 +81,7 @@ int main() {
app_window.clear();

// Draw the GUI
sfg::Renderer::Get().Display( app_window );
sfgui.Display( app_window );

// Update the window
app_window.display();
Expand Down
36 changes: 22 additions & 14 deletions examples/Button.cpp
Expand Up @@ -4,27 +4,35 @@
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;

// Create the label label pointer globally to reach it from OnButtonClick().
sfg::Label::Ptr g_label;
class ButtonExample {
public:
// Our button click handler.
void OnButtonClick();

void OnButtonClick() {
void Run();

private:
// Create an SFGUI. This is required before doing anything with SFGUI.
sfg::SFGUI m_sfgui;

// Create the label pointer here to reach it from OnButtonClick().
sfg::Label::Ptr g_label;
};

void ButtonExample::OnButtonClick() {
g_label->SetText( "Hello SFGUI, pleased to meet you!" );
}

int main() {
void ButtonExample::Run() {
// Create SFML's window.
sf::RenderWindow render_window( sf::VideoMode( SCREEN_WIDTH, SCREEN_HEIGHT ), "Hello world!" );

// Install SFGUI guard that prevents applications from crashing at exit. You
// only need to create one object, but make sure to do it!
sfg::SFGUI guard;

// Create the label.
g_label = sfg::Label::Create( "Hello world!" );

// Create a simple button and connect the click signal.
sfg::Button::Ptr button( sfg::Button::Create( "Greet SFGUI!" ) );
button->OnClick.Connect( &OnButtonClick );
button->OnClick.Connect( &ButtonExample::OnButtonClick, this );

// Create a vertical box layouter with 5 pixels spacing and add the label
// and button to it.
Expand Down Expand Up @@ -65,14 +73,14 @@ int main() {

// Rendering.
render_window.clear();
sfg::Renderer::Get().Display( render_window );
m_sfgui.Display( render_window );
render_window.display();
}
}

// If you have any global or static widgets,
// you need to reset their pointers before your
// application exits.
g_label.reset();
int main() {
ButtonExample example;
example.Run();

return 0;
}
86 changes: 45 additions & 41 deletions examples/ComboBox.cpp
Expand Up @@ -6,58 +6,80 @@
// you can possibly need automatically.
#include <SFGUI/SFGUI.hpp>

// Create our ComboBox smart pointer.
sfg::ComboBox::Ptr combo_box;
class ComboBoxExample {
public:
void OnComboSelect();
void OnAddItemClick();

sfg::Label::Ptr sel_label;
void Run();

void OnComboSelect();
void OnAddItemClick();
private:
// Create an SFGUI. This is required before doing anything with SFGUI.
sfg::SFGUI m_sfgui;

int main() {
// Create our ComboBox smart pointer.
sfg::ComboBox::Ptr m_combo_box;

sfg::Label::Ptr m_sel_label;
};

void ComboBoxExample::OnComboSelect() {
std::stringstream sstr;

sstr << "Item " << m_combo_box->GetSelectedItem() << " selected with text \"" << static_cast<std::string>( m_combo_box->GetSelectedText() ) << "\"";
m_sel_label->SetText( sstr.str() );
}

void ComboBoxExample::OnAddItemClick() {
static int counter( 0 );

std::stringstream sstr;
sstr << "Item " << counter;
m_combo_box->AppendItem( sstr.str() );

++counter;
}

void ComboBoxExample::Run() {
// Create the main SFML window
sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "SFGUI Combo Box Example", sf::Style::Titlebar | sf::Style::Close );

// We have to do this because we don't use SFML to draw.
app_window.resetGLStates();

// Construct our SFML guard
// See http://sfgui.sfml-dev.de/forum/topic52-crash-on-close.html for more info.
sfg::SFGUI sfgui;

// Create our main SFGUI window
sfg::Window::Ptr window;
window = sfg::Window::Create();
window->SetTitle( "Title" );

// Create the combo box itself.
combo_box = sfg::ComboBox::Create();
m_combo_box = sfg::ComboBox::Create();

// Set the entries of the combo box.
combo_box->AppendItem( "Bar" );
combo_box->PrependItem( "Foo" );
m_combo_box->AppendItem( "Bar" );
m_combo_box->PrependItem( "Foo" );

sel_label = sfg::Label::Create( L"Please select an item!" );
m_sel_label = sfg::Label::Create( L"Please select an item!" );

sfg::Button::Ptr button( sfg::Button::Create( L"Add item" ) );

sfg::Box::Ptr hbox( sfg::Box::Create( sfg::Box::HORIZONTAL, 5 ) );
hbox->Pack( combo_box );
hbox->Pack( m_combo_box );
hbox->Pack( button, false );

sfg::Box::Ptr vbox( sfg::Box::Create( sfg::Box::VERTICAL, 5 ) );
vbox->Pack( hbox, false );
vbox->Pack( sel_label, true );
vbox->Pack( m_sel_label, true );

// Add the combo box to the window
window->Add( vbox );

// So that our combo box has a meaningful purpose (besides just looking
// awesome :P) we need to tell it to connect to a callback of our choosing to
// notify us when it is clicked.
combo_box->OnSelect.Connect( &OnComboSelect );
m_combo_box->OnSelect.Connect( &ComboBoxExample::OnComboSelect, this );

button->OnClick.Connect( &OnAddItemClick );
button->OnClick.Connect( &ComboBoxExample::OnAddItemClick, this );

// If attempting to connect to a class method you need to provide
// a pointer to it as the second parameter after the function address.
Expand Down Expand Up @@ -85,34 +107,16 @@ int main() {
app_window.clear();

// Draw the GUI
sfg::Renderer::Get().Display( app_window );
m_sfgui.Display( app_window );

// Update the window
app_window.display();
}

// If you have any global or static widgets,
// you need to reset their pointers before your
// application exits.
combo_box.reset();
sel_label.reset();

return EXIT_SUCCESS;
}

void OnComboSelect() {
std::stringstream sstr;

sstr << "Item " << combo_box->GetSelectedItem() << " selected with text \"" << static_cast<std::string>( combo_box->GetSelectedText() ) << "\"";
sel_label->SetText( sstr.str() );
}

void OnAddItemClick() {
static int counter( 0 );

std::stringstream sstr;
sstr << "Item " << counter;
combo_box->AppendItem( sstr.str() );
int main() {
ComboBoxExample example;
example.Run();

++counter;
return EXIT_SUCCESS;
}
16 changes: 6 additions & 10 deletions examples/Desktop.cpp
Expand Up @@ -17,6 +17,9 @@ class DesktopExample {
void OnDestroyWindowClick();
void OnFrontClick();

// Create an SFGUI. This is required before doing anything with SFGUI.
sfg::SFGUI m_sfgui;

sfg::Desktop m_desktop;
sfg::Window::Ptr m_window;
unsigned int m_count;
Expand All @@ -26,15 +29,8 @@ const unsigned int DesktopExample::SCREEN_WIDTH = 800;
const unsigned int DesktopExample::SCREEN_HEIGHT = 600;

int main() {
// Construct our SFML guard
// See http://sfgui.sfml-dev.de/forum/topic52-crash-on-close.html for more info.
sfg::SFGUI sfgui;

// Make sure app is destroyed before the guard.
{
DesktopExample app;
app.Run();
}
DesktopExample app;
app.Run();

return 0;
}
Expand Down Expand Up @@ -100,7 +96,7 @@ void DesktopExample::Run() {

m_desktop.Update( 0.f );
render_window.clear();
sfg::Renderer::Get().Display( render_window );
m_sfgui.Display( render_window );
render_window.display();
}
}
Expand Down
62 changes: 33 additions & 29 deletions examples/Entry.cpp
Expand Up @@ -5,25 +5,36 @@
// you can possibly need automatically.
#include <SFGUI/SFGUI.hpp>

// Create our entry smart pointer.
sfg::Entry::Ptr entry;
class EntryExample {
public:
void ButtonClick();

// Create our label smart pointer.
sfg::Label::Ptr label;
void Run();

void ButtonClick();
private:
// Create an SFGUI. This is required before doing anything with SFGUI.
sfg::SFGUI m_sfgui;

int main() {
// Create our entry smart pointer.
sfg::Entry::Ptr m_entry;

// Create our label smart pointer.
sfg::Label::Ptr m_label;
};

void EntryExample::ButtonClick() {
// When the button is clicked set the contents of the label
// to the contents of the entry widget.
m_label->SetText( m_entry->GetText() );
}

void EntryExample::Run() {
// Create the main SFML window
sf::RenderWindow app_window( sf::VideoMode( 800, 600 ), "SFGUI Entry Example", sf::Style::Titlebar | sf::Style::Close );

// We have to do this because we don't use SFML to draw.
app_window.resetGLStates();

// Construct our SFML guard
// See http://sfgui.sfml-dev.de/forum/topic52-crash-on-close.html for more info.
sfg::SFGUI sfgui;

// Create our main SFGUI window
sfg::Window::Ptr window;
window = sfg::Window::Create();
Expand All @@ -37,14 +48,14 @@ int main() {
button->SetLabel( "Set" );

// Connect the button.
button->OnClick.Connect( &ButtonClick );
button->OnClick.Connect( &EntryExample::ButtonClick, this );

// Create a label.
label = sfg::Label::Create();
label->SetText( "no text yet" );
m_label = sfg::Label::Create();
m_label->SetText( "no text yet" );

// Create our entry widget itself.
entry = sfg::Entry::Create();
m_entry = sfg::Entry::Create();

// Until now all widgets only expanded to fit the text inside of it.
// This is not the case with the entry widget which can be empty
Expand All @@ -54,15 +65,15 @@ int main() {
// parameter. Depending on which side you want to have a minimum size,
// you set the corresponding value in the vector.
// Here we chose to set the minimum x size of the widget to 80.
entry->SetRequisition( sf::Vector2f( 80.f, 0.f ) );
m_entry->SetRequisition( sf::Vector2f( 80.f, 0.f ) );

// Setting sizing back to automatic is as easy as setting
// x and y sizes to 0.

// Pack into box
box->Pack( entry );
box->Pack( m_entry );
box->Pack( button );
box->Pack( label );
box->Pack( m_label );

// Set box spacing
box->SetSpacing( 5.f );
Expand Down Expand Up @@ -99,23 +110,16 @@ int main() {
app_window.clear();

// Draw the GUI
sfg::Renderer::Get().Display( app_window );
m_sfgui.Display( app_window );

// Update the window
app_window.display();
}
}

// If you have any global or static widgets,
// you need to reset their pointers before your
// application exits.
entry.reset();
label.reset();
int main() {
EntryExample example;
example.Run();

return EXIT_SUCCESS;
}

void ButtonClick() {
// When the button is clicked set the contents of the label
// to the contents of the entry widget.
label->SetText( entry->GetText() );
}

0 comments on commit c7beecf

Please sign in to comment.