3 This file is part of libmspgltk
4 Copyright © 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 A simple graphical Hello World application implemented with mspgltk.
10 Demonstrates some of the most common widget types.
13 #include <msp/core/application.h>
14 #include <msp/gbase/simplewindow.h>
15 #include <msp/gl/blend.h>
16 #include <msp/gl/matrix.h>
17 #include <msp/gltk/button.h>
18 #include <msp/gltk/entry.h>
19 #include <msp/gltk/label.h>
20 #include <msp/gltk/panel.h>
21 #include <msp/gltk/resources.h>
22 #include <msp/gltk/root.h>
26 // Application class. Because it's so much nicer than global variables.
27 class HelloWorld: public Msp::Application
30 // An OpenGL window to display our widgets in
31 Graphics::SimpleGLWindow wnd;
33 // GLtk resources and widgets
36 GLtk::Entry *ent_name;
37 GLtk::Label *lbl_hello;
39 // Indicate our main class to the core library
40 static Application::RegApp<HelloWorld> reg;
43 HelloWorld(int, char **);
50 Application::RegApp<HelloWorld> HelloWorld::reg;
52 HelloWorld::HelloWorld(int, char **):
54 // Load resources. This must be done before the root widget is created.
56 // A Root receives input from a Graphics::Window and passes it on
59 wnd.set_title("Hello World");
60 wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
62 /* Container widgets will delete their contents upon destruction so we can
63 safely forget about the pointers after setting the widgets up. */
65 // Panels can be used to divide the window into sub-areas
66 GLtk::Panel *panel = new GLtk::Panel;
68 panel->set_geometry(GLtk::Geometry(20, 20, 160, 160));
71 // Prompts can be displayed with Labels
72 panel->add(*(lbl = new GLtk::Label("Type your name below:")));
73 lbl->set_geometry(GLtk::Geometry(10, 130, 140, 20));
75 // The user can type text into an Entry
76 panel->add(*(ent_name = new GLtk::Entry));
77 ent_name->set_geometry(GLtk::Geometry(10, 110, 140, 20));
80 // Buttons can be wired to cause things to happen
81 panel->add(*(btn = new GLtk::Button("Hello")));
82 btn->set_geometry(GLtk::Geometry(10, 85, 140, 20));
83 btn->signal_clicked.connect(sigc::mem_fun(this, &HelloWorld::show_hello));
85 // Another label for displaying some information
86 panel->add(*(lbl_hello = new GLtk::Label));
87 lbl_hello->set_geometry(GLtk::Geometry(10, 65, 140, 20));
89 // The user might want to exit the program (*gasp*)
90 panel->add(*(btn = new GLtk::Button("Exit")));
91 btn->set_geometry(GLtk::Geometry(50, 10, 100, 20));
92 btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
94 // Done with setting things up, show the window!
98 // This function will be called periodically from the main loop
99 void HelloWorld::tick()
103 // Set up an orthogonal projection matching the root widget
104 GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(200, 200);
105 GL::MatrixStack::modelview() = GL::Matrix();
107 // Font rendering requires blending
108 GL::Bind bind_blend(GL::Blend::alpha());
115 // Displays a greeting to the user
116 void HelloWorld::show_hello()
118 lbl_hello->set_text("Hello, "+ent_name->get_text()+"!");