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