Skip to content

Commit 3e1b9ef

Browse files
committed
stuff
1 parent 4d0f6b5 commit 3e1b9ef

10 files changed

Lines changed: 758 additions & 420 deletions

File tree

src/ruisapp/application.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class application : public utki::intrusive_singleton<application>
186186
* @return shared_ref to the created window object.
187187
*/
188188
// TODO: allow injecting own style provider (along with loader)
189-
ruisapp::window& make_window(const window_parameters& window_params);
189+
ruisapp::window& make_window(window_parameters window_params);
190190

191191
/**
192192
* @brief Destroy native window.

src/ruisapp/glue/macos/application.hxx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ public:
2222
utki::shared_ref<native_window> ruis_native_window
2323
);
2424

25+
// needed for using with std::set
2526
bool operator<(const app_window& w) const noexcept
2627
{
2728
return this < &w;
2829
}
2930

31+
// needed for using with std::set
3032
bool operator==(const app_window& w) const noexcept
3133
{
3234
return this == &w;

src/ruisapp/glue/windows/application.cxx

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,151 @@ ruisapp::application::directories get_application_directories(std::string_view a
3939
}
4040
} // namespace
4141

42+
application_glue::application_glue(const utki::version_duplet& gl_version) :
43+
gl_version(gl_version),
44+
shared_gl_context_native_window(utki::make_shared<native_window>(
45+
this->display,
46+
this->gl_version,
47+
ruisapp::window_parameters{
48+
.dims = {1, 1},
49+
.fullscreen = false,
50+
.visible = false
51+
},
52+
nullptr // no shared gl context
53+
)),
54+
resource_loader_ruis_rendering_context([&]() {
55+
utki::logcat_debug("application_glue::application_glue(): creating shared gl context", '\n');
56+
auto c = utki::make_shared<ruis::render::opengl::context>(this->shared_gl_context_native_window);
57+
utki::logcat_debug("application_glue::application_glue(): shared gl context created", '\n');
58+
return c;
59+
}()),
60+
common_shaders(this->resource_loader_ruis_rendering_context.get().make_shaders()),
61+
common_render_objects(
62+
utki::make_shared<ruis::render::renderer::objects>(this->resource_loader_ruis_rendering_context)
63+
),
64+
ruis_resource_loader( //
65+
utki::make_shared<ruis::resource_loader>(
66+
this->resource_loader_ruis_rendering_context, //
67+
this->common_render_objects
68+
)
69+
),
70+
ruis_style_provider( //
71+
utki::make_shared<ruis::style_provider>(this->ruis_resource_loader)
72+
)
73+
{}
74+
75+
void application_glue::render() {
76+
for (auto& w : this->windows) {
77+
w.second.get().render();
78+
}
79+
}
80+
81+
app_window* application_glue::get_window(native_window::window_id_type id) {
82+
auto i = this->windows.find(id);
83+
if (i == this->windows.end()) {
84+
return nullptr;
85+
}
86+
87+
return &i->second.get();
88+
}
89+
90+
ruisapp::window& application_glue::make_window(ruisapp::window_parameters window_params) {
91+
auto ruis_native_window = utki::make_shared<native_window>(
92+
this->display,
93+
this->gl_version,
94+
window_params,
95+
&this->shared_gl_context_native_window.get()
96+
);
97+
98+
auto ruis_context = utki::make_shared<ruis::context>(ruis::context::parameters{
99+
.post_to_ui_thread_function =
100+
[this](std::function<void()> procedure) {
101+
if (PostMessage(
102+
NULL, // post message to UI thread's message queue
103+
WM_USER,
104+
0, // no wParam
105+
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-pro-type-reinterpret-cast)
106+
reinterpret_cast<LPARAM>(
107+
new std::remove_reference_t<decltype(procedure)>(std::move(procedure))
108+
)
109+
) == 0)
110+
{
111+
throw std::runtime_error("PostMessage(): failed");
112+
}
113+
},
114+
.updater = this->updater,
115+
.renderer = utki::make_shared<ruis::render::renderer>(
116+
#ifdef RUISAPP_RENDER_OPENGL
117+
utki::make_shared<ruis::render::opengl::context>(ruis_native_window),
118+
#elif defined(RUISAPP_RENDER_OPENGLES)
119+
utki::make_shared<ruis::render::opengles::context>(ruis_native_window),
120+
#else
121+
# error "Unknown graphics API"
122+
#endif
123+
this->common_shaders,
124+
this->common_render_objects
125+
),
126+
.style_provider = this->ruis_style_provider
127+
});
128+
129+
auto ruisapp_window = utki::make_shared<app_window>(
130+
std::move(ruis_context), //
131+
std::move(ruis_native_window)
132+
);
133+
134+
ruisapp_window.get().gui.set_viewport( //
135+
ruis::rect(
136+
0, //
137+
0,
138+
ruis::real(window_params.dims.x()),
139+
ruis::real(window_params.dims.y())
140+
)
141+
);
142+
143+
auto res = this->windows.insert( //
144+
std::make_pair(
145+
ruisapp_window.get().ruis_native_window.get().get_id(), //
146+
std::move(ruisapp_window)
147+
)
148+
);
149+
utki::assert(res.second, SL);
150+
151+
return res.first->second.get();
152+
}
153+
154+
void application_glue::destroy_window(native_window::window_id_type id){
155+
auto i = this->windows.find(id);
156+
utki::assert(i != this->windows.end(), SL);
157+
158+
this->windows_to_destroy.push_back(std::move(i->second));
159+
this->windows.erase(i);
160+
}
161+
42162
ruisapp::application::application(parameters params) :
43163
application(
44164
utki::make_unique<application_glue>(params.graphics_api_version), //
45165
get_application_directories(params.name),
46166
std::move(params)
47167
)
48168
{}
169+
170+
void ruisapp::application::quit() noexcept{
171+
auto& glue = get_glue(*this);
172+
173+
PostQuitMessage(
174+
0 // exit code
175+
);
176+
}
177+
178+
ruisapp::window& ruisapp::application::make_window(ruisapp::window_parameters window_params) {
179+
auto& glue = get_glue(*this);
180+
return glue.make_window(std::move(window_params));
181+
}
182+
183+
void ruisapp::application::destroy_window(ruisapp::window& w) {
184+
auto& glue = get_glue(*this);
185+
186+
utki::assert(dynamic_cast<app_window*>(&w), SL);
187+
auto& app_win = static_cast<app_window&>(w);
188+
glue.destroy_window(app_win.ruis_native_window.get().get_id());
189+
}

src/ruisapp/glue/windows/application.hxx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,60 @@
11
#pragma once
22

3+
#include <atomic>
4+
#include <map>
5+
36
#include "../../application.hpp"
47

8+
#include "display.hxx"
9+
#include "window.hxx"
10+
11+
namespace {
12+
class app_window : public ruisapp::window{
13+
public:
14+
const utki::shared_ref<native_window> ruis_native_window;
15+
16+
app_window(
17+
utki::shared_ref<ruis::context> ruis_context, //
18+
utki::shared_ref<native_window> ruis_native_window
19+
):
20+
ruisapp::window(std::move(ruis_context)),
21+
ruis_native_window(std::move(ruis_native_window))
22+
{}
23+
};
24+
}
25+
526
namespace {
627
class application_glue : public utki::destructable
728
{
29+
const utki::version_duplet gl_version;
30+
31+
utki::shared_ref<display_wrapper> display = utki::make_shared<display_wrapper>();
32+
33+
const utki::shared_ref<native_window> shared_gl_context_native_window;
34+
const utki::shared_ref<ruis::render::context> resource_loader_ruis_rendering_context;
35+
const utki::shared_ref<const ruis::render::context::shaders> common_shaders;
36+
const utki::shared_ref<const ruis::render::renderer::objects> common_render_objects;
37+
const utki::shared_ref<ruis::resource_loader> ruis_resource_loader;
38+
const utki::shared_ref<ruis::style_provider> ruis_style_provider;
39+
40+
std::map<
41+
native_window::window_id_type,
42+
utki::shared_ref<app_window>> windows;
843
public:
44+
std::atomic_bool quit_flag = false;
45+
46+
const utki::shared_ref<ruis::updater> updater = utki::make_shared<ruis::updater>();
47+
48+
std::vector<utki::shared_ref<app_window>> windows_to_destroy;
49+
50+
application_glue(const utki::version_duplet& gl_version);
51+
52+
ruisapp::window& make_window(ruisapp::window_parameters window_params);
53+
void destroy_window(native_window::window_id_type id);
54+
55+
app_window* get_window(native_window::window_id_type id);
56+
57+
void render();
958
};
1059
} // namespace
1160

0 commit comments

Comments
 (0)