]> git.tdb.fi Git - libs/gltk.git/blob - helloworld.cpp
Add Container class
[libs/gltk.git] / helloworld.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2008  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 /*
9 A simple graphical Hello World application implemented with mspgltk.
10 Demonstrates some of the most common widget types.
11 */
12
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>
28
29 using namespace Msp;
30
31 // Application class.  Because it's so much nicer than global variables.
32 class HelloWorld: public Msp::Application
33 {
34 private:
35         // Objects for setting up an OpenGL window
36         Graphics::Display dpy;
37         Graphics::Window wnd;
38         Graphics::GLContext glc;
39
40         // GLtk resources and widgets
41         GLtk::Resources res;
42         RefPtr<GLtk::Root> root;
43         GLtk::Entry *ent_name;
44         GLtk::Label *lbl_hello;
45
46         // Indicate our main class to the core library
47         static Application::RegApp<HelloWorld> reg;
48
49 public:
50         HelloWorld(int, char **);
51 private:
52         virtual void tick();
53         void show_hello();
54 };
55
56 Application::RegApp<HelloWorld> HelloWorld::reg;
57
58
59 HelloWorld::HelloWorld(int, char **):
60         wnd(dpy, 200, 200),
61         glc(wnd)
62 {
63         wnd.set_title("Hello World");
64         wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
65
66         // Load resources.  This must be done before any widgets are created.
67         DataFile::load(res, "basic.skin");
68
69         // A Root receives input from a Graphics::Window and passes it on
70         root=new GLtk::Root(res, wnd);
71
72         /* Container widgets will delete their contents upon destruction so we can
73         safely forget about the pointers after setting the widgets up. */
74
75         // Panels can be used to divide the window into sub-areas
76         GLtk::Panel *panel=new GLtk::Panel(res);
77         root->add(*panel);
78         panel->set_geometry(GLtk::Geometry(20, 20, 160, 160));
79
80         GLtk::Label *lbl;
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));
84
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));
88
89         GLtk::Button *btn;
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));
94
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));
98
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));
103
104         // Font rendering requires blending
105         GL::enable(GL::BLEND);
106         GL::blend_func(GL::SRC_ALPHA, GL::ONE_MINUS_SRC_ALPHA);
107
108         // Done with setting things up, show the window!
109         wnd.show();
110 }
111
112 // This function will be called periodically from the main loop
113 void HelloWorld::tick()
114 {
115         dpy.tick();
116
117         // Set up an orthogonal projection matching the root widget
118         GL::matrix_mode(GL::PROJECTION);
119         GL::load_identity();
120         GL::ortho_bottomleft(200, 200);
121         GL::matrix_mode(GL::MODELVIEW);
122         GL::load_identity();
123
124         root->render();
125
126         glc.swap_buffers();
127 }
128
129 // Displays a greeting to the user
130 void HelloWorld::show_hello()
131 {
132         lbl_hello->set_text("Hello, "+ent_name->get_text()+"!");
133 }