@@ -24,6 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2424using namespace ruis ::render;
2525
2626std::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
2829context::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
4346context::~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
0 commit comments