From: Mikko Rasa Date: Thu, 25 Aug 2016 20:29:54 +0000 (+0300) Subject: Move the helloworld example into a subdirectory X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=467e235bb1f647b618763280ca2a1ce13dcdbd72;p=libs%2Fgltk.git Move the helloworld example into a subdirectory --- diff --git a/Build b/Build index ea9ef31..969993e 100644 --- a/Build +++ b/Build @@ -20,7 +20,7 @@ package "mspgltk" program "helloworld" { - source "helloworld.cpp"; + source "examples/helloworld.cpp"; require "sigc++-2.0"; use "mspgltk"; }; diff --git a/examples/helloworld.cpp b/examples/helloworld.cpp new file mode 100644 index 0000000..4f85964 --- /dev/null +++ b/examples/helloworld.cpp @@ -0,0 +1,107 @@ +/* +A simple graphical Hello World application implemented with mspgltk. +Demonstrates some of the most common widget types. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace Msp; + +// Application class. Because it's so much nicer than global variables. +class HelloWorld: public Msp::RegisteredApplication +{ +private: + // An OpenGL window to display our widgets in + Graphics::SimpleGLWindow wnd; + + // GLtk resources and widgets + GLtk::Resources res; + GLtk::Root root; + GLtk::Entry *ent_name; + GLtk::Label *lbl_hello; + +public: + HelloWorld(int, char **); +private: + virtual void tick(); + void show_hello(); +}; + + +HelloWorld::HelloWorld(int, char **): + wnd(200, 200), + // Load resources. This must be done before the root widget is created. + res("basic.skin"), + // A Root receives input from a Graphics::Window and passes it on + root(res, wnd) +{ + wnd.set_title("Hello World"); + wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0)); + + /* Container widgets will delete their contents upon destruction so we can + safely forget about the pointers after setting the widgets up. */ + + // Panels can be used to divide the window into sub-areas + GLtk::Panel *panel = new GLtk::Panel; + root.add(*panel); + panel->set_geometry(GLtk::Geometry(20, 20, 160, 160)); + + GLtk::Label *lbl; + // Prompts can be displayed with Labels + panel->add(*(lbl = new GLtk::Label("Type your name below:"))); + lbl->set_geometry(GLtk::Geometry(10, 130, 140, 20)); + + // The user can type text into an Entry + panel->add(*(ent_name = new GLtk::Entry)); + ent_name->set_geometry(GLtk::Geometry(10, 110, 140, 20)); + + GLtk::Button *btn; + // Buttons can be wired to cause things to happen + panel->add(*(btn = new GLtk::Button("Hello"))); + btn->set_geometry(GLtk::Geometry(10, 85, 140, 20)); + btn->signal_clicked.connect(sigc::mem_fun(this, &HelloWorld::show_hello)); + + // Another label for displaying some information + panel->add(*(lbl_hello = new GLtk::Label)); + lbl_hello->set_geometry(GLtk::Geometry(10, 65, 140, 20)); + + // The user might want to exit the program (*gasp*) + panel->add(*(btn = new GLtk::Button("Exit"))); + btn->set_geometry(GLtk::Geometry(50, 10, 100, 20)); + btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0)); + + // Done with setting things up, show the window! + wnd.show(); +} + +// This function will be called periodically from the main loop +void HelloWorld::tick() +{ + wnd.tick(); + + // Set up an orthogonal projection matching the root widget + GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(200, 200); + GL::MatrixStack::modelview() = GL::Matrix(); + + // Font rendering requires blending + GL::Bind bind_blend(GL::Blend::alpha()); + + root.render(); + + wnd.swap_buffers(); +} + +// Displays a greeting to the user +void HelloWorld::show_hello() +{ + lbl_hello->set_text("Hello, "+ent_name->get_text()+"!"); +} diff --git a/helloworld.cpp b/helloworld.cpp deleted file mode 100644 index 4f85964..0000000 --- a/helloworld.cpp +++ /dev/null @@ -1,107 +0,0 @@ -/* -A simple graphical Hello World application implemented with mspgltk. -Demonstrates some of the most common widget types. -*/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace Msp; - -// Application class. Because it's so much nicer than global variables. -class HelloWorld: public Msp::RegisteredApplication -{ -private: - // An OpenGL window to display our widgets in - Graphics::SimpleGLWindow wnd; - - // GLtk resources and widgets - GLtk::Resources res; - GLtk::Root root; - GLtk::Entry *ent_name; - GLtk::Label *lbl_hello; - -public: - HelloWorld(int, char **); -private: - virtual void tick(); - void show_hello(); -}; - - -HelloWorld::HelloWorld(int, char **): - wnd(200, 200), - // Load resources. This must be done before the root widget is created. - res("basic.skin"), - // A Root receives input from a Graphics::Window and passes it on - root(res, wnd) -{ - wnd.set_title("Hello World"); - wnd.signal_close.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0)); - - /* Container widgets will delete their contents upon destruction so we can - safely forget about the pointers after setting the widgets up. */ - - // Panels can be used to divide the window into sub-areas - GLtk::Panel *panel = new GLtk::Panel; - root.add(*panel); - panel->set_geometry(GLtk::Geometry(20, 20, 160, 160)); - - GLtk::Label *lbl; - // Prompts can be displayed with Labels - panel->add(*(lbl = new GLtk::Label("Type your name below:"))); - lbl->set_geometry(GLtk::Geometry(10, 130, 140, 20)); - - // The user can type text into an Entry - panel->add(*(ent_name = new GLtk::Entry)); - ent_name->set_geometry(GLtk::Geometry(10, 110, 140, 20)); - - GLtk::Button *btn; - // Buttons can be wired to cause things to happen - panel->add(*(btn = new GLtk::Button("Hello"))); - btn->set_geometry(GLtk::Geometry(10, 85, 140, 20)); - btn->signal_clicked.connect(sigc::mem_fun(this, &HelloWorld::show_hello)); - - // Another label for displaying some information - panel->add(*(lbl_hello = new GLtk::Label)); - lbl_hello->set_geometry(GLtk::Geometry(10, 65, 140, 20)); - - // The user might want to exit the program (*gasp*) - panel->add(*(btn = new GLtk::Button("Exit"))); - btn->set_geometry(GLtk::Geometry(50, 10, 100, 20)); - btn->signal_clicked.connect(sigc::bind(sigc::mem_fun(this, &HelloWorld::exit), 0)); - - // Done with setting things up, show the window! - wnd.show(); -} - -// This function will be called periodically from the main loop -void HelloWorld::tick() -{ - wnd.tick(); - - // Set up an orthogonal projection matching the root widget - GL::MatrixStack::projection() = GL::Matrix::ortho_bottomleft(200, 200); - GL::MatrixStack::modelview() = GL::Matrix(); - - // Font rendering requires blending - GL::Bind bind_blend(GL::Blend::alpha()); - - root.render(); - - wnd.swap_buffers(); -} - -// Displays a greeting to the user -void HelloWorld::show_hello() -{ - lbl_hello->set_text("Hello, "+ent_name->get_text()+"!"); -}