MCPcopy Create free account
hub / github.com/TankOs/SFGUI / main

Function main

examples/GuessMyNumber.cpp:10–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8#include <cstdlib>
9
10int main() {
11 sf::RenderWindow render_window( sf::VideoMode( { 1024, 768 }, 32 ), "Guess My Number (SFGUI)", sf::Style::Titlebar | sf::Style::Close );
12
13 // We have to do this because we don't use SFML to draw.
14 render_window.resetGLStates();
15
16 // Create an SFGUI. This is required before doing anything with SFGUI.
17 sfg::SFGUI sfgui;
18
19 auto current_number_entry = sfg::Entry::Create();
20 auto tries_label = sfg::Label::Create();
21 auto hint_label = sfg::Label::Create();
22 auto guess_button = sfg::Button::Create( "Guess" );
23
24 unsigned char current_number = 0;
25 auto tries = 0u;
26
27 // Custom properties.
28 sfg::Context::Get().GetEngine().SetProperties(
29 "Button#guess {"
30 " BackgroundColor: #006400FF;"
31 " BorderColor: #006400FF;"
32 "}"
33 "Button#guess:Prelight {"
34 " BackgroundColor: #008200FF;"
35 " BorderColor: #008200FF;"
36 "}"
37 "Button#guess > Label {"
38 " FontSize: 20;"
39 "}"
40 );
41
42 auto update_ui = [&tries, &tries_label] {
43 std::stringstream sstr;
44 sstr << tries;
45 tries_label->SetText( sstr.str() );
46 };
47
48 auto reset_game = [update_ui, &tries, &current_number, &hint_label, &guess_button] {
49 tries = 0;
50 current_number = static_cast<unsigned char>( std::rand() % 100 + 1 );
51
52 hint_label->SetText( "-" );
53
54 update_ui();
55 guess_button->Show( true );
56 };
57
58 // Create widgets.
59 auto window = sfg::Window::Create();
60 window->SetTitle( "Guess My Number (SFGUI)" );
61
62 auto new_game_button = sfg::Button::Create( "New game" );
63 new_game_button->GetSignal( sfg::Widget::OnLeftClick ).Connect( [reset_game] { reset_game(); } );
64
65 guess_button->SetId( "guess" );
66 guess_button->GetSignal( sfg::Widget::OnLeftClick ).Connect( [&current_number_entry, &hint_label, &tries, &update_ui, &current_number, &guess_button] {
67 // Validate number.

Callers

nothing calls this directly

Calls 15

SetTitleMethod · 0.80
ConnectMethod · 0.80
SetIdMethod · 0.80
AttachMethod · 0.80
SetColumnSpacingsMethod · 0.80
SetRowSpacingsMethod · 0.80
PackMethod · 0.80
SetPropertiesMethod · 0.45
SetTextMethod · 0.45
strMethod · 0.45
ShowMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected