Skip to content

Conversation

@fgallegosalido
Copy link
Contributor

Currently, the window contexts are stored in a std::vector. This forces us to loop through it in order to find a specific WindowContext by the system handle of the window.

Using a std::map allows us to just look for it using the system handle of the window as the key, and thus avoiding many loops. The ideal solution would be to use std::unordered_set and define a hash for the class WindowContext, but this requires at least C++11, which should be considered for future releases of ImGui-SFML, as SFML 3 will use C++17.

} else {
// no alternatives...
s_currWindowCtx = NULL;
ImGui::SetCurrentContext(NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this SetCurrentContext(NULL) is a change in behavior, previously there wasn't such an action.

Copy link
Contributor Author

@fgallegosalido fgallegosalido Mar 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember exactly the reasoning, but I think it was because, as s_currWindowCtx was being set to NULL, the ImGui context pointer was pointing to something invalid. Therefore, setting it to null too made sense to me.

Also, now that C++11 is allowed, map should be changed to unordered_map for even better performance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not just unordered_map, but std::unique_ptr should be used as well, probably. :)

Copy link
Member

@oprypin oprypin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this change, I think it should be merged as is, and then further improvements can be done with a smaller diff

Copy link
Member

@oprypin oprypin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope nevermind, it introduces a crash on the multi-window example.

c++ -I. -I../cimgui/imgui examples/multiple_windows/main.cpp imgui-SFML.cpp ../cimgui/imgui/{imgui_demo.cpp,imgui_widgets.cpp,imgui.cpp,imgui_draw.cpp,imgui_tables.cpp} -lsfml-graphics -lsfml-window -lsfml-system -lGL && ./a.out

close the Child Window

a.out: imgui-SFML.cpp:533: void ImGui::SFML::Shutdown(const sf::Window&): Assertion `false && "Window wasn't inited properly: forgot to call ImGui::SFML::Init(window)?"' failed. zsh: IOT instruction (core dumped) ./a.out 
s_windowContexts.erase(window.getSystemHandle());

// set current context to some window for convenience if needed
if (window.getSystemHandle() == s_currWindowCtx->window->getSystemHandle()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point:

 printf("%d %d\n", window.getSystemHandle(), s_currWindowCtx->window->getSystemHandle());

on Linux this prints 0 0.

That is, after a window is closed, its handle changes to 0.
So basically previously it kinda worked by accident, the condition is basically this: if the handle of the passed window is closed and the handle of the current window is closed, assume they're the same window.
Because the usage of map assumes the SystemHandle will stay constant and valid, the new logic is not equivalent and causes a crash.

@oprypin
Copy link
Member

oprypin commented Apr 1, 2022

Because I think this use of map is not viable, I keep vector but make most of the other suggested improvements in my PR #202

@eliasdaler
Copy link
Contributor

Superseded by #202

@eliasdaler eliasdaler closed this Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants