Skip to content

Commit 0ef1dab

Browse files
committed
stuff
1 parent 89ea1dc commit 0ef1dab

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/ruis/render/context.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2424
using namespace ruis::render;
2525

2626
std::vector<const ruis::render::context*> ruis::render::context::cur_context_stack;
27+
std::vector<const ruis::render::context*> ruis::render::context::existing_contexts_list;
2728

2829
context::context(
2930
utki::shared_ref<ruis::render::native_window> native_window, //
@@ -32,6 +33,8 @@ context::context(
3233
native_window(std::move(native_window)),
3334
initial_matrix(std::move(params.initial_matrix))
3435
{
36+
this->existing_contexts_list.push_back(this);
37+
3538
// TODO: document this behaviour in doxygen that first created context becomes bound by default
3639
if (cur_context_stack.empty()) {
3740
// this created context is the only one existing context at the moment, bind it just to have some context always bound
@@ -42,10 +45,22 @@ context::context(
4245

4346
context::~context()
4447
{
45-
// if the context is bound during destruction then it is the last context
48+
// remove this context from list of existing contexts
49+
auto i = std::find(existing_contexts_list.begin(), existing_contexts_list.end(), this);
50+
utki::assert(i != existing_contexts_list.end(), SL);
51+
existing_contexts_list.erase(i);
52+
4653
if (this->is_current()) {
4754
cur_context_stack.pop_back();
48-
if (!cur_context_stack.empty()) {
55+
if (cur_context_stack.empty()) {
56+
if (!existing_contexts_list.empty()) {
57+
// there are no contexts in bound contexts stack, but
58+
// there are still some contexts existing, bind first one of them
59+
auto c = existing_contexts_list.front();
60+
c->native_window.get().bind_rendering_context();
61+
cur_context_stack.push_back(c);
62+
}
63+
} else {
4964
// bind the previous context
5065
cur_context_stack.back()->native_window.get().bind_rendering_context();
5166
}
@@ -58,6 +73,8 @@ context::~context()
5873
if (i != cur_context_stack.end()) {
5974
cur_context_stack.erase(i);
6075
}
76+
// this context was not the current one, so the cur_context_stack should still be not empty
77+
utki::assert(!cur_context_stack.empty(), SL);
6178
}
6279
}
6380

src/ruis/render/context.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class context : public std::enable_shared_from_this<context>
4343

4444
// Context destruction is rare, so removing from the middle of stack is rare, ok to use std::vector.
4545
static std::vector<const context*> cur_context_stack;
46+
static std::vector<const context*> existing_contexts_list;
4647

4748
protected:
4849
utki::shared_ref<context> get_shared_ref()

0 commit comments

Comments
 (0)