-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer.cpp
163 lines (124 loc) · 3.59 KB
/
viewer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "viewer.h"
#include "core/log.h"
#include "glfw/base.h"
#include "glfw/window.h"
#include <iostream>
viewer::viewer(const window_settings& settings)
: m_frameclock(1024)
{
if constexpr (core::g_logging)
{
auto& log = core::log::instance();
log.add_sink(core::log::level::info, [](const auto& msg){ std::cout << msg.text << std::endl; });
log.add_sink(core::log::level::warning, [](const auto& msg){ std::cout << msg.text << std::endl; });
log.add_sink(core::log::level::error, [](const auto& msg){ std::cerr << msg.file << ":" << msg.line << ":" << msg.func << " " << msg.text << std::endl; });
}
/*----- event listener ----*/
m_msg_bus.connect<msg::window_resize>(this);
m_msg_bus.connect<msg::key>(this);
m_msg_bus.connect<msg::mouse_button>(this);
glfw::startup();
m_window = std::make_unique<glfw::window>(m_msg_bus, settings.title, settings.width, settings.heigth);
m_window->vsync(settings.vsync);
m_glcontext.reset(new opengl::context(static_cast<glfw::window&>(*m_window)));
m_glcontext->clear_color(0, 0, 0, 1);
m_glcontext->clear(opengl::clear_options::all);
m_window->display();
m_imgui = std::make_unique<imgui::manager>(m_msg_bus, *m_window, *m_glcontext);
m_camera.perspective(settings.width, settings.heigth, 0.25f*glm::pi<float>(), 0.01f, 20.0f);
camera_control<util::orbit_control>();
}
viewer::~viewer()
{
m_glcontext.reset();
m_window.reset();
glfw::shutdown();
/*----- event listener ----*/
m_msg_bus.disconnect<msg::window_resize>(this);
m_msg_bus.disconnect<msg::key>(this);
m_msg_bus.disconnect<msg::mouse_button>(this);
if constexpr (core::g_logging)
{
auto& log = core::log::instance();
log.clear();
}
}
core::window& viewer::window()
{
return *m_window;
}
opengl::context& viewer::context()
{
return *m_glcontext;
}
core::msg_bus &viewer::msg_bus()
{
return m_msg_bus;
}
util::camera& viewer::camera()
{
return m_camera;
}
const core::frameclock& viewer::frameclock() const
{
return m_frameclock;
}
void viewer::on_key(const key_cb& func)
{
m_key_cb = func;
}
void viewer::on_mouse_button(const mouse_cb& func)
{
m_mouse_cb = func;
}
void viewer::on_resize(const resize_cb& func)
{
m_resize_cb = func;
}
void viewer::on_render(const render_cb& func)
{
m_render_cb = func;
}
void viewer::on_update(const update_cb& func)
{
m_update_cb = func;
}
void viewer::on_gui(const gui_cb& func)
{
m_gui_cb = func;
}
void viewer::run()
{
m_frameclock.start();
while(!m_window->closed())
{
m_controls->ignore(m_imgui->mouse_captured());
glfw::poll_events();
auto dt = core::time_cast<core::sec>(m_frameclock.restart());
if(m_update_cb) { m_update_cb(*m_window, dt); }
m_glcontext->clear(opengl::clear_options::all);
{
if(m_render_cb) { m_render_cb(*m_window, dt); }
m_imgui->new_frame(dt);
{
if(m_gui_cb) { m_gui_cb(*m_window, dt); }
}
m_imgui->end_frame();
}
m_window->display();
}
}
void viewer::receive(const msg::window_resize& msg)
{
m_glcontext->viewport(0, 0, msg.width, msg.height);
m_camera.size(msg.width, msg.height);
if(m_resize_cb) { m_resize_cb(*m_window, msg.width, msg.height); }
}
void viewer::receive(const msg::mouse_button& msg)
{
if(m_mouse_cb) { m_mouse_cb(*m_window, msg.button, msg.position, msg.pressed); }
}
void viewer::receive(const msg::key& msg)
{
if(m_key_cb) { m_key_cb(*m_window, msg.key, msg.pressed); }
}