Search code examples
c++glfwbgfx

GLFW library failed to initialized with bgfx


My code does compile:

#define WDW_WIDTH 1280
#define WDW_HEIGHT 720

#define GLFW_INCLUDE_NONE

#if defined (_WIN32)
#define GLFW_EXPOSE_NATIVE_WIN32
#elif defined (__linux__)
#define GLFW_EXPOSE_NATIVE_X11
#elif defined (__APPLE__)
#define GLFW_EXPOSE_NATIVE_COCOA
#endif

#include <iostream>

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

auto GetNativeWindow(GLFWwindow* window) {
    #if defined (GLFW_EXPOSE_NATIVE_WIN32)
        return glfwGetWin32Window(window);
    #elif defined (GLFW_EXPOSE_NATIVE_X11)
        return (void*)(uintptr_t)glfwGetX11Window(window);
    #elif defined (GLFW_EXPOSE_NATIVE_COCOA)
        return glfwGetCocoaWindow(window);
    #else
        std::cout << "undefined platform\n";
    #endif
}

#if defined(GLFW_EXPOSE_NATIVE_X11)
Display* GetNativeDisplay() {
    return glfwGetX11Display();
}
#else 
int* GetNativeDisplay() {
    return NULL;
}
#endif

static void error_callback(int error, const char* description) {
    std::cout << "Error [" << error << "] : " << description << "\n";
}

int main() {

    glfwSetErrorCallback(error_callback);

    if (!glfwInit()) {
        error_callback(1, "failed to init glfw");
        glfwTerminate();
    }
    
    GLFWwindow* window = glfwCreateWindow(WDW_WIDTH, WDW_HEIGHT, "GAME", nullptr, nullptr);
    if (window == nullptr) {
        error_callback(2, "failed to create window");
        glfwTerminate();
    }

    glfwMakeContextCurrent(window);

    bgfx::Init bgfx_init;
    bgfx_init.type = bgfx::RendererType::Count; // Automatically choose a renderer.
    bgfx_init.vendorId = BGFX_PCI_ID_NONE;
    bgfx_init.resolution.width = WDW_WIDTH;
    bgfx_init.resolution.height = WDW_HEIGHT;
    bgfx_init.resolution.reset = BGFX_RESET_VSYNC;
    if (!bgfx::init(bgfx_init)) {
        error_callback(1, "failed to init bgfx");
        glfwTerminate();
    }
    
    bgfx::PlatformData pd;
    pd.ndt = GetNativeDisplay();
    pd.nwh = GetNativeWindow(window);
    bgfx::setPlatformData(pd);

    bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x443355FF, 1.0f, 0);
    bgfx::setViewRect(0, 0, 0, WDW_WIDTH, WDW_HEIGHT);
    

    while (!glfwWindowShouldClose(window)) {
        bgfx::frame();
        glfwPollEvents();
    }

    bgfx::shutdown();
    glfwDestroyWindow(window);
    glfwTerminate();    

    return 0;
}

...but when i tried to run the program i go some error:

Error [1] : failed to init bgfx
Error [65537] : The GLFW library is not initialized
Error [65537] : The GLFW library is not initialized
Segmentation fault (core dumped)

when i delete the bgfx code leaving just glfw, evrything just works and glfw is initialized without error(maybe).

i've also tried using SDL2 but it also have the same error(Segmentation fault (core dumped)).

glfw and bgfx library is installed with vcpkg and build with makefile and my OS is Linux Ubuntu 22.04.3 LTS


Solution

  • the fix was not to set the platform data with bgfx::setPlatformData(pd);. instead i set the platform data into the bgfx_init variable it self

        bgfx::Init bgfx_init;
        bgfx_init.type = bgfx::RendererType::Count; // Automatically choose a renderer.
        bgfx_init.vendorId = BGFX_PCI_ID_NONE;
        bgfx_init.resolution.width = WDW_WIDTH;
        bgfx_init.resolution.height = WDW_HEIGHT;
        bgfx_init.resolution.reset = BGFX_RESET_VSYNC;
        bgfx_init.platformData = pd;
        if (!bgfx::init(bgfx_init)) {
            error_callback(1, "failed to init bgfx");
            glfwTerminate();
        }