I've been trying to wrap my head around why I am not able to use a GLFW window handle cross dynamic library boundaries. This is most likely due to lack of understanding how GLFW works under the hood, but I would really appreciate if someone could shed some light on what could be the problem here.
I have a real-time rendering project that currently consists of the following parts:
- core.dll
- shader-editor.exe
The core part contains all GLEW and GLFW initialization, context and window creation code (such as glewInit
, glfwMakeContextCurrent
, etc). This is then compiled as a dynamic library.
The shader-editor dynamically links to core.dll and relies on core.dll to do all initialization needed for GLFW and GLEW.
When having this setup I came across some weird behavior I did not expect:
- In order to use any OpenGL functions pointers outside of the dynamic library (in e.g. shader-editor.exe) I still need to explicitely call
glewInit
to setup these pointers (otherwise I get various seg faults). This I can kinda buy as the initialization is done in the source file in this case, so either the fix here would be to callglewInit
again (fromshader-editor.exe
) or do theglewInit
call in the header file. However, doing it the header file forces the consumer of this dynamic library to also depend on glew, which I kinda want to avoid. - I expose the window that has been created from within
core.dll
with a simple getter function that theshader-editor.exe
uses to get the window handle. However, when I try to use that window handle it seems like it cannot be used from outside thecore.dll
as I can't query for basic window information. E.g. somwhere inshader-editor.exe
I would do
GLFWwindow* window = this->window_manager_->GetWindow(); // window_manager is instantiated in `core.dll` and has prior to this line created a window and initialized a working GLFW context.
int w, h;
int display_w, display_w;
glfwGetWindowSize(window, &w, &h);
glfwGetFramebufferSize(window, &display_w, &display_h);
In the code above display_w
, display_w
, w
and h
all come back as 0. However if I run the same code within the this->window_manager_->GetWindow()
function I get the proper widths and heights back.
It might also be worth noting that both GLFW and GLEW is properly initialized as I can render my meshes and so on without any issues.
Appreciate any input, thanks!