]> git.tdb.fi Git - libs/gltk.git/blob - helloworld.cpp
Update helloworld to compile again
[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/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>
23
24 using namespace Msp;
25
26 // Application class.  Because it's so much nicer than global variables.
27 class HelloWorld: public Msp::Application
28 {
29 private:
30         // An OpenGL window to display our widgets in
31         Graphics::SimpleGLWindow wnd;
32
33         // GLtk resources and widgets
34         GLtk::Resources res;
35         GLtk::Root root;
36         GLtk::Entry *ent_name;
37         GLtk::Label *lbl_hello;
38
39         // Indicate our main class to the core library
40         static Application::RegApp<HelloWorld> reg;
41
42 public:
43         HelloWorld(int, char **);
44 private:
45         virtual void tick();
46         void show_hello();
47 };
48
49
50 Application::RegApp<HelloWorld> HelloWorld::reg;
51
52 HelloWorld::HelloWorld(int, char **):
53         wnd(200, 200),
54         // Load resources.  This must be done before the root widget is created.
55         res("basic.skin"),
56         // A Root receives input from a Graphics::Window and passes it on
57         root(res, wnd)
58 {
59         wnd.set_title("Hello World");
60         wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0));
61
62         /* Container widgets will delete their contents upon destruction so we can
63         safely forget about the pointers after setting the widgets up. */
64
65         // Panels can be used to divide the window into sub-areas
66         GLtk::Panel *panel = new GLtk::Panel;
67         root.add(*panel);
68         panel->set_geometry(GLtk::Geometry(20, 20, 160, 160));
69
70         GLtk::Label *lbl;
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));
74
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));
78
79         GLtk::Button *btn;
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));
84
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));
88
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));
93
94         // Done with setting things up, show the window!
95         wnd.show();
96 }
97
98 // This function will be called periodically from the main loop
99 void HelloWorld::tick()
100 {
101         wnd.tick();
102
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();
106
107         // Font rendering requires blending
108         GL::Bind bind_blend(GL::Blend::alpha());
109
110         root.render();
111
112         wnd.swap_buffers();
113 }
114
115 // Displays a greeting to the user
116 void HelloWorld::show_hello()
117 {
118         lbl_hello->set_text("Hello, "+ent_name->get_text()+"!");
119 }