Glfw functions are not working in sub classes

I wanted to use glfw functions, but they don’t seemed to be working. Here’s what I did: I have a class inherited from cocos2d::Node, I’ve overridden the visit function and called glfwGetWindowSize, but it returs 0 for width and height. I’ve tried other glfw functions as well and they behave the same.

in the header of my class I have this:

virtual void visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags) override;

and the implementation of this function is this:

void MyClass::visit(cocos2d::Renderer *renderer, const cocos2d::Mat4 &parentTransform, uint32_t parentFlags)
    Node::visit(renderer, parentTransform, parentFlags);

    // glfw part:
    cocos2d::GLViewImpl* glv = (cocos2d::GLViewImpl*)cocos2d::Director::getInstance()->getOpenGLView();
    GLFWwindow* window = glv->getWindow();
    int isVisible = glfwGetWindowAttrib(window, GLFW_VISIBLE);
    int width, height;
    glfwGetWindowSize(window, &width, &height);

When I run this, isVisible , width and height are all 0. But if I put that glfw part in the base Node class’s visit funcion, inside CCNode.cpp , then glfw works fine ( isVisible , width and height are all set).
Am I doing something wrong or I’m not supposed to use glfw functions at all?
I’m on windows using cocos2d-x 3.17.1 with Visual Studio 2017

I don’t know if you are trying to create a wraper for something or to extend some functionality so i apologize if my answer is dumb, but to develop a game there is no need to do that.

There is a singleton class called Director. You can get it using:

auto director = Director::getInstance();

you can also ask for that information like this:

auto visibleSize = Director::getInstance()->getVisibleSize();
auto winSize = Director::getInstance()->getWinSize();
auto winSizeInPixels = Director::getInstance()->getWinSizeInPixels();

auto origin = Director::getInstance()->getVisibleOrigin();

Diretor is initialized in AppDelegate.cpp inside

bool AppDelegate::applicationDidFinishLaunching()

What about using CustomCommand?

Please, read my comments in this topic:

1 Like

What I really wanted is to integrate ImGui ( ) into cocos2d-x and to do that I need direct access to glfw. But unfortunately glfw functions don’t work (at least for me it didn’t work) in my classes/files (not even in classes derived from cocos2d-x base classes).
What I ended up doing is I added some helper functions in the GLViewImpl class for each glfw function that I need (in the CCGLViewImpl-desktop.h and .cpp files).
So, instead of this:

cocos2d::GLViewImpl* glv = (cocos2d::GLViewImpl*)cocos2d::Director::getInstance()->getOpenGLView();
GLFWwindow* window = glv->getWindow();
int width, height;
glfwGetWindowSize(window, &width, &height);

I do this:

cocos2d::GLViewImpl* glv = (cocos2d::GLViewImpl*)cocos2d::Director::getInstance()->getOpenGLView();
int width, height;
glv->myGlfwGetWindowSize(&width, &height);

and myGlfwGetWindowSize looks like this:

void myGlfwGetWindowSize(int* w, int* h)
    glfwGetWindowSize(_mainWindow, w, h);

It’s not an ideal solution, but it works.

When I coded for usb Joystick, i need to do this way only. Something its related to glfw thread, there was one article related to this. Dont remember now. May be @zhangxm can tell us why is it so?

Have you ever tried this ImGui coco2d-x Cocos2d-x v3 integration?

Yes, I’ve tried that. Same problem. Here’s the issue I’ve raised related to this:
It seems that this issue only happens on windows 10? I see working screenshots posted:
It would be good if someone could try this on mac ( ).

I have it working on Windows 10, using Cocos2d-x 3.17.x (latest github code), but I may have updated ImGui to a newer release, and adapted the Cocos2d-x integration code from to work with it.

I’ve attached it to this post, so try it out if you want to see if it works. (476.5 KB)

This is how I link it in the game root CMakeLists.txt:

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/imgui ${PROJECT_BINARY_DIR}/imgui)
get_target_property(imgui_INCLUDE_DIRS imgui-static INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(${APP_NAME} PRIVATE ${imgui_INCLUDE_DIRS})
target_link_libraries(${APP_NAME} PRIVATE imgui-static)

For my projects it is in the [gameproject]/external/imgui path, so you will need to adjust the above CMakeLists.txt sample if you put it anywhere else.