source: trunk/tests/gui_test/nv_gui_test.cc @ 239

Last change on this file since 239 was 239, checked in by epyon, 11 years ago
  • massive update of mesh handling
  • universal mesh handling routines
  • removed a lot of legacy code
  • significantly streamlined MD5 loading
  • all tests updated to new features
File size: 2.7 KB
RevLine 
[127]1#include <nv/gl/gl_device.hh>
2#include <nv/gui/gui_environment.hh>
[239]3#include <nv/interface/context.hh>
[127]4#include <nv/logging.hh>
5#include <nv/logger.hh>
6#include <cstdlib> // rand
7#include <ctime> // time
8
9class application
10{
11public:
12        application();
13        bool initialize();
14        bool run();
15        void kill_window();
16        void spawn_window();
17        void recolor_window();
18        ~application();
19protected:
20        nv::device* m_device;
21        nv::window* m_window;
22        nv::clear_state m_clear_state;
23        nv::gui::environment* m_guienv;
24        std::vector<nv::gui::element*>   m_windows;
25};
26
27application::application()
28{
29        m_device = new nv::gl_device();
[229]30        m_window = m_device->create_window( 800, 600, false );
[127]31        m_clear_state.buffers = nv::clear_state::COLOR_AND_DEPTH_BUFFER;
32        m_guienv = new nv::gui::environment( m_window );
33        m_guienv->load_style( "test.style.lua" );
34}
35
36bool application::initialize()
37{
38        return true;
39}
40
41bool application::run()
42{
43        int keypress = 0;
44
45        while(!keypress)
46        {
47                m_guienv->update();
48
49                m_window->get_context()->clear( m_clear_state );
50                m_guienv->draw();
51                m_window->swap_buffers();
52
53                nv::io_event event;
54                while(m_window->poll_event(event))
55                {     
56                        switch (event.type)
57                        {
58                        case nv::EV_QUIT:
59                                keypress = 1;
60                                break;
61                        case nv::EV_KEY:
62                                if (event.key.pressed)
63                                {
64                                        switch (event.key.code)
65                                        {
66                                        case nv::KEY_N      : spawn_window(); break;
67                                        case nv::KEY_K      : kill_window(); break;
68                                        case nv::KEY_ESCAPE : keypress = 1; break;
69                                        }
70                                }
71                                break;
72                        }
73                }
74        }
75        return true;
76}
77
78void application::spawn_window()
79{
80        glm::ivec2 a( std::rand() % 600, std::rand() % 400 );
81        glm::ivec2 b( std::rand() % 200 + 40, std::rand() % 200 + 40 );
82        nv::gui::element* e = new nv::gui::element( m_guienv, nv::rectangle(a).dim(b) );
83        e->set_class( "window" );
84        e->set_text("Window "+nv::to_string(m_windows.size()+1) );
85        m_guienv->add_child( e );
86        NV_LOG( nv::LOG_INFO, "Spawn (" << a.x << "," << a.y << "x" << b.x << "," << b.y << ")" );
87        m_windows.push_back( e );
88}
89
90void application::kill_window()
91{
92        if ( m_windows.size() == 0 ) return;
93        size_t index = rand() % m_windows.size();
94        delete m_windows[ index ];
95        m_windows.erase( m_windows.begin() + index );
96}
97
98application::~application()
99{
100        m_windows.clear();
101        delete m_window;
102        delete m_device;
103}
104
105int main(int, char* [])
106{
107        std::srand((unsigned int) std::time(0) );
108        nv::logger log(nv::LOG_TRACE);
109        log.add_sink( new nv::log_file_sink("log.txt"), nv::LOG_TRACE );
110        log.add_sink( new nv::log_console_sink(), nv::LOG_TRACE );
111       
112        NV_LOG( nv::LOG_NOTICE, "Logging started" );
113        application app;
114        if ( app.initialize() )
115        {
116                app.run();
117        }
118        NV_LOG( nv::LOG_NOTICE, "Logging stopped" );
119
120        return 0;
121}
122
Note: See TracBrowser for help on using the repository browser.