2 A simple graphical Hello World application implemented with mspgltk.
3 Demonstrates some of the most common widget types.
6 #include <msp/core/application.h>
7 #include <msp/gbase/simplewindow.h>
8 #include <msp/gl/blend.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gltk/button.h>
11 #include <msp/gltk/entry.h>
12 #include <msp/gltk/label.h>
13 #include <msp/gltk/panel.h>
14 #include <msp/gltk/resources.h>
15 #include <msp/gltk/root.h>
19 // Application class. Because it's so much nicer than global variables.
20 class HelloWorld: public Msp::Application
23 // An OpenGL window to display our widgets in
24 Graphics::SimpleGLWindow wnd;
26 // GLtk resources and widgets
29 GLtk::Entry *ent_name;
30 GLtk::Label *lbl_hello;
32 // Indicate our main class to the core library
33 static Application::RegApp<HelloWorld> reg;
36 HelloWorld(int, char **);
43 Application::RegApp<HelloWorld> HelloWorld::reg;
45 HelloWorld::HelloWorld(int, char **):
47 // Load resources. This must be done before the root widget is created.
49 // A Root receives input from a Graphics::Window and passes it on
52 wnd.set_title("Hello World");
53 wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
55 /* Container widgets will delete their contents upon destruction so we can
56 safely forget about the pointers after setting the widgets up. */
58 // Panels can be used to divide the window into sub-areas
59 GLtk::Panel *panel = new GLtk::Panel;
61 panel->set_geometry(GLtk::Geometry(20, 20, 160, 160));
64 // Prompts can be displayed with Labels
65 panel->add(*(lbl = new GLtk::Label("Type your name below:")));
66 lbl->set_geometry(GLtk::Geometry(10, 130, 140, 20));
68 // The user can type text into an Entry
69 panel->add(*(ent_name = new GLtk::Entry));
70 ent_name->set_geometry(GLtk::Geometry(10, 110, 140, 20));
73 // Buttons can be wired to cause things to happen
74 panel->add(*(btn = new GLtk::Button("Hello")));
75 btn->set_geometry(GLtk::Geometry(10, 85, 140, 20));
76 btn->signal_clicked.connect(sigc::mem_fun(this, &HelloWorld::show_hello));
78 // Another label for displaying some information
79 panel->add(*(lbl_hello = new GLtk::Label));
80 lbl_hello->set_geometry(GLtk::Geometry(10, 65, 140, 20));
82 // The user might want to exit the program (*gasp*)
83 panel->add(*(btn = new GLtk::Button("Exit")));
84 btn->set_geometry(GLtk::Geometry(50, 10, 100, 20));
85 btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
87 // Done with setting things up, show the window!
91 // This function will be called periodically from the main loop
92 void HelloWorld::tick()
96 // Set up an orthogonal projection matching the root widget
97 GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(200, 200);
98 GL::MatrixStack::modelview() = GL::Matrix();
100 // Font rendering requires blending
101 GL::Bind bind_blend(GL::Blend::alpha());
108 // Displays a greeting to the user
109 void HelloWorld::show_hello()
111 lbl_hello->set_text("Hello, "+ent_name->get_text()+"!");