]> git.tdb.fi Git - libs/gltk.git/blob - helloworld.cpp
Strip copyright messages and id tags from individual files
[libs/gltk.git] / helloworld.cpp
1 /*
2 A simple graphical Hello World application implemented with mspgltk.
3 Demonstrates some of the most common widget types.
4 */
5
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>
16
17 using namespace Msp;
18
19 // Application class.  Because it's so much nicer than global variables.
20 class HelloWorld: public Msp::Application
21 {
22 private:
23         // An OpenGL window to display our widgets in
24         Graphics::SimpleGLWindow wnd;
25
26         // GLtk resources and widgets
27         GLtk::Resources res;
28         GLtk::Root root;
29         GLtk::Entry *ent_name;
30         GLtk::Label *lbl_hello;
31
32         // Indicate our main class to the core library
33         static Application::RegApp<HelloWorld> reg;
34
35 public:
36         HelloWorld(int, char **);
37 private:
38         virtual void tick();
39         void show_hello();
40 };
41
42
43 Application::RegApp<HelloWorld> HelloWorld::reg;
44
45 HelloWorld::HelloWorld(int, char **):
46         wnd(200, 200),
47         // Load resources.  This must be done before the root widget is created.
48         res("basic.skin"),
49         // A Root receives input from a Graphics::Window and passes it on
50         root(res, wnd)
51 {
52         wnd.set_title("Hello World");
53         wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
54
55         /* Container widgets will delete their contents upon destruction so we can
56         safely forget about the pointers after setting the widgets up. */
57
58         // Panels can be used to divide the window into sub-areas
59         GLtk::Panel *panel = new GLtk::Panel;
60         root.add(*panel);
61         panel->set_geometry(GLtk::Geometry(20, 20, 160, 160));
62
63         GLtk::Label *lbl;
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));
67
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));
71
72         GLtk::Button *btn;
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));
77
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));
81
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));
86
87         // Done with setting things up, show the window!
88         wnd.show();
89 }
90
91 // This function will be called periodically from the main loop
92 void HelloWorld::tick()
93 {
94         wnd.tick();
95
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();
99
100         // Font rendering requires blending
101         GL::Bind bind_blend(GL::Blend::alpha());
102
103         root.render();
104
105         wnd.swap_buffers();
106 }
107
108 // Displays a greeting to the user
109 void HelloWorld::show_hello()
110 {
111         lbl_hello->set_text("Hello, "+ent_name->get_text()+"!");
112 }