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