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/graphics/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::RegisteredApplication<HelloWorld>
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;
33 HelloWorld(int, char **);
40 HelloWorld::HelloWorld(int, char **):
42 // Load resources. This must be done before the root widget is created.
44 // A Root receives input from a Graphics::Window and passes it on
47 wnd.set_title("Hello World");
48 wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
50 /* Container widgets will delete their contents upon destruction so we can
51 safely forget about the pointers after setting the widgets up. */
53 // Panels can be used to divide the window into sub-areas
54 GLtk::Panel *panel = new GLtk::Panel;
56 panel->set_geometry(GLtk::Geometry(20, 20, 160, 160));
59 // Prompts can be displayed with Labels
60 panel->add(*(lbl = new GLtk::Label("Type your name below:")));
61 lbl->set_geometry(GLtk::Geometry(10, 130, 140, 20));
63 // The user can type text into an Entry
64 panel->add(*(ent_name = new GLtk::Entry));
65 ent_name->set_geometry(GLtk::Geometry(10, 110, 140, 20));
68 // Buttons can be wired to cause things to happen
69 panel->add(*(btn = new GLtk::Button("Hello")));
70 btn->set_geometry(GLtk::Geometry(10, 85, 140, 20));
71 btn->signal_clicked.connect(sigc::mem_fun(this, &HelloWorld::show_hello));
73 // Another label for displaying some information
74 panel->add(*(lbl_hello = new GLtk::Label));
75 lbl_hello->set_geometry(GLtk::Geometry(10, 65, 140, 20));
77 // The user might want to exit the program (*gasp*)
78 panel->add(*(btn = new GLtk::Button("Exit")));
79 btn->set_geometry(GLtk::Geometry(50, 10, 100, 20));
80 btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
82 // Done with setting things up, show the window!
86 // This function will be called periodically from the main loop
87 void HelloWorld::tick()
91 // Set up an orthogonal projection matching the root widget
92 GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(200, 200);
93 GL::MatrixStack::modelview() = GL::Matrix();
95 // Font rendering requires blending
96 GL::Bind bind_blend(GL::Blend::alpha());
103 // Displays a greeting to the user
104 void HelloWorld::show_hello()
106 lbl_hello->set_text("Hello, "+ent_name->get_text()+"!");