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/core/refptr.h>
15 #include <msp/gbase/display.h>
16 #include <msp/gbase/glcontext.h>
17 #include <msp/gbase/window.h>
18 #include <msp/gl/blend.h>
19 #include <msp/gl/matrix.h>
20 #include <msp/gl/misc.h>
21 #include <msp/gl/projection.h>
22 #include <msp/gltk/button.h>
23 #include <msp/gltk/entry.h>
24 #include <msp/gltk/label.h>
25 #include <msp/gltk/panel.h>
26 #include <msp/gltk/resources.h>
27 #include <msp/gltk/root.h>
31 // Application class. Because it's so much nicer than global variables.
32 class HelloWorld: public Msp::Application
35 // Objects for setting up an OpenGL window
36 Graphics::Display dpy;
38 Graphics::GLContext glc;
40 // GLtk resources and widgets
42 RefPtr<GLtk::Root> root;
43 GLtk::Entry *ent_name;
44 GLtk::Label *lbl_hello;
46 // Indicate our main class to the core library
47 static Application::RegApp<HelloWorld> reg;
50 HelloWorld(int, char **);
56 Application::RegApp<HelloWorld> HelloWorld::reg;
59 HelloWorld::HelloWorld(int, char **):
63 wnd.set_title("Hello World");
64 wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
66 // Load resources. This must be done before any widgets are created.
67 DataFile::load(res, "basic.skin");
69 // A Root receives input from a Graphics::Window and passes it on
70 root=new GLtk::Root(res, wnd);
72 /* Container widgets will delete their contents upon destruction so we can
73 safely forget about the pointers after setting the widgets up. */
75 // Panels can be used to divide the window into sub-areas
76 GLtk::Panel *panel=new GLtk::Panel(res);
78 panel->set_geometry(GLtk::Geometry(20, 20, 160, 160));
81 // Prompts can be displayed with Labels
82 panel->add(*(lbl=new GLtk::Label(res, "Type your name below:")));
83 lbl->set_geometry(GLtk::Geometry(10, 130, 140, 20));
85 // The user can type text into an Entry
86 panel->add(*(ent_name=new GLtk::Entry(res)));
87 ent_name->set_geometry(GLtk::Geometry(10, 110, 140, 20));
90 // Buttons can be wired to cause things to happen
91 panel->add(*(btn=new GLtk::Button(res, "Hello")));
92 btn->set_geometry(GLtk::Geometry(10, 85, 140, 20));
93 btn->signal_clicked.connect(sigc::mem_fun(this, &HelloWorld::show_hello));
95 // Another label for displaying some information
96 panel->add(*(lbl_hello=new GLtk::Label(res)));
97 lbl_hello->set_geometry(GLtk::Geometry(10, 65, 140, 20));
99 // The user might want to exit the program (*gasp*)
100 panel->add(*(btn=new GLtk::Button(res, "Exit")));
101 btn->set_geometry(GLtk::Geometry(50, 10, 100, 20));
102 btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
104 // Font rendering requires blending
105 GL::enable(GL::BLEND);
106 GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
108 // Done with setting things up, show the window!
112 // This function will be called periodically from the main loop
113 void HelloWorld::tick()
117 // Set up an orthogonal projection matching the root widget
118 GL::matrix_mode(GL::PROJECTION);
120 GL::ortho_bottomleft(200, 200);
121 GL::matrix_mode(GL::MODELVIEW);
129 // Displays a greeting to the user
130 void HelloWorld::show_hello()
132 lbl_hello->set_text("Hello, "+ent_name->get_text()+"!");