1

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:

  1. 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 call glewInit again (from shader-editor.exe) or do the glewInit 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.
  2. I expose the window that has been created from within core.dll with a simple getter function that the shader-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 the core.dll as I can't query for basic window information. E.g. somwhere in shader-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!

Calle Bergström
  • 480
  • 4
  • 12
  • I don't really know what your APIs actually do, but _copying_ that `GLFWwindow` struct there is probably not what you should do. Actually, I don't even know how this code could even compile because the compiler should not even know the definition of `GLFWwindow` because GLFW hides that in it's internal headers only. – derhass Oct 05 '19 at 18:05
  • @derhass that is a good point, I actually made a typo there and `GetWindow` actually just returns a pointer to the window. Sorry about that! – Calle Bergström Oct 06 '19 at 11:29

0 Answers0