Skip to content

Commit d86196c

Browse files
committed
stuff
1 parent 5cbf10a commit d86196c

File tree

11 files changed

+58
-26
lines changed

11 files changed

+58
-26
lines changed

src/ruis/context.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class context : public std::enable_shared_from_this<context>
112112
* @brief Shorthand alias for native_window.
113113
* @return The native window of this GUI context.
114114
*/
115-
ruis::native_window& window() noexcept
115+
ruis::render::native_window& window() noexcept
116116
{
117117
return this->ren().ctx().native_window;
118118
}

src/ruis/render/context.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,49 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2323

2424
using namespace ruis::render;
2525

26+
const ruis::render::context* ruis::render::context::cur_context = nullptr;
27+
2628
context::context(
27-
utki::shared_ref<ruis::native_window> native_window, //
29+
utki::shared_ref<ruis::render::native_window> native_window, //
2830
parameters params
2931
) :
3032
native_window(std::move(native_window)),
3133
initial_matrix(std::move(params.initial_matrix))
3234
{}
3335

36+
context::~context()
37+
{
38+
if (this->is_current()) {
39+
cur_context = nullptr;
40+
}
41+
}
42+
3443
void context::apply(std::function<void()> func)
3544
{
45+
utki::assert(func, SL);
46+
3647
if (this->is_current()) {
48+
// this context is already current
3749
func();
3850
return;
3951
}
4052

41-
throw std::logic_error("context::apply(): the context is non-current, switching context is not yet implemented");
53+
auto old_cc = cur_context;
54+
utki::scope_exit restore_old_cc_scope_exit([&]() {
55+
if (!old_cc) {
56+
// There was no current context, leave current one bound.
57+
return;
58+
}
59+
old_cc->native_window.get().bind_render_context();
60+
cur_context = old_cc;
61+
});
62+
63+
cur_context = this;
64+
this->native_window.get().bind_render_context();
65+
66+
utki::assert(this->is_current(), SL);
67+
68+
func();
4269
}
4370

4471
void context::set_framebuffer(frame_buffer* fb)

src/ruis/render/context.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class context : public std::enable_shared_from_this<context>
3939
{
4040
friend class frame_buffer;
4141

42+
static const context* cur_context;
43+
4244
protected:
4345
utki::shared_ref<context> get_shared_ref()
4446
{
@@ -49,7 +51,7 @@ class context : public std::enable_shared_from_this<context>
4951
std::weak_ptr<frame_buffer> cur_fb;
5052

5153
public:
52-
const utki::shared_ref<ruis::native_window> native_window;
54+
const utki::shared_ref<ruis::render::native_window> native_window;
5355

5456
struct parameters {
5557
r4::matrix4<float> initial_matrix;
@@ -63,7 +65,7 @@ class context : public std::enable_shared_from_this<context>
6365
const r4::matrix4<float> initial_matrix;
6466

6567
context(
66-
utki::shared_ref<ruis::native_window>, //
68+
utki::shared_ref<ruis::render::native_window>, //
6769
parameters params
6870
);
6971

@@ -73,12 +75,11 @@ class context : public std::enable_shared_from_this<context>
7375
context(context&&) = delete;
7476
context& operator=(context&&) = delete;
7577

76-
virtual ~context() = default;
78+
virtual ~context();
7779

7880
bool is_current() const noexcept
7981
{
80-
// TODO: actually check that the context is current
81-
return true;
82+
return cur_context == this;
8283
}
8384

8485
/**

src/ruis/render/native_window.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <utki/debug.hpp>
44

5-
using namespace ruis;
5+
using namespace ruis::render;
66

77
void native_window::set_fullscreen(bool enable)
88
{

src/ruis/render/native_window.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
#include "../util/mouse_cursor.hpp"
66

7-
namespace ruis {
7+
namespace ruis::render {
8+
class context;
9+
810
// TODO: doxygen
911
class native_window
1012
{
13+
friend class ruis::render::context;
14+
1115
bool is_fullscreen_v = false;
1216

1317
virtual void bind_render_context() = 0;
@@ -41,4 +45,4 @@ class native_window
4145

4246
virtual void set_mouse_cursor_visible(bool visible) {}
4347
};
44-
} // namespace ruis
48+
} // namespace ruis::render

src/ruis/widget/group/tiling_area.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class dragger : public ruis::gap
3838

3939
tiling_area& owner;
4040

41-
ruis::native_window::cursor_id arrows_cursor_iter;
41+
ruis::render::native_window::cursor_id arrows_cursor_iter;
4242

4343
public:
4444
// TODO: use shared_ref?

src/ruis/widget/group/window.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ void ruis::window::setup_widgets()
416416
this->title_bg = this->try_get_widget_as<color_widget>("ruis_window_title_bg");
417417
ASSERT(this->title_bg);
418418

419-
auto make_mouse_button_handler = [this](native_window::cursor_id& iter) {
419+
auto make_mouse_button_handler = [this](ruis::render::native_window::cursor_id& iter) {
420420
return decltype(mouse_proxy::mouse_button_handler)([this, &iter](mouse_proxy& mp, const mouse_button_event& e) {
421421
if (e.button != mouse_button::left) {
422422
return false;
@@ -435,7 +435,7 @@ void ruis::window::setup_widgets()
435435
});
436436
};
437437

438-
auto make_hovered_change_handler = [this](ruis::mouse_cursor cursor, native_window::cursor_id& iter) {
438+
auto make_hovered_change_handler = [this](ruis::mouse_cursor cursor, render::native_window::cursor_id& iter) {
439439
return [this, cursor, &iter](mouse_proxy& mp, unsigned pointer_id) {
440440
// LOG("hover = " << mp.is_hovered() << std::endl)
441441
// LOG("this->mouse_captured = " << this->mouse_captured << std::endl)

src/ruis/widget/group/window.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ class window :
6363
std::shared_ptr<widget> lb_border;
6464
std::shared_ptr<widget> rb_border;
6565

66-
native_window::cursor_id caption_cursor_iter;
67-
native_window::cursor_id lt_border_cursor_iter;
68-
native_window::cursor_id rt_border_cursor_iter;
69-
native_window::cursor_id t_border_cursor_iter;
70-
native_window::cursor_id l_border_cursor_iter;
71-
native_window::cursor_id r_border_cursor_iter;
72-
native_window::cursor_id b_border_cursor_iter;
73-
native_window::cursor_id lb_border_cursor_iter;
74-
native_window::cursor_id rb_border_cursor_iter;
66+
render::native_window::cursor_id caption_cursor_iter;
67+
render::native_window::cursor_id lt_border_cursor_iter;
68+
render::native_window::cursor_id rt_border_cursor_iter;
69+
render::native_window::cursor_id t_border_cursor_iter;
70+
render::native_window::cursor_id l_border_cursor_iter;
71+
render::native_window::cursor_id r_border_cursor_iter;
72+
render::native_window::cursor_id b_border_cursor_iter;
73+
render::native_window::cursor_id lb_border_cursor_iter;
74+
render::native_window::cursor_id rb_border_cursor_iter;
7575

7676
bool mouse_captured = false;
7777
ruis::vector2 capture_point;

0 commit comments

Comments
 (0)