Trying to use GLFW APIs but it crashes

I’m macOS/ M1 and I’m trying to get multiple windows raised on top programmatically.
It seems the only way is to use glfwFocusWindow.
So this is how I’m trying to do:

in .h file

#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>

in .cpp file setup():

void ofApp::setup()
{
      glfwInit();
      .......
}

in .cpp file where I need it:

GLFWwindow* w = (GLFWwindow*) ofGetWindowPtr()->getCocoaWindow();
glfwFocusWindow(w);

But it crashes badly, always, getting an infinite crash message which is impossible for me to understand.

BTW It crashes with any GLFW function I try, for any window setting function, like
glfwShowWindow(GLFWwindow*) or glfwSetWindowTitle(GLFWwindow*, const char* title)

Otherwise GLFW general getting functions work fine, like
glfwGetMonitors(int*) or glfwGetVideoMode(GLFWmonitor* )

I suspect something is wrong in getting GLFWwindow pointer.
Anyone has any clue?
Or, in alternative, any other method to get focus and raise on top a window in macOS?

UPDATE:
Ok, with further googling around I found this post here in the forum:

where Theo shows how to change an Xcode setting to compile a mixed C++/Objective-C source.

Then in Stack Overflow this:

So mixing and matching, it works!

in ofApp.cpp you have to include Cocoa header. It must be exactly in this order:

#include <Cocoa/Cocoa.h>
#include "ofApp.h"

Then where you need it:

NSWindow* cocoaWindow = (NSWindow*) ofGetWindowPtr()->getCocoaWindow();
[cocoaWindow setLevel:NSFloatingWindowLevel];

Quick and simple, but hard to find a meaningful answer about this topic in the whole web.
Hope this will help.

2 Likes